Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aws_subnet_ids Data source is returning String #9381

Closed
ghost opened this issue Jul 17, 2019 · 16 comments
Closed

aws_subnet_ids Data source is returning String #9381

ghost opened this issue Jul 17, 2019 · 16 comments
Labels
service/ec2 Issues and PRs that pertain to the ec2 service.

Comments

@ghost
Copy link

ghost commented Jul 17, 2019

This issue was originally opened by @rajivreddy as hashicorp/terraform#22099. It was migrated here as a result of the provider split. The original body of the issue is below.


Terraform Version

➜  private_subnets git:(dev) ✗ terraform -v
Terraform v0.12.2
+ provider.aws v2.14.0

Terraform Configuration Files

data.tf

data "aws_subnet_ids" "public_subnet_ids" {
  vpc_id = var.vpc_id
  tags = merge(
    {
      "Environment" = format("%s", var.environment)
      "Tier"        = var.public_subnet_suffix
    },
    var.additional_tags
  )
}

main.tf

resource "aws_nat_gateway" "nat" {
  count = var.create_nat_in_each_az ? 3 : 1
  allocation_id = element(aws_eip.nat_eip.*.id,count.index)
  subnet_id     =  element(data.aws_subnet_ids.public_subnet_ids,count.index) # this is not working
  tags = merge(
    {
      "Name"        = format("%s-nat-gateway-%s-%d", var.name, var.environment,count.index+1)
      "Environment" = format("%s", var.environment)
    },
    var.additional_tags
  )
  lifecycle {
    create_before_destroy = true
    ignore_changes = [
      tags,
    ]
  }
}

Expected Behavior

aws_nat_gateway modules should be able to get individual public_subnet_ids based on count.index based on https://www.terraform.io/docs/providers/aws/d/subnet_ids.html#example-usage
As return value of data.aws_subnet_ids.public_subnet_ids.ids should be a list

Actual Behavior

➜  private_subnets git:(dev) ✗ terraform apply
data.aws_availability_zones.az[0]: Refreshing state...
aws_eip.nat_eip[0]: Refreshing state... [id=eipalloc-sdsd]
data.aws_subnet_ids.public_subnet_ids: Refreshing state...

Error: Error in function call

  on main.tf line 30, in resource "aws_nat_gateway" "nat":
  30:   subnet_id     =  "${element(data.aws_subnet_ids.public_subnet_ids.ids,count.index)}"
    |----------------
    | count.index is 0
    | data.aws_subnet_ids.public_subnet_ids.ids is set of string with 3 elements

Call to function "element" failed: cannot read elements from set of string.

Steps to Reproduce

Please list the full steps required to reproduce the issue, for example:
use the example listed https://www.terraform.io/docs/providers/aws/d/subnet_ids.html#example-usage with 0.12.x TF version

  1. terraform init
  2. terraform plan
  3. terraform apply

References

@github-actions github-actions bot added the needs-triage Waiting for first response or review from a maintainer. label Jul 17, 2019
@aflatto
Copy link

aflatto commented Jul 22, 2019

+1

I have just encountered that same issue:
Terraform v0.12.2

  • provider.aws v2.16.0
  • provider.template v2.1.2

@pavanhg
Copy link

pavanhg commented Jul 23, 2019

+1

Same issue for me.

Issue occurred at --> subnet_id = "${element(data.aws_subnet_ids.test.ids, 0)}

  • Terraform v0.12.3
  • provider.aws v2.20.0

@eguven
Copy link
Contributor

eguven commented Jul 23, 2019

Having the same issue with

  • Terraform v0.12.4/v0.12.5
  • provider.aws v2.20.0

As a workaround splat syntax works

  subnet_id = element(data.aws_subnet_ids.foobar.ids[*], count.index)

@aflatto
Copy link

aflatto commented Jul 23, 2019

Trying the work around did not work for me

data "aws_subnet_ids" "nets" {
  vpc_id = "${aws_vpc.main.id}"

 depends_on = [aws_vpc.main]
}


resource "aws_instance" "mongo" {
  provider    = "aws"
  ami           = "ami-00aa61be0e9a8f948"
  subnet_id     = "${element(data.aws_subnet_ids.nets.ids[*], count.index)}"
}

Gets this error

Error: no matching subnet found for vpc with id vpc-095312a0dbf33a83f

