Maciej
Maciej

Reputation: 1499

Terraform, passing few subnets to autosecaling group

Have a code defining subnets as below:

resource "aws_subnet" "public" {
  count = length(data.aws_availability_zones.available.names)

  vpc_id                  = aws_vpc.main_vpc.id
  cidr_block              = cidrsubnet(var.vpc_cidr, 8, count.index)
  availability_zone       = element(data.aws_availability_zones.available.names, count.index)
  map_public_ip_on_launch = true

  tags = {
    Name = "${var.environment}-public-${element(data.aws_availability_zones.available.names, count.index)}"
  }
}

Later have a code defining autoscaling group:

resource "aws_autoscaling_group" "ec2_testing" {
  availability_zones  = [data.aws_availability_zones.available.names[0]]
  vpc_zone_identifier = [element(aws_subnet.public.*id, count.index),]
  desired_capacity    = 1
  max_size            = 1
  min_size            = 1

  launch_template {
    id      = aws_launch_template.ec2_testing.id
    version = "$Latest"
  }
}

I'm getting error:

A comma is required to separate each function argument from the next.

Edit: would need assign only one subnet.

How vpc_zone_identifier should be corrected? Help please.

Upvotes: 1

Views: 1861

Answers (1)

Maciej
Maciej

Reputation: 1499

Like ydaetskcoR mentioned, two course of actions should be taken:

aws_subnet.public.*id should be aws_subnet.public.*.id. Also you shouldn't be specifying the availability_zones parameter if you are also specifying the vpc_zone_identifier as it's unnecessary and will complicate logic (your current example will fail because those subnets aren't all in the same AZ)

And you either need count on that aws_autoscaling_group to be able to use count.index or you need to just use aws_subnet.public.*.id[0] similarly to how you are using the first AZ in the list of zones in the above parameter.

Adding this for visibility.

Upvotes: 1

Related Questions