Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

terraform missing resource instance key

Tags:

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
}

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]

help is appreciated.

like image 590
sebastian Avatar asked Jul 29 '20 06:07

sebastian


1 Answers

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.

For example:

domain_name = aws_apigatewayv2_domain_name.web[0].id
like image 122
Marcin Avatar answered Oct 24 '22 04:10

Marcin