-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Enhancement]: Add region argument/parameter where appropriate #27758
Comments
Community NoteVoting for Prioritization
Volunteering to Work on This Issue
|
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 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. |
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) |
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! |
It's two quarters later, @breathingdust ; is there an update? I don't see one here or on #31517 . Thanks! |
Is there any progress on this? The friction it causes in multi-region deployments is very high. |
@take-five 👋 The maintainers are actively working on an RFC to enable this functionality. |
#13992 is probably the OG issue for this request! |
ProposalMost AWS resources are Regional – they are created and exist in a single AWS Region, and to manage these resources the Terraform AWS Provider directs API calls to endpoints in the Region. The AWS Region used to provision a resource using the provider is defined in the provider configuration used by the resource, either implicitly via environment variables or shared configuration files, or explicitly via the Let’s start with an example. provider "aws" {
region = "us-east-1"
}
provider "aws" {
region = "us-west-2"
alias = "west"
}
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
}
resource "aws_vpc" "peer" {
provider = aws.west
cidr_block = "10.1.0.0/16"
}
# Requester's side of the connection.
resource "aws_vpc_peering_connection" "main" {
vpc_id = aws_vpc.main.id
peer_vpc_id = aws_vpc.peer.id
peer_region = "us-west-2"
auto_accept = false
}
# Accepter's side of the connection.
resource "aws_vpc_peering_connection_accepter" "peer" {
provider = aws.west
vpc_peering_connection_id = aws_vpc_peering_connection.main.id
auto_accept = true
} should be able to be written as: provider "aws" {
region = "us-east-1"
}
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
}
resource "aws_vpc" "peer" {
region = "us-west-2"
cidr_block = "10.1.0.0/16"
}
# Requester's side of the connection.
resource "aws_vpc_peering_connection" "main" {
vpc_id = aws_vpc.main.id
peer_vpc_id = aws_vpc.peer.id
peer_region = "us-west-2"
auto_accept = false
}
# Accepter's side of the connection.
resource "aws_vpc_peering_connection_accepter" "peer" {
region = "us-west-2"
vpc_peering_connection_id = aws_vpc_peering_connection.main.id
auto_accept = true
} This new Terraform code requires only a single provider configuration. ImplementationEvery Regional resource, data source and ephemeral resource will support this feature transparently – the new argument will not need to be explicitly defined in the resource’s schema and (in the majority of cases) the resource implementation will not need to be aware whether or not a resource-level Region override is in place. Using the provider’s interceptor functionality, introduced for transparent tagging, every Regional resource will have the Accessing The Effective RegionThe same interceptor functionality used to transparently inject the new configuration block will be used to handle any configured The provider’s global state object (colloquially known as the provider’s meta object) had a field named AWS API ClientsThe AWS API client lazy initialization mechanism will be extended to store per-Region maps of API clients and return clients for the currently in-effect Region. This ensures that all AWS API calls will be made to the correct Region. Global ResourcesSome resources (for example IAM or STS resources) are global, they exist in all of a partition’s Regions. For such resources, no Global ServicesAll resources for the following services will be considered global:
Importing ResourcesImporting existing resources into Terraform, using either the terraform import CLI command or an import block, does not access configuration and so will not be aware of any desired per-resource Region override. To allow importing resources in Regions different from that specified via provider configuration we will support a standard suffix, For example if the provider configuration specified the us-west-2 Region, % terraform import aws_db_instance.default mydb-rds-instance@us-east-1 will import an RDS DB Instance in the us-east-1 Region. Conflicting Attribute NamesAs of Terraform AWS Provider v5.83.0 a small number of existing resources and data sources already have a top-level Data SourcesThe data sources that act as per-Region lookups will remain as-is:
Other data sources that will remain as-is:
Some data sources will have a well documented change in semantics:
Some data sources will have their existing
ResourcesSome resources will have a well documented change in semantics:
Some resources will have their existing
TimelineWe are now solicting feedback from the community with the goal of finalizing design by the end of January 2025. Implementation will take place in the February 2025 to April 2025 quarter with release (after a planned public beta) in Terraform AWS Provider v6.0.0. |
Note that not all IAM resources are global. IAM Access Analyzer, for example, is regional. |
There is another region-related quirk with the current design of this provider: if you've previously applied a configuration containing AWS resources associated with a particular region and then you change the I wonder if a solution to that problem could also fit in with the changes proposed above. Here's some details on what I'm thinking, although I've not tried this out so I can't promise all of this is sound... The first part I'd add is to make the
After that, compare the region in the prior state to the region in the configuration. If they are different, return a plan with the Taken alone the above rule would force proposing replacement for every object that already exists, because the prior state would have a null The second part is that whenever making requests for a particular object the provider should take care to always use the region associated with the resource instead of the region in the provider configuration. In particular, during When combined with the special planning behavior I just described, this would mean that changing the Overall I don't think what I've described here is significantly different than the proposal above. The main addition is for the provider to replace an unpopulated resource-level One specific problem I've not thought very deeply about is how to handle the case where the region is changed in a way that also changes the partition. As the above proposal notes, each partition has completely separate authentication objects and so it doesn't seem like it would be possible to support a single resource instance being destroyed in a region in one partition and created in a region in a different partition in the same plan/apply round: there is no one set of credentials that could possibly work for both. Perhaps |
@lorengordon Thanks for pointing that out. There is the same ambiguity around Route 53 -- Route 53 profiles and Route 53 resolver are Regional while vanilla Route 53 and Route 53 domains are global. I'll add the names of the service packages (directories below |
@apparentlymart Thanks for the considered comment. We saw a previous iteration of your thoughts in #31517 (comment) and very much would like to implement that capability. The stumbling block to immediate (in the scope of the proposal for v6.0.0) implementation is the capturing of the already in-effect Region (from provider configuration) via the state upgrade mechanism -- we haven't (yet) thought of a way to force the upgrade mechanism to run (i.e. an increment of each resource's schema version number) without manually modifying the ~1500 resource implementations. We would definitely like to introduce the capability in a future version (having captured the in-effect Region once the provider has been upgraded to v6.0.0). I have updated the proposal with details on ensuring that API calls are made to the correct Region (missed in initial copy-paste from internal design documentation). Your point around changing a per-resource Region to one in a different partition is partly handled by implementing verification that old and new Regions are in the same partition, but we would have to think about this more in the case that we are "remembering" the prior Region. In this case I don't think we can delete the resource under the old credentials (credentials are partition-specific) as they are no longer available. |
Hi @ewbankkit! I'd forgotten that I'd shared a similar idea before, so sorry for the redundancy but I'm pleased to see that the two times I wrote this up I wrote essentially the same thing since when I wrote that earlier comment I'd been playing with the idea more actively and so had more relevant context in my head. 😀 Terraform v0.12 and later call In principle then, from Terraform Core's perspective it shouldn't be necessary to increment the schema version number. Whether it's feasible to make the SDK and framework themselves run this special "global" upgrade logic without changing the schema version number is a different question of course, and not one I'm equipped to answer. (What I've said above about |
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:
Or if used in a module that accepts multiple regions something like:
References
Some more specific issues:
aws_cloudwatch_log_subscription_filter
resource #24194Would you like to implement a fix?
No response
The text was updated successfully, but these errors were encountered: