Terraform multiple aws instances multiple subnets

amazon-web-servicesterraform

terraform -v
Terraform v0.12.23
+ provider.aws v2.53.0

I am trying to create 4 instances on put them on 2 different subnets

resource "aws_subnet" "private-subnet-1" {
  vpc_id            = aws_vpc.vpc.id
  cidr_block        = "10.150.2.0/24"
  availability_zone = "us-west-2a"
  tags = {
   Name = "private-subnet-1"
  }
}

resource "aws_subnet" "private-subnet-2" {
  vpc_id            = aws_vpc.vpc.id
  cidr_block        = "10.150.4.0/24"
  availability_zone = "us-west-2b"
  tags = {
   Name = "private-subnet-2"
  }
}

variable "subnet_ids" {
  type    = list(string)
  default = ["aws_subnet.private-subnet-1.id","aws_subnet.private-subnet-2.id"]
}

resource "aws_instance" "web" {
  count = 4
  ami           = "ami-0dc33a373d61e6ce0"
  instance_type = "t2.micro"
  key_name   = "AF_KEY_PAIR"
  subnet_id     = element(var.subnet_ids, count.index)
  vpc_security_group_ids = ["${aws_security_group.web-SG.id}"]
}

When I try to apply I get the follow error

Error: Error launching source instance: InvalidSubnetID.NotFound: The subnet ID 'aws_subnet.private-subnet-1.id' does not exist
        status code: 400, request id: 17b46634-5319-4f78-9898-09142e9cb449
  on test.tf line 175, in resource "aws_instance" "web":
 175: resource "aws_instance" "web" {

Error: Error launching source instance: InvalidSubnetID.NotFound: The subnet ID 'aws_subnet.private-subnet-2.id' does not exist
        status code: 400, request id: d092e6a6-d430-4a28-bd75-5af373c76ac7
  on test.tf line 175, in resource "aws_instance" "web":
 175: resource "aws_instance" "web" {

Error: Error launching source instance: InvalidSubnetID.NotFound: The subnet ID 'aws_subnet.private-subnet-1.id' does not exist
        status code: 400, request id: ec672172-3bf7-4cbb-bce0-ae752c59393f
  on test.tf line 175, in resource "aws_instance" "web":
 175: resource "aws_instance" "web" {

Error: Error launching source instance: InvalidSubnetID.NotFound: The subnet ID 'aws_subnet.private-subnet-2.id' does not exist
        status code: 400, request id: 4136f03c-1a5b-44ab-b0bf-a209134c08c9
  on test.tf line 175, in resource "aws_instance" "web":
 175: resource "aws_instance" "web" {

I have tried to change up the

subnet_id     = element(var.subnet_ids, count.index)

and

variable "subnet_ids" {
  type    = list(string)
  default = [aws_subnet.AF-private-subnet-1.id,aws_subnet.AF-private-subnet-2.id]
}

I still get the same error

When I create the subnet_id manually it will work

subnet_id     = aws_subnet.public-subnet-1.id

Best Answer

The issue you're encountering is how variable subnet_ids is interpreting your list. It's not seeing the ids as variables but instead it's interpreting them as strings.

I used the locals for the variable and it worked as you'd expect.

Try these changes to your code:

locals {
  subs = concat([aws_subnet.private-subnet-1.id], [aws_subnet.private-subnet-2.id])
}

resource "aws_instance" "web" {
  count = 4
  ami           = "ami-0dc33a373d61e6ce0"
  instance_type = "t2.micro"
  key_name   = "AF_KEY_PAIR"
  subnet_id     = element(local.subs, count.index)
  vpc_security_group_ids = ["${aws_security_group.web-SG.id}"]
}

References

Local Values
Expressions