terraform-community-modules / tf_aws_vpc

[DEPRECATED] Use https://github.com/terraform-aws-modules/terraform-aws-vpc
Other
211 stars 203 forks source link

get id of private subnet created in vpc module #70

Closed TinajaLabs closed 7 years ago

TinajaLabs commented 7 years ago

I'm using the following to create private subnets in a tf_aws_vpc module. I'm only creating one.

private_subnets    = ["${var.main_subnet_cidr_block}"]

How do I reference and get the subnet ID of the private network so I can use it to create an aws_network_interface?

I'm trying to build a module for network Interfaces as I did not see one already created. Then once I have a network interface I can apply it to the instances. I think.

Thanks for any tips, Chris.

epierotto commented 7 years ago

This terraform module outputs all the private subnets id as an array:

output "private_subnets" {
  value = ["${aws_subnet.private.*.id}"]
}

So lets assume that you are declaring this module like this:

module "vpc" {
  source = "github.com/terraform-community-modules/tf_aws_vpc"
  name = "my-vpc"
  cidr = "10.0.0.0/16"
  private_subnets = ["10.0.1.0/24"]
  public_subnets  = ["10.0.2.0/24"]
  enable_nat_gateway = "true"
  azs      = ["us-west-2a"]
}

To get the outputs references for private subnets you need to use: module.vpc.private_subnets

Remember that the output is a list so to access the elements in that list you can use: element(module.vpc.private_subnets, 0)

An example of usage of the output in another resource:

data "template_file" "private_subnet" {
  template = "dummy"

  vars {
    subnet = "${element(module.vpc.private_subnets, 0)}"
  }
}

Let me know if you need more explanation

TinajaLabs commented 7 years ago

That's perfect. Thank you so much.

bpottier commented 6 years ago

Can someone explain how you would then access those ids as variables for another resource?