Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Terraform to manage multiple AWS regions

Could someone please give me an example of how to programmatically create Terraform provider aliases based on a variable map? This is what I've tried, but I'm receiving the following error:

variable "aws_regions" {
  default = [
    {
      region = "us-east-1"
      alias  = "default"
    },
    {
      region = "us-east-2"
      alias  = "useast2"
    },
    {
      region = "us-west-1"
      alias  = "uswest1"
    },
    {
      region = "us-west-2"
      alias  = "uswest2"
    },
    {
      region = "eu-central-1"
      alias  = "eucent1"
    }
  ]
}

provider "aws" {
  count  = "${length(var.aws_regions)}"
  region = "${lookup(var.aws_regions[count.index], "region")}"
  alias  = "${lookup(var.aws_regions[count.index], "alias")}"
}

# CloudWatch Log Groups
resource "aws_cloudwatch_log_group" "linux" {
  count    = "${length(var.aws_regions)}"
  provider = "aws.${lookup(var.aws_regions[count.index], "alias")}"

  name = "Linux"
}

Error:

$ terraform plan
* provider.aws.${lookup(var.aws_regions[count.index], "alias")}: count.index: count.index is only valid within resources
like image 912
skohrs Avatar asked Feb 05 '18 22:02

skohrs


People also ask

Is Terraform good for AWS?

Terraform has helped a lot in the DevOps space, changing the way infrastructure is provisioned and managed. Can Terraform be used in AWS? Yes, Terraform can be used in AWS with the help of access and secret keys.

What is multi region deployment AWS?

The Multi-Region Infrastructure Deployment guidance helps customers more easily control updates to infrastructure for applications that are deployed across primary and secondary Regions. This guidance sets up multi-region architectures and maintains consistency of workloads.

Is it possible to configure AWS with Terraform?

You can provide Terraform with an AWS access key directly through the provider, but we recommend that you use a credential profile already configured by one of the AWS Software Developer Kits (SDKs).


1 Answers

It turns out that Terraform provider processing takes place very early and the current version (v.0.11.3) doesn't currently support variable interpolation for providers. I did discover a workaround that isn't too terrible, but it requires a lot of code duplication.

main.tf

# Default Region
provider "aws" {
  region  = "us-east-1"
  version = "~> 1.8"
}

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

provider "aws" {
  alias  = "us-east-2"
  region = "us-east-2"
}

provider "aws" {
  alias  = "us-west-1"
  region = "us-west-1"
}

provider "aws" {
  alias  = "us-west-2"
  region = "us-west-2"
}

provider "aws" {
  alias  = "eu-central-1"
  region = "eu-central-1"
}

# CloudTrail Setup in Default Region
module "cloudtrail" {
  source = "./cloudtrail"
}

# CloudWatch Setup per Region
module "us-east-1_cloudwatch" {
  source = "./cloudwatch"
  providers = {
    "aws.region" = "aws.us-east-1"
  }
}

module "us-east-2_cloudwatch" {
  source = "./cloudwatch"
  providers = {
    "aws.region" = "aws.us-east-2"
  }
}

module "us-west-1_cloudwatch" {
  source = "./cloudwatch"
  providers = {
    "aws.region" = "aws.us-west-1"
  }
}

module "us-west-2_cloudwatch" {
  source = "./cloudwatch"
  providers = {
    "aws.region" = "aws.us-west-2"
  }
}

module "eu-central-1_cloudwatch" {
  source = "./cloudwatch"
  providers = {
    "aws.region" = "aws.eu-central-1"
  }
}

cloudwatch/main.tf

provider "aws" {
  alias = "region"
}

# CloudWatch Log Groups
resource "aws_cloudwatch_log_group" "linux" {
  name     = "Linux"
  provider = "aws.region"

  tags {
    OS = "Linux"
  }
}
like image 200
skohrs Avatar answered Oct 08 '22 17:10

skohrs