Terrform launch aws_instance with existing security group

 Terraform launch aws_instance with existing security group

Hi Friends πŸ˜€, 

Before going ahead,😌  I am assuming that we all have bit knowledge of how terraform works for AWS infrastructure.

You can learn more on it official site  Terraform By HashiCorp .πŸ’₯πŸ’₯

We can create new security group and attached to newly created AWS instance with terraform.
But some time we require to attached existing Security Group only. πŸ’

Here , I am helping out in such condition. 😎

πŸ‘‰First, Create main.tf terraform file. 
πŸ‘‰Then use vpc_security_group_ids field as mentioned below 

provider "aws" {
    region = "ap-south-1"

}

resource "aws_instance" "Instance_from_ami" {
    ami = "your_ami_ID"
    instance_type = "t2.micro"
    vpc_security_group_ids = [ 
                "your_sg_group1",
                "your_sg_group2" 
]
    tags = {
Created_Instance_from = "Terraform_script"
    }
}

πŸ‘‰Then use terraform command to apply your template.

➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖

πŸ‘‰If  you are using .tfvars file then below are one method you can use.

✔your variables.tf ,

variable "sg_ID" { 

}
 
✔your .tfvars file ,

sg_ID="my_security_group"

✔your main.tf file,

provider "aws" {
    region = "ap-south-1"

}

resource "aws_instance" "Instance_from_ami" {
    ami = "your_ami_ID"
    instance_type = "t2.micro"
    vpc_security_group_ids = [ var.sg_ID ]
    tags = {
Created_Instance_from = "Terraform_script"
    }
}

πŸ‘‰Then apply your terraform command with -var-file attribute .
 
➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖

I have explained you with a very easy manner usage of vpc_security_group_ids.
And I hope you feel this article very useful in you DevOps Carrier. 

Thank you for reading...😊😊

Keep reading and Keep sharing.....πŸ’¬πŸ’¬πŸ‘

For more DevOps related tips and Tricks please visit my website scipterra.blogstop.com



Comments

Popular posts from this blog

System Administrator to DevOps Engineer Roadmap - Level 1

Roadmap for Devops Engineer - Level 1