Merge Terraform States
August 13, 2017
If states are being stored in S3, after importing a resource, for example:
$ terraform import aws_customer_gateway.main cgw-b4dc3961
You will end with a terraform.tfstate that differs from the one in the S3 bucket.
To merge the remote and local state do this:
$ terraform state pull > terraform.tfstate
$ terraform import aws_customer_gateway.main cgw-b4dc3961
$ terraform state push terraform.tfstate
First get the current s3 state:
$ terraform state pull > terraform.tfstate
Then import what you need, for example:
$ terraform import aws_customer_gateway.main cgw-b4dc3961
Then push back to s3 the state
$ terraform state push terraform.tfstate