本文介绍了terraform 缺少资源实例键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定我做错了什么.我有这样的地形:

I'm unsure what I'm doing wrong. I have terraform as such:

resource "aws_apigatewayv2_domain_name" "web" {
  domain_name = var.web_url
  count       = var.web_url != "" ? 1 : 0

  domain_name_configuration {
    certificate_arn = var.web_acm_arn
    endpoint_type   = "REGIONAL"
    security_policy = "TLS_1_2"
  }
}

resource "aws_apigatewayv2_api_mapping" "web" {
  api_id      = aws_apigatewayv2_api.web.id
  domain_name = aws_apigatewayv2_domain_name.web.id
  stage       = aws_apigatewayv2_stage.web_stage.id
  count       = var.web_url != "" ? 1 : 0
}

我的 terraform 计划返回了这个.它抱怨计数,但不确定如何处理它.

My terraform plan returns this. it complains about count, but unsure what to do with it.


Terraform v0.12.24
Configuring remote state backend...
Initializing Terraform configuration...
2020/07/29 06:20:46 [DEBUG] Using modified User-Agent: Terraform/0.12.24 TFC/29e17ad841


Error: Missing resource instance key

  on ../modules/web/api.tf line 37, in resource "aws_apigatewayv2_api_mapping" "web":
  37:   domain_name = aws_apigatewayv2_domain_name.web.id

Because aws_apigatewayv2_domain_name.web has "count" set, its
attributes must be accessed on specific instances.

For example, to correlate with indices of a referring resource, use:
    aws_apigatewayv2_domain_name.web[count.index]

感谢您的帮助.

推荐答案

正如错误消息所示,由于您在 aws_apigatewayv2_domain_name 中使用了 count,因此应该使用引用时立即索引.

As the error message suggest, since you've used count in your aws_apigatewayv2_domain_name, you should use index now when you refer to it.

例如:

domain_name = aws_apigatewayv2_domain_name.web[0].id

这篇关于terraform 缺少资源实例键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 20:16