Attaching security group to aws_instance

Hi Experts,

I am new to terrform. I am unable to find how can I attach security group when creating EC2 instance with aws_instance. Adding my code below, please suggest.

resource “aws_instance” “example” {
ami = “ami-07cc15c3ba6f8e287”
instance_type = “t2.micro”
subnet_id = “subnet-0bexxxxxxxx” #Defining subnet ID as default VPC was deleted.

Hi @VishavpreetSingh,

Within the EC2 AWS instance resource you’ve written you can add one of the security_group or vpc_security_group_ids arguments, depending on where the security group is.

For specific information on the terraform resource you are working with feel free to check out the official AWS Terraform provider documentation.