hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io/
Other
42.7k stars 9.55k forks source link

Instantiating Multiple Providers with a loop #19932

Open JakeNeyer opened 5 years ago

JakeNeyer commented 5 years ago

Current Terraform Version

Terraform v0.11.11

Use-cases

In my current situation, I am using the AWS provider so I will scope this feature request to that specific provider, although this may extend to other providers as well.

I am attempting to create resources in multiple AWS accounts. The number of the accounts ranges from 0....x and will be dynamic. I would like to be able to instantiate multiple providers which can assume a role in each account, and in turn, create resources with the associated provider without hard-coding providers for each subsequent account.

For example, something like this:


variable "accounts" {
  type    = "list"

  default = ["123456789012", "210987654321"]
}
variable "names" {
  type = "map"

  default = {
    "123456789012" = "foo"
    "210987654321" = "bar"
  }
}
provider "aws" {
  count   = "${length(var.accounts)}"
  alias   = "${lookup(var.names, element(var.accounts, count.index))}"

  assume_role {
    role_arn = "arn:aws:iam::${element(var.accounts, count.index)}:role/ASSUMEDROLE"
  }

resource "aws_instance" "web" {
  count         = "${length(var.accounts)}"
  provider      = "aws.${lookup(var.names, element(var.accounts, count.index))}"
  ami           = "${data.aws_ami.ubuntu.id}"
  instance_type = "t2.micro"

  tags = {
    Name = "HelloWorld"
  }
}
aleksasiriski commented 2 months ago

I've also encountered this limition trying to achieve multi-region deploys for my app, I've ended up using TF templates with Terragrunt to generate Terraform code simulating the desired for_each behavior. It's ugly and doesn't provide any IDE niceness a normal .tf file would, but it works

KeksBombe commented 1 month ago

Please fix... I meant please implement

mcandre commented 1 month ago

I don't understand why the industry has chosen to adopt a tool with a broken for loop. And when you try to explain why you're wrapping Terraform in jinja2, engineers get mad. I'd just as soon use Ansible... or a shell script.

aleksasiriski commented 1 month ago

Take a look at OpenTofu, I believe they're adding support for this in the upcoming release.

kereza commented 1 month ago

@aleksasiriski can you provide some link to opentofu docs about that ?

rgeraskin commented 1 month ago

@mcandre terramate is a great workaround for this limitation instead of jinja2

air3ijai commented 1 month ago

@aleksasiriski can you provide some link to opentofu docs about that ?

Probably this one - https://github.com/opentofu/opentofu/issues/300, and they also have a backlink to this issue, right after this comment.

amirasyraf commented 1 day ago

It seems Hashicorp decided to implement this feature as part of stacks: https://developer.hashicorp.com/terraform/language/stacks/use-cases#deploy-across-regions

... which currently is only available on their HCP Terraform service, with no guarantee this feature will come to the Community edition.

In other news, OpenTofu is officially going to support this natively: https://github.com/opentofu/opentofu/releases/tag/v1.9.0-alpha1