英文:
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]。
英文:
As mentioned in the comments already the reference in the resource aws_cloudfront_distribution
is incorrect.
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>
is the format for referencing the attributes for Terraform managed resources.[1].
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论