Bootstrap FreeKB - Amazon Web Services (AWS) - List Virtual Private Cloud (VPC) using Terraform
Amazon Web Services (AWS) - List Virtual Private Cloud (VPC) using Terraform


Let's say you have two Amazon Web Services (AWS) Virtual Private Clouds (VPC), one name default, the other named my-vpc.

 

This assumes you have setup Terraform as described in Amazon Web Services (AWS) - Getting Started with Terraform

There are two similar modules that can be used to return Virtual Private Clouds.

  • aws_vpc (this article) - Return a single Virtual Private Cloud
  • aws_vpcs - Return one or more Virtual Private Clouds

Let's say you have the following files on your Terraform server.

├── required_providers.tf
├── virtual_private_clouds (directory)
│   ├── data.tf
│   ├── outputs.tf
│   ├── provider.tf

 

Let's say data.tf has the following. This will create unique variables for each VPC (default_vpc and my_vpc).

data "aws_vpc" "default-vpc" {
  filter {
    name   = "tag:Name"
    values = ["default"]
  }
}

data "aws_vpc" "my-vpc" {
  filter {
    name   = "tag:Name"
    values = ["my-vpc"]
  }
}

 

Instead of using tag:Name, any of the following can be used by the "name" key.

  • cidr - The IPv4 CIDR block of the VPC (e.g. 10.0.0.0/16)
  • cidr-block-association.cidr-block - An IPv4 CIDR block associated with the VPC.
  • cidr-block-association.association-id - The association ID for an IPv4 CIDR block associated with the VPC.
  • cidr-block-association.state - The state of an IPv4 CIDR block associated with the VPC.
  • dhcp-options-id - The ID of a set of DHCP options.
  • ipv6-cidr-block-association.ipv6-cidr-block - An IPv6 CIDR block associated with the VPC.
  • ipv6-cidr-block-association.ipv6-pool - The ID of the IPv6 address pool from which the IPv6 CIDR block is allocated.
  • ipv6-cidr-block-association.association-id - The association ID for an IPv6 CIDR block associated with the VPC.
  • ipv6-cidr-block-association.state - The state of an IPv6 CIDR block associated with the VPC.
  • is-default - true or false
  • owner-id - The ID of the AWS account that owns the VPC.
  • state - The state of the VPC (pending | available).
  • tag:<key> - (e.g. tag:Name).
  • tag-key - The key of a tag assigned to the resource. Use this filter to find all resources assigned a tag with a specific key, regardless of the tag value.
  • vpc-id - The ID of the VPC.

 

For example, here is how you could use "cidr".

data "aws_vpc" "default-vpc" {
  filter {
    name   = "cidr"
    values = ["10.0.0.0/16"]
  }
}

 

Or like this, using tag-key and tag-value.

data "aws_vpc" "my-vpc" {
  filter {
    name = "tag-key"
    values = ["Name"]
  }
  filter {
    name = "tag-value"
    values = ["my-vpc"]
  }
}

 

Be aware that if you used Terraform to create the VPC, Terraform should already know about the VPC, as the VPC will be in the terraform.tfstate file.

AVOID TROUBLE

A resource gets added or update in the terraform.tfstate file when the terraform apply command is used, which means that other commands such as terraform plan, terraform refresh and terraform output will not update the terraform.tfstate file.

output can only be returned for resources that Terraform has listed in the  terraform.tfstate file.

In this scenario, there should be no need to use the data module to get the VPC since the VPC is already in the terraform.tfstate file.

resource "aws_vpc" "my-vpc" {
  cidr_block = "10.0.0.0/16"
  tags = {
    Name = "my-vpc"
  }
}

 

Let's say outputs.tf in your vpc module has the following. Notice here that "data" is used for the default VPC since the data module was used to get the JSON for the default VPC. "data" is not used with my_vpc since my_vpc was created using Terraform, thus my_vpc is already known in the terraform.tfstate file.

output "default_vpc" {
  value = data.aws_vpc.default-vpc
}

output "my_vpc" {
  value = data.aws_vpc.my-vpc
}

 

And outputs.tf in the same directory as your main root module (main.tf) may have something like this.

output "defaultvpc" {
  value = module.virtual_private_clouds.default_vpc
}

output "myvpc" {
  value = module.virtual_private_clouds.my_vpc
}

 

terraform refresh should return something like this.

vpc = {
  "arn" = "arn:aws:ec2:us-east-1:123456789012:vpc/vpc-0521ac4b76ed94d8a"
  "cidr_block" = "10.0.0.0/16"
  "cidr_block_associations" = tolist([
    {
      "association_id" = "vpc-cidr-assoc-09ff7264a51c86a4b"
      "cidr_block" = "10.0.0.0/16"
      "state" = "associated"
    },
  ])
  "default" = false
  "dhcp_options_id" = "dopt-017f0a715e4ce2fc9"
  "enable_dns_hostnames" = false
  "enable_dns_support" = true
  "filter" = toset([
    {
      "name" = "tag:Name"
      "values" = toset([
        "my-vpc",
      ])
    },
  ])
  "id" = "vpc-0521ac4b76ed94d8a"
  "instance_tenancy" = "default"
  "ipv6_association_id" = ""
  "ipv6_cidr_block" = ""
  "main_route_table_id" = "rtb-091e5416b1c0d33a6"
  "owner_id" = "123456789012"
  "state" = tostring(null)
  "tags" = tomap({
    "Name" = "my-vpc"
  })
  "timeouts" = null /* object */
}

 

And here is how you can output just the value of a certain key, the "id" key in this example.

output "vpc id" {
  value = data.aws_vpc.default_vpc.id
}

 

Which should return something like this.

vpc = "vpc-0521ac4b76ed94d8a"

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter a713b5 in the box below so that we can be sure you are a human.