Terraform got `Error: Computed attribute cannot be set` when I try to set dns_entry in endpoint definition

Terraform got `Error: Computed attribute cannot be set` when I try to set dns_entry in endpoint definition

Terraform v0.13.5
+ provider registry.terraform.io/hashicorp/aws v3.16.0

描述

我想做一个端点并想把它设置到 Route53 记录中。
我将在域中部署一个 Kubernetes 集群。

我遇到了这个错误。

$ terraform plan

Error: Computed attribute cannot be set

  on route53.tf line 24, in resource "aws_vpc_endpoint" "endpoint":
  24:   dns_entry = [
  25:     {
  26:         "dns_name" = "vpce",
  27:         "hosted_zone_id" = "ap-northeast-1a"
  28:     },
  29:   ]

resource "aws_route53_zone" "primary" {
    name = local.cluster_name
}

resource "aws_route53_record" "cluster" {
    zone_id = aws_route53_zone.primary.zone_id
    name = local.cluster_name
    type = "CNAME"
    ttl = 30
    records = [aws_vpc_endpoint.endpoint.dns_entry[0]["dns_name"]]
}

resource "aws_vpc_endpoint" "endpoint" {
  vpc_id       = module.vpc.vpc_id
  service_name = "com.amazonaws.ap-northeast-1.ec2"
  vpc_endpoint_type = "Interface"
  security_group_ids = [
    module.vpc.default_security_group_id,
  ]
  subnet_ids = [
      module.vpc.public_subnets
  ]
  private_dns_enabled = false
  dns_entry = [
    {
        "dns_name" = "vpce",
        "hosted_zone_id" = "ap-northeast-1a"
    },
  ]
}

我试过的

如果我删除 dns_entry 块,就会出现此错误。

Error: Invalid index

  on route53.tf line 10, in resource "aws_route53_record" "cluster":
  10:     records = [aws_vpc_endpoint.endpoint.dns_entry[0]["dns_name"]]
    |----------------
    | aws_vpc_endpoint.endpoint.dns_entry is empty list of object

The given key does not identify an element in this collection value.

我想知道的

在没有 terraform 的情况下手动创建端点的情况下,我成功并获得了这个 DNS 名称 vpce-0814cfe7cf6dd0f57-t6i209re.ec2.ap-northeast-1.vpce.amazonaws.com

我想使用 Terraform 将这样的 DNS 名称设置到 Route53 记录中。

如何修复错误并设置 DNS 名称?

您的托管区域 ID 似乎不正确:

hosted_zone_id" = "ap-northeast-1a"
  28:     },

该值应该是您域的 Route 53 托管区域 ID。好像是区域id。

dns_entryaws_vpc_endpoint创建后返回给你的。这不是你可以自己设置的。

获取接口端点的dns_namehosted_zone_id

aws_vpc_endpoint.endpoint.dns_entry[0].dns_name
aws_vpc_endpoint.endpoint.dns_entry[0].hosted_zone_id

通常你会有超过 1 个,所以你要么必须遍历 aws_vpc_endpoint.endpoint.dns_entry 要么将它们作为列表获取。