4

I'm creating private subnets with Terraform:

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

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

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

Later I'm trying to create SSM parameter with:

resource "aws_ssm_parameter" "private_subnets_ids" {
  name  = "/${var.client_code}-${var.environment}/backend/SUBNET_IDS"
  type  = "StringList"
  value = aws_subnet.private.*.id
}

As subnets resource is making three subnets, it raises the following error:

   4:   value = aws_subnet.private.*.id
    |----------------
    | aws_subnet.private is tuple with 3 elements

Inappropriate value for attribute "value": string required.

How should I pass this three element tuple to the StringList type parameter?

Grzegorz Oledzki
  • 23,614
  • 16
  • 68
  • 106
Maciej
  • 1,209
  • 4
  • 16
  • 26

2 Answers2

11

The value parameter for the aws_ssm_parameter resource needs to be a string type regardless of the type specified. In fact, AWS always expects parameters to be of a string type as seen in the API docs and mentioned in this answer and the StringList type is essentially metadata for the client to expect it to be a string that contains other strings concatenated together by a comma character.

To convert your tuple type from aws_subnet.private.*.id into a list you can join it with the join function like this:

resource "aws_ssm_parameter" "private_subnets_ids" {
  name  = "/${var.client_code}-${var.environment}/backend/SUBNET_IDS"
  type  = "StringList"
  value = join(",", aws_subnet.private.*.id)
}
ydaetskcoR
  • 53,225
  • 8
  • 158
  • 177
  • 1
    Thank you, as always :) Small correction to anwser: `value = join(",", aws_subnet.private.*.id)` - coma was missing – Maciej Nov 20 '20 at 07:02
0

Here is full example:

resource "aws_ssm_parameter" "SubnetIDs" {
  name        = "SubnetIDs"
  description = "SubnetIDs"
  type        = "StringList"
  value       = join(", ", aws_subnet.private-subnet.*.id)
}
Amir Babic
  • 41
  • 1