This script works fine for creating EC2 instances:
provider "aws" {
access_key = "ACCESS_KEY"
secret_key = "SECRET-KEY"
region = "us-east-2"
}
resource "aws_instance" "web" {
count = 3
ami = "ami-001as38b07206d4ceaf"
instance_type = "t2.micro"
key_name = "labkey"
vpc_security_group_ids = [ "sg-341233f1856" ]
}
Now I am trying to create a security group. The script works fine however I cannot find out how to predefined the "vpc_security_group_ids" when creating the security group
resource "aws_security_group" "Testlab_VPC_Security_Group" {
vpc_id = aws_vpc.Testlab_VPC.id
name = "Testlab VPC Security Group"
description = "Testlab VPC Security Group"
ingress {
cidr_blocks = var.ingressCIDRblock
from_port = 22
to_port = 22
protocol = "tcp"
}
ingress {
cidr_blocks = var.ingressCIDRblock
from_port = 80
to_port = 80
protocol = "tcp"
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "Testlab VPC Security Group"
Description = "Testlab VPC Security Group"
}
}
I would like to be able to combine both of these scripts.
Is there another way to add the security using terraform that I am missing?
Try this
Because this expression refers to the security group, Terraform can infer automatically that the security group must be created first.