Search code examples
terraform-provider-awsamazon-vpc

Using for_each with provider setting


I try to create VPCs in different regions for doing this I have multiple providers set up that I have given the alias equal to its region. The VPCs is set up with a for_each where the each.key is the region I want to spin up the VPC in. The issue I have is that i can't find a way to use the each.key with the "aws." prefix needed for the provider setting for the resource.

This is what I try to do:

provider "aws" {
  alias = "eu-west-1"
  profile = "Terraform"
  region = "eu-west-1"
}
provider "aws" {
  alias = "eu-west-2"
  profile = "Terraform"
  region = "eu-west-2"
}

locals {
  pools = {
    "eu-west-1" = "${data.aws_vpc_ipam_pool.pooleu-west-1.id}"
    "eu-west-2" = "${data.aws_vpc_ipam_pool.pooleu-west-2.id}"
  }
}

resource "aws_vpc" "default" {
  for_each = local.pools
  provider = aws.${each.key}
  ipv4_ipam_pool_id = each.value
  enable_dns_support   = true
  enable_dns_hostnames = true

  tags = {
    Name = "main-vpc-${each.key}"
  }
}

Can this be done or should I try to find another solution? I cant find any answers to this and I am to under experienced with terraform to know this.


Solution

  • After a lot of resource I have found this thread on the topic and it's not possible (yet and maybe never) to assign the provider dynamically...

    You can read more here: https://github.com/hashicorp/terraform/issues/24476