(Cross-posted to Stack Overflow, for better answer-ranking software.)
Summary
I’m trying to migrate from a single default provider for one AWS region to multiple aliased providers for each AWS region that’s in use for my AWS account. I need to know what I can change the Terraform state JSON to to get Terraform to know which resources apply to which provider instances mentioned in the Terraform HCL configuration.
Version
$ terraform --version
Terraform v1.7.0
on darwin_arm64
+ provider registry.terraform.io/hashicorp/aws v5.31.0
Specifics
I currently have the following Terraform state file, generated when Terracognita imported the state for a single AWS region:
# ./terraform.tfstate
{
"version": 4,
"terraform_version": "1.6.5",
"serial": 1008,
"lineage": "fdc76586-c01b-226a-e481-a73218d5c118",
"outputs": {},
"resources": [
{
"mode": "managed",
"type": "aws_instance",
"name": "name_of_aws_instance",
"provider": "provider[\"registry.terraform.io/hashicorp/aws\"]",
...
}
}
Where the value of provider
is currently for the default provider
instance of aws
. I’m trying to merge the state of several different AWS regions in to the same Terraform state file, by adding the following per-AWS-region aliased provider
instances in Terraform configuration code: (where the text: <aws_region>
is actually replaced with an AWS region, such as: us-east-1
)
# ./provider.tf
provider "aws" {
alias = "<aws_region>"
region = "<aws_region>"
}
All of the resources for a particular AWS region are in *.tf
files in the following directories:
./<aws_region>/
And the modules are instantiated with the following entries:
# ./aws_region_module_instances.tf
module "provider_<aws_region>" {
source = "./<aws_region>"
providers = {
aws = aws.<aws_region>
}
}
With code for the aws_instance
in this file:
# ./us-east-1/ec2.tf
resource "aws_instance" "name_of_aws_instance" {
provider = aws.us-east-1
...
}
If I add a property: provider = aws.us-east-1
to the HCL code for the resource "aws_instance" "name_of_aws_instance"
entity, thus changing its provider, what should the provider
field be set to in the terraform.tfstate
JSON for the local Terraform state?
Note that the provider
field in the terraform.tfstate
file is currently set to:
provider[\"registry.terraform.io/hashicorp/aws\"]
And changing it to any of the following doesn’t work:
provider[\"registry.terraform.io/hashicorp/aws\"].us-east-1
module.provider_us-east-1.provider[\"registry.terraform.io/hashicorp/aws\"].us-east-1
aws.us-east-1
And renders the same error when invoking terraform validate
:
│ Error: Provider configuration not present
│
│ To work with module.provider_us-east-1.aws_instance.name_of_aws_instance its original provider configuration at
│ module.provider_us-east-1.provider["registry.terraform.io/hashicorp/aws"].us-east-1 is required, but it has been removed.
│ This occurs when a provider configuration is removed while objects created by that provider still exist in the state. Re-add
│ the provider configuration to destroy module.provider_us-east-1.aws_instance.name_of_aws_instance, after which you can
│ remove the provider configuration again.