Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform lookup AWS region

I have the following code in my main.tf file:

provider "aws" {
  access_key = "${var.aws_access_key}"
  secret_key = "${var.aws_secret_key}"
  region     = "us-east-1"
  alias      = "us-east-1"
}

provider "aws" {
  access_key = "${var.aws_access_key}"
  secret_key = "${var.aws_secret_key}"
  region     = "us-west-1"
  alias      = "us-west-1"
}

module "us-east_vpc" {
  source = "./setup-networking"

  providers = {
    "aws.region" = "aws.us-east-1"
  }
}

module "us-west_vpc" {
  source = "./setup-networking"

  providers = {
    "aws.region" = "aws.us-west-1"
  }
}

And then in my modules file I have:

provider "aws" {
  alias = "region"
}

resource "aws_vpc" "default" {
  provider             = "aws.region"
  cidr_block           = "${lookup(var.vpc_cidr, ${aws.region.region})}"
  enable_dns_hostnames = true

  tags {
    Name = "AWS VPC"
  }
}

resource "aws_internet_gateway" "default" {
  provider = "aws.region"
  vpc_id   = "${aws_vpc.default.id}"
}

resource "aws_subnet" "default" {
  provider = "aws.region"
  vpc_id   = "${aws_vpc.default.id}"

  cidr_block        = "${lookup(var.subnet_cidr, ${aws.region.region})}"
  availability_zone = "aws.region"

  tags {
    Name = "AWS Subnet"
  }
}

resource "aws_route_table" "default" {
  provider = "aws.region"
  vpc_id   = "${aws_vpc.default.id}"

  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = "${aws_internet_gateway.default.id}"
  }

  tags {
    Name = "Main Gateway"
  }
}

As you can see in the modules file code I am trying to do a lookup to find the VPC CIDR mask and the subnet CIDR mask from map variables.

The trouble is that I can't seem to sort out how to get the region to be used as a lookup value.

If I hard code these values:

cidr_block = "10.10.0.0/16"
cidr_block = "10.10.10.0/24"

The script works as expected but I don't want to hard code the values.

Can someone with more Terraform experience help me understand how I can properly reference the region to lookup the correct value?

like image 356
Brad Avatar asked Jul 31 '18 18:07

Brad


2 Answers

I was looking for the same answer for a different problem. I wanted to get the region for a name of a role, I was able to get the info by doing this:

1.- Create a file like data.tf and add this info:

data "aws_region" "current" {}

2.- Get the info from the data by calling this variable in any TF file:

name = "${var.vpc-name}-${data.aws_region.current.name}-Bastion-Role"

This way it will get the region where you are executing the code, and you don't have to mess with the provider.tf file.

like image 176
Juan Avatar answered Oct 02 '22 19:10

Juan


You can get the region that's currently in use by the provider by using the aws_region data source.

So in your case you could do something like this:

provider "aws" {
  alias = "region"
}

data "aws_region" "current" {
  provider = "aws.region"
}

resource "aws_vpc" "default" {
  provider             = "aws.region"
  cidr_block           = "${lookup(var.vpc_cidr, ${data.aws_region.current.name})}"
  enable_dns_hostnames = true

  tags {
    Name = "AWS VPC"
  }
}

...
like image 28
ydaetskcoR Avatar answered Oct 02 '22 19:10

ydaetskcoR