long time lurker first time posting.
I am new to using terraform and tried going through the documentation and it isn't documented clearly enough for me to resolve my issue.
Scenario
I am trying to simulate the event that an end-user accidentally deletes a public hosted zone from route53 then using terraform to restore the public zone and it's record sets.
Issue
The issue I'm running into is that when the public hosted zone is removed, running terraform plan will freak out and say that the route53 record/zone does not exist but doesn't actually prompt me to create that new zone even though it is being managed terraform currently.
The workaround I have is to run terraform state rm on the imported objects, editing my example.tf file to remove any route53 resource record sets, leaving only the aws_route53_zone record intact and re-running terraform plan, doing so terraform recognizes that this is a new public hosted zone and will create the new public zone. The problem with that is, is that it's done in two steps and I would like to achieve this all in one step ideally running terraform plan once to create the hosted zone then creating the record sets in the new zone.
Current working solution - for example.tf
This method requires the administrator to run terraform plan twice and involves editing the example.tf file. First to remove the existing record sets that terraform manages in order to run terraform plan to create the public hosted zone that was accidentally deleted.
resource "aws_route53_zone" "example" {
name = "terraform-test.example.com"
}
The ideal solution - example.tf
This is the solution I had in mind that would work, but when I run terraform plan it complains about the resource not found or undeclared.
# This should create the public hosted zone if it's not existing.
resource "aws_route53_zone" "example" {
name = "terraform-test.example.com"
private_zone = false
}
# Then it should create the following records under the hosted zone.
resource "aws_route53_record" "www-terraform-A" {
name = "${data.aws_route53_zone.example.name}"
zone_id = "${data.aws_route53_zone.example.zone_id}"
type = "A"
ttl = "300"
records = ["X.X.X.X"]
}
I expect after running terraform plan that terraform recognizes the zone isn't existing and it should prompt me to create it then record the record sets but it doesn't this is the error message I get
Error: Reference to undeclared resource
on terraform-test.servallapps.com.tf line 6, in resource "aws_route53_record" "www-terraform-A":
6: name = "${data.aws_route53_zone.example.name}"
A data resource "aws_route53_zone" "example" has not been declared in the
root module.