I am trying to create an AWS route53 hosted zone and add records to it. I added the following resources to a module main.tf
resource "aws_route53_zone" "zone" {
name = var.name
}
data "aws_route53_zone" "zone_data" {
name = var.name
}
resource "aws_route53_record" "record" {
zone_id = data.aws_route53_zone.zone_data.zone_id
name = var.record_name
type = var.record_type
ttl = var.record_ttl
records = var.record_value
}
Then I reference that module in a stack main.py
as follows:
module "route53" {
source = "../../modules/route53"
name = "website.com"
type = "NS"
ttl = "30"
}
My issue is that building the stack will use the same name variable for both zone
and record
resources. How do I add another name to the stack module route53
for the record
resource that is different from the zone
resource?
3
Answers
Randomly experimenting with solutions got me to add the resource variables' names as arguments to the module. This seems to allow referring to arguments of a specific resource in the root module if its argument name is the same as other resources (e.g.
record_name
vsname
).If you have multiple records and names, the best way is to use
for_each
. For example:then
This way you can have same module for multiple names.
If all you’re trying to do in the module is create a zone and a record, you could use split to get the zone from the record name given. Like this:
main.tf
modules/route53/main.tf
If however, you want multiple records in that zone, you could consider something like this, but this will depend heavily on what record configuration you’re after.
main.tf
modules/route53/main.tf