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
Post a Comment