Pydam
Pydam

Reputation: 129

Terraform: Retrieving each subnet gateway address from a list. GCP

Had a question answered earlier to help with creating a set of subnets off a list. Now I am trying to output each assigned ip address.

module "subnets" {
  source = "../../../Modules/subnets-test/"
  network_name = module.vpc.network_name
  subnet_region = "europe-west2"

  subnets = {
    lister = "192.2.128.0/18",
    kryten = "192.2.0.0/17",
    rimmer = "192.2.208.0/20",
    cat = "192.2.192.0/20",
    holly = "192.2.224.0/20"
  }
}

I can successfully output the list of subnets and their values

output "private_subnets" {
  description = "List of IDs of private subnets"
  value       = ["${module.subnets.subnets}"]
}

Giving me all of the subnet outputs (one as example below)

 "rimmer" = {
    "creation_timestamp" = "2020-06-06T03:13:30.244-07:00"
    "description" = ""
    "gateway_address" = "192.2.208.1"
    "id" = "projects/red-dwarf/regions/europe-west2/subnetworks/rimmer"
    "ip_cidr_range" = "192.2.208.0/20"
    "log_config" = []
    "name" = "rimmer"
    "network" = "https://www.googleapis.com/compute/v1/projects/red-dwarf/global/networks/red-dwarf-vpc"
    "private_ip_google_access" = false
    "project" = "red-dwarf"
    "region" = "europe-west2"
    "secondary_ip_range" = []
    "self_link" = "https://www.googleapis.com/compute/v1/projects/red-dwarf/regions/europe-west2/subnetworks/rimmer"

But now I just want to extract the gateway address as a single output. But anything I try to do to the module gives me an error saying the list has 5 attributes.

How can I pull the attributes out of the created subnets when they've been provisioned via map(string) ?

Edit - Subnet Module

resource "google_compute_subnetwork" "subnet" {
  network       = var.network_name
  for_each      = var.subnets
  name         = each.key
  ip_cidr_range = each.value

}

Edit - Subnet Output - This works to output everything as a whole.

output "subnets" {
  value       = google_compute_subnetwork.subnet
  description = "The created subnet resources"
}

Upvotes: 1

Views: 753

Answers (1)

Helder Sepulveda
Helder Sepulveda

Reputation: 17604

Here is what I would do:

locals {
  subnets = {
    cow = "10.0.208.0/20",
    cat = "10.0.192.0/20",
    dog = "10.0.224.0/20"
  }
}

provider "aws" {
  region = "us-east-1"
}

resource "aws_vpc" "myvpc" {
  cidr_block = "10.0.0.0/16"
}

resource "aws_subnet" "subnet" {
  vpc_id     = aws_vpc.myvpc.id
  for_each   = local.subnets
  cidr_block = each.value
  tags       = { Name = each.key }
}

output "subnets" {
  value = aws_subnet.subnet
}

output "subnets_arn" {
  value = { for k, v in aws_subnet.subnet : k => v.arn }
}

The key there is the for loop:
value = { for k, v in aws_subnet.subnet : k => v.arn }
that creates a new object with key the name and value any property we want.

The terraform output of an apply is:

subnets_arn = {
  "cat" = "arn:aws:ec2:us-east-1:841836440307:subnet/subnet-046fff167cdc81e9f"
  "cow" = "arn:aws:ec2:us-east-1:841836440307:subnet/subnet-00217a1ec0531d2c6"
  "dog" = "arn:aws:ec2:us-east-1:841836440307:subnet/subnet-0ac82ef0fd87bcee2"
}



In my case I'm using AWS (that is what I got access to right now) but the same should translate to GCP, just use the property you need, educated guess should be something like:

output "subnets_gateway_address" {
  value = { for k, v in aws_subnet.subnet : k => v.gateway_address }
}

Upvotes: 1

Related Questions