我正在尝试创建一个AWS route53托管区域并向其添加记录。我将以下资源添加到模块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
}然后,我在堆栈main.py中引用该模块如下:
module "route53" {
source = "../../modules/route53"
name = "website.com"
type = "NS"
ttl = "30"
}我的问题是,构建堆栈将对zone和record资源使用相同的名称变量。如何为与route53资源不同的record资源向堆栈模块zone添加另一个名称?
发布于 2022-08-14 13:15:18
如果您在模块中所要做的就是创建一个区域和一个记录,您可以使用拆分从给定的记录名中获取该区域。如下所示:
main.tf
module "route53" {
source = "./modules/route53"
name = "something.website.com"
type = "NS"
ttl = "30"
}模块/route53 53/main.tf
variable "name" {}
variable "type" {}
variable "ttl" {}
resource "aws_route53_zone" "this" {
name = split(".", var.name)[0]
}
resource "aws_route53_record" "this" {
zone_id = aws_route53_zone.this.zone_id
name = var.name
type = var.type
ttl = var.ttl
records = [var.name]
}但是,如果您想要该区域中的多个记录,您可以考虑类似的事情,但这将在很大程度上取决于您所追求的记录配置。
main.tf
module "route53" {
source = "./modules/route53"
name = "website.com"
record_configs = {
something = {
type = "A"
records = ["192.168.0.99"]
}
other = {
type = "CNAME"
records = ["something.website.com"]
}
}
}模块/route53 53/main.tf
variable "name" {}
variable "record_configs" {}
resource "aws_route53_zone" "this" {
name = var.name
}
resource "aws_route53_record" "this" {
for_each = var.record_configs
zone_id = aws_route53_zone.this.zone_id
name = each.key
type = each.value.type
records = each.value.records
}发布于 2022-08-14 01:35:17
如果您有多个记录和名称,最好的方法是使用for_each。例如:
variable "names" {
default = ["website.com", "website1.com", "website2.com"]
}然后
module "route53" {
source = "../../modules/route53"
for_each = toset(var.name)
name = each.key
type = "NS"
ttl = "30"
}这样,您就可以对多个名称具有相同的模块。
发布于 2022-08-14 12:33:33
随机试验解决方案让我将资源变量的名称作为参数添加到模块中。这似乎允许引用根模块中特定资源的参数,如果其参数名称与其他资源相同(例如,record_name与name)。
module "route53" {
source = "../../modules/route53"
name = "website.com"
record_name = "_auth.website.com"
record_type = "NS"
record_ttl = "30"
}https://stackoverflow.com/questions/73348043
复制相似问题