hashicorp / terraform-provider-aws

The AWS Provider enables Terraform to manage AWS resources.
https://registry.terraform.io/providers/hashicorp/aws
Mozilla Public License 2.0
9.77k stars 9.12k forks source link

[Enhancement]: Add region argument/parameter where appropriate #27758

Open tmccombs opened 1 year ago

tmccombs commented 1 year ago

Description

When working with multiple regions, currently in most cases you have to use a separate aws provider alias for each region. While this is workable in many cases, there are other cases where it is not very workable. In the best case it means you have to duplicate your provider configuration, varying only in the region. In other cases it can seriously constrain the design of the terraform code. If resources allowed you to specify the region to use for them, through an attribute that override the default region of the provider, than it would simplify working with multiple regions without having to wait for support for more dynamic provider configuration from the terraform engine.

Example Use Cases

Here are some specific use cases where being able to create resources in multiple regions with the same provider would be beneficial:

Affected Resource(s) and/or Data Source(s)

Many. Some of the most important ones are possibly:

Potential Terraform Configuration

The example from https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_replica_key

could be rewritten to something like:

resource "aws_kms_key" "primary" {
  description             = "Multi-Region primary key"
  deletion_window_in_days = 30
  multi_region            = true
  region                  = "us-east-1"
}

resource "aws_kms_replica_key" "replica" {
  description             = "Multi-Region replica key"
  deletion_window_in_days = 7
  primary_key_arn         = aws_kms_key.primary.arn
  region                  = "us-west-2"
}

Or if used in a module that accepts multiple regions something like:

resource "aws_kms_key" "primary" {
  description             = "Multi-Region primary key"
  deletion_window_in_days = 30
  multi_region            = true
  region                  = var.primary_region
}

resource "aws_kms_replica_key" "replica" {
  for_each = var.replica_regions

  description             = "Multi-Region replica key for ${each.key}"
  deletion_window_in_days = 7
  primary_key_arn         = aws_kms_key.primary.arn
  region                  = each.key
}

References

Some more specific issues:

Would you like to implement a fix?

No response

github-actions[bot] commented 1 year ago

Community Note

Voting for Prioritization

Volunteering to Work on This Issue

sblask commented 1 year ago

I am looking into Guardduty and need to generate 23 providers, one for each supported region to enable Guardduty, it would be much simpler to just have a for_each over the regions. Unfortunately Terraform does not allow dynamic providers...

There is also a problem with having the region on provider level, if you create resources in the wrong region and then change the region on the provider, Terraform only wants to create the resource in the new region, but doesn't want to destroy the resource in the wrong region.

tmccombs commented 1 year ago

If I were to create pull requests to add support for this for at least some resources, would that be likely to be accepted? (contingent on it being high enough quality of course)

breathingdust commented 1 year ago

Hi @tmccombs πŸ‘‹ We have been chatting with @brittandeyoung about this (he has put a compelling PoC together in #31517) and we are all positive about the approach. There is some internal due diligence we need to get through in introducing what is a bit of a paradigm change in how the provider is used. We have that scheduled for next quarter after which we will update to community on how we propose to implement and introduce it to the provider, would love feedback at that point.

Appreciate your patience and input!

cobbr2 commented 9 months ago

It's two quarters later, @breathingdust ; is there an update? I don't see one here or on #31517 . Thanks!

take-five commented 1 week ago

Is there any progress on this? The friction it causes in multi-region deployments is very high. EDIT: typo

ewbankkit commented 4 days ago

@take-five πŸ‘‹ The maintainers are actively working on an RFC to enable this functionality. Expect more information in the next couple of months.