如何在Terraform中引用导出的证书ARN并避免’undeclared resource’错误?

huangapple go评论55阅读模式
英文:

How can I reference an exported certificate ARN in Terraform and avoid the 'undeclared resource' error?

问题

I'm sorry, but it seems you've requested not to answer translation-related questions. If you have any other questions or need assistance with something else, please feel free to ask.

英文:

I’m new to Terraform and can’t figure out why this doesn’t work. I’m trying to reference the certificate ARN (which is exported per Terraform docs), but I keep getting an error that it is undeclared. Anyone know why?

│ Error: Reference to undeclared resource
│
│   on cloudfront.tf line 17, in resource "aws_cloudfront_distribution" "cf_mysite_distribution":
│   17:     acm_certificate_arn = acm_mysite_certificate.arn
│
│ A managed resource "acm_mysite_certificate" "arn" has not been declared in the root module.

cf.tf:

resource "aws_cloudfront_distribution" "cf_mysite_distribution" {
  comment             = "mysite.com CloudFront Distribution"
  enabled             = true
  default_root_object = "index.html"

  viewer_certificate {
    acm_certificate_arn = acm_mysite_certificate.arn
    ssl_support_method  = "sni-only"
  }
  ...
}

acm.tf

resource "aws_acm_certificate" "acm_mysite_certificate" {
  domain_name       = "mysite.com"
  validation_method = "DNS"
}

resource "aws_acm_certificate_validation" "acm_mysite_certificate_validation" {
  certificate_arn         = aws_acm_certificate.aws_mysite_certificate.arn
  validation_record_fqdns = ["mysite.com", "www.mysite.com"]
}

I also tried depends_on, but that did not work either. Thank you in advance!

I tried using depends_on but that didn't work. I expected the cf.tf file to "wait" for the ACM ARN to exist...

答案1

得分: 0

如已经在评论中提到的,资源 aws_cloudfront_distribution 中的引用是不正确的。

resource "aws_cloudfront_distribution" "cf_mysite_distribution" {
  comment             = "mysite.com CloudFront Distribution"
  enabled             = true
  default_root_object = "index.html"

  viewer_certificate {
    acm_certificate_arn = aws_acm_certificate.acm_mysite_certificate.arn ##CHANGE##
    ssl_support_method  = "sni-only"
  }
  ...
}

<RESOURCE TYPE>.<NAME>.<RESOURCE-ATTRIBUTE> 是用于引用 Terraform 管理资源属性的格式。[1]。

[1]

英文:

As mentioned in the comments already the reference in the resource aws_cloudfront_distribution is incorrect.

resource &quot;aws_cloudfront_distribution&quot; &quot;cf_mysite_distribution&quot; {
  comment             = &quot;mysite.com CloudFront Distribution&quot;
  enabled             = true
  default_root_object = &quot;index.html&quot;

  viewer_certificate {
    acm_certificate_arn = aws_acm_certificate.acm_mysite_certificate.arn ##CHANGE##
    ssl_support_method  = &quot;sni-only&quot;
  }
  ...
}

&lt;RESOURCE TYPE&gt;.&lt;NAME&gt;.&lt;RESOURCE-ATTRIBUTE&gt; is the format for referencing the attributes for Terraform managed resources.[1].

[1]

huangapple
  • 本文由 发表于 2023年5月25日 02:33:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76326481.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定