hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io/
Other
42.58k stars 9.54k forks source link

No arguments available to fetch IPv4 CIDR Block from aws subnet #25610

Open sagar89jadhav opened 4 years ago

sagar89jadhav commented 4 years ago

I am trying to pass a variable as a IPv4 CIDR block using aws_subnet resource however, it seems there is no attributes available to fetch CIDR block. Is there a way to fetch IPv4 CIDR block from any of the available terraform resource?

I am referring below article https://www.terraform.io/docs/providers/aws/r/subnet.html

Thanks.

ae-ou commented 4 years ago

You can use the subnet data source. Basically, you pass the ID of the subnet resource to the subnet data source, the data source then retrieves the info relating to the subnet with the matching ID - you can then get data about the subnet from the data source (including its IPv4 CIDR block). Here's an example:

// This data source gets the data for the subnet that matches the specified subnet ID - e.g. subnet-1234567890abcdefg
data "aws_subnet" "selected" {
  id = var.my_subnet_id
}

// You should then be able to get the CIDR block from the data with data.aws_subnet.selected.cidr_block

When you look up a subnet using the ID, it returns all of the arguments that weren't specified (look at the documentation for the subnet data source for an exhaustive list of arguments) in addition to the attributes.