0
votes

I have a production environment that is configured to have a domain name that points to a load-balancer. This is already working, and it was configured using Route53.

I am using Terraform to deploy the infrastructure, including the Route53 record.

The Route53 record was set manually.

I would like for Terraform to manage the Route53 record in subsequent deployments. However, when I run an update to update the infrastructure and include the Route53 record, I get this error:

Error: Error applying plan:

1 error(s) occurred:

 * module.asg.aws_route53_record.www: 1 error(s) occurred:

 * aws_route53_record.www: [ERR]: Error building changeset: 
    InvalidChangeBatch: [Tried to create a resource record set
    [name='foo.com.', type='A'] but it already exists]

Well, at first, this error makes sense, because the resource already exists. But, given this, how can I overcome this issue without causing downtime?

I've tried to manually edit the state file to include the route53 record, but that failed with the same error...

I'm happy to provide more information if necessary. Any suggestions that you might have are welcome. Thank you.

3
Can you edit your question to include the error when you ran terraform import ... to add the record to your state file and also show the plan output?ydaetskcoR

3 Answers

3
votes

You have to import the record into your Terraform state with the terraform import command. You should not edit the state manually!

See the resource Docs for additional information on how to import the record.

3
votes

You can use terraform import to import the existing Route53 resource into your current terraform infrastructure. Here are the steps:

  1. Init terraform with your desire workspace via terraform init.

  2. Define your aws_route53_record exactly the same as the existing resource that you have

    resource "aws_route53_record" "www" { // your code here }

  3. Import the desired resource

terraform import aws_route53_record.www ZONEID_RECORDNAME_TYPE_SET-IDENTIFIER

For example:

terraform import aws_route53_record.www Z4KAPRWWNC7JR_dev.example.com_CNAME

After import successfully, this will save the state of the existing resource.

  1. Do terraform plan to check the resource
  2. You now can update to your existing resource
2
votes

Keeping it here for new visitors.

In the later versions of aws provider(~3.10), they offer an argument allow_overwrite defaults to false. No need to edit state file (not recommended) or do terraform import.

allow_overwrite - (Optional) Allow creation of this record in Terraform to overwrite an existing record, if any. This does not affect the ability to update the record in Terraform and does not prevent other resources within Terraform or manual Route 53 changes outside Terraform from overwriting this record. false by default. This configuration is not recommended for most environments.

from: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route53_record#allow_overwrite