on databases.tf line 9, in data "aws_subnet_ids" "nets":
9: data "aws_subnet_ids" "nets" {

while the VPC has been created

@pavanhg
Copy link

pavanhg commented Jul 23, 2019

Tried using locals instead of element. This worked for me:

data "aws_subnet_ids" "mysubnets" {
  vpc_id = "${aws_vpc.myVpc.id}"
}

locals {
  subnet_ids_string = join(",", data.aws_subnet_ids.mysubnets.ids)
  subnet_ids_list = split(",", local.subnet_ids_string)
}

resource "aws_nat_gateway" "nat" {
     allocation_id = "${aws_eip.elastic_ip.id}"
     subnet_id = local.subnet_ids_list[0]
}

@aeschright aeschright added the service/ec2 Issues and PRs that pertain to the ec2 service. label Aug 2, 2019
@ninjapugdevil
Copy link

Convert it to a list should work

data "aws_subnet_ids" "private" {
vpc_id = "${var.vpc_id}"

tags = {
Tier = "Private"
}
}

resource "aws_instance" "app" {
count = "3"
ami = "${var.ami}"
instance_type = "t2.micro"
subnet_id = "${element(tolist(data.aws_subnet_ids.private.ids), count.index)}"
}

@toddlers
Copy link

toddlers commented Aug 9, 2019

I used something like this :

# define a data provider
data "aws_subnet" "subnet-prv-1" {
  count = "${length(data.aws_subnet_ids.prv-1.ids)}"
  id    = "${tolist(data.aws_subnet_ids.prv-1.ids)[count.index]}"
}
 
# use them in this way
subnets         = ["${data.aws_subnet.subnet-prv-1[0].id}"]

@rajivreddy
Copy link

@toddlers Yes
tolist()
Function will works

@rekahsoft
Copy link

As @ninjapugdevil and @rajivreddy mentioned, using the tolist function will work. This is because the aws_subnet_ids data source returns a set and not a list.

See:

@bbaptist
Copy link

Someone really needs to update the docs to say that then.

"aws_subnet_ids provides a list of ids for a vpc_id"

As @ninjapugdevil and @rajivreddy mentioned, using the tolist function will work. This is because the aws_subnet_ids data source returns a set and not a list.

See:

@rajivreddy
Copy link

Hi @bbaptist,
I already raised a PR for updating docs.

@sayyedarshad
Copy link

Convert it to a list should work

data "aws_subnet_ids" "private" {
vpc_id = "${var.vpc_id}"

tags = {
Tier = "Private"
}
}

resource "aws_instance" "app" {
count = "3"
ami = "${var.ami}"
instance_type = "t2.micro"
subnet_id = "${element(tolist(data.aws_subnet_ids.private.ids), count.index)}"
}

This works for me. Thanks

@CyberKiller40
Copy link

Convert it to a list should work

data "aws_subnet_ids" "private" {
vpc_id = "${var.vpc_id}"

tags = {
Tier = "Private"
}
}

resource "aws_instance" "app" {
count = "3"
ami = "${var.ami}"
instance_type = "t2.micro"
subnet_id = "${element(tolist(data.aws_subnet_ids.private.ids), count.index)}"
}

Thanks, this works. Strange that it doesn't work directly, if it looks like a list, then I want to access it like a list.

@RafPe
Copy link

RafPe commented Apr 29, 2021

I did that one a little bit differently than using count in my target resource.

data "aws_subnet_ids" "ecs" {
  vpc_id = "vpc-123"

  tags = {
    Name = "*private*"
  }

}

data "aws_subnet" "ecs" {
  count = "${length(data.aws_subnet_ids.ecs.ids)}"
  id    = "${tolist(data.aws_subnet_ids.ecs.ids)[count.index]}"
}


resource "aws_ec2_transit_gateway_vpc_attachment" "ecs" {
  depends_on         = [data.aws_subnet_ids.ecs]
  subnet_ids = flatten(data.aws_subnet.ecs.*.id)
  transit_gateway_id = aws_ec2_transit_gateway.tgw.id
  vpc_id             = "vpc-123"
}

@ewbankkit ewbankkit removed the needs-triage Waiting for first response or review from a maintainer. label Aug 17, 2021
@ewbankkit
Copy link
Contributor

The new aws_subnets data source returns ids as a list.

@github-actions
Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
service/ec2 Issues and PRs that pertain to the ec2 service.
Projects
None yet
Development

No branches or pull requests