Overview
I imported all of the AWS resources for each AWS region with Terracognita in to a per-region terraform.tfstate
file, in isolation. I am now trying to merge all of those state files, along with the Terraform configuration that came with it. Unfortunately I have to do this because Terracognita doesn’t work with multiple AWS regions simultaneously.
So, I have to modify the terraform.tfstate
file manually after invoking terraform state mv
on each resource from each region. The question is what to se the provider
, type
, and name
JSON properties for each resource, after creating a module for each AWS region that gets instantiated with its own aliased aws
provider.
Details
If I have the following Terraform module definition:
# module_instances.tf
module "provider_us-east-1" {
source = "./us-east-1"
providers = {
aws = aws.us-east-1
}
}
And the following provider definition:
# provider.tf
provider "aws" {
alias = "us-east-1"
region = "us-east-1"
}
And invoking terraform plan
says this:
# aws_vpc.vpc_0b00b33f will be destroyed
# (because aws_vpc.vpc_0b00b33f is not in configuration)
# module.provider_us-east-1.aws_vpc.vpc_0b00b33f will be created
+ resource "aws_vpc" "vpc_0b00b33f" {
...
}
What do I need to change in the terraform.tfstate
file to make sure the VPC doesn’t get destroyed and recreated?
Changing the provider
field in the VPC resource in the terraform.tfstate
JSON to different values has varying effects:
Changing to this value redundantly creates the resources and also emits a couple of errors:
"provider": "provider[\"registry.terraform.io/hashicorp/aws\"]",
Errors:
│ Error: Invalid provider configuration
│
│ Provider "registry.terraform.io/hashicorp/aws" requires explicit configuration. Add a provider block to the root module and
│ configure the provider's required arguments as described in the provider documentation.
│
╵
╷
│ Error: Invalid AWS Region:
│
│ with provider["registry.terraform.io/hashicorp/aws"],
│ on <empty> line 0:
│ (source code not available)
Changing to this value causes all resources to be destroyed and recreated:
"provider": "module.provider_us-east-1.provider[\"registry.terraform.io/hashicorp/aws\"]",
Setting this value prevents any resources from getting destroyed, but redundantly creates resources that already exist:
"provider": "module.provider_us-east-1",
Setting this value emits the Error: Provider configuration not present
error:
"provider": "module.provider_us-east-1.provider[\"registry.terraform.io/hashicorp/aws\"].us-east-1",
Should I instead change one of these JSON fields in the terraform.tfstate
JSON?:
"mode": "managed",
"type": "aws_vpc",
"name": "vpc_0b00b33f",