Import existing infrastructure into Terraform

Hi all.

I’ve created a new topic about bulk importing Route53 Record a couple of days ago, but haven’t had any replies yet. I was wondering if there is a way to import into the terraform state some aws_route53_record resources in a .tf file?

For example:

The record already exists in my AWS Account. Then, I create the following aws_route53_record record:

resource "aws_route53_record" "www_example_com" {
  count   = (terraform.workspace == "prod") ? 1 : 0
  name    = ""
  records = [""]
  ttl     = "60"
  type    = "CNAME"
  zone_id = one(aws_route53_zone.r53_example_com[*].zone_id)

If I do a terraform plan, terraform shows me it will try to create this resource. However, it already exists in my AWS account, but it’s not imported into the Terraform state file, therefore, Terraform doesn’t know it already exists.

Is there a way to import it into the Terraform state? Without having to repeat terraform import aws_route53_record.myrecord Z4KAPRWWNC7JR_dev.example.com_NS hundreds of times?

That is the import command (or similar for the different records) that you need to run if you are wanting Terraform to take over the management of existing resources.

Yeah, I know that… I just don’t wanna run it hundreds of times… There must be another way to automate this.

You can write your own script that takes the output of the AWS CLI and then runs terraform import if you want.