【问题标题】:Terraform resolving Cloudformation OutputsTerraform 解析 Cloudformation 输出
【发布时间】:2021-02-08 22:49:18
【问题描述】:

我有一个 terraform 部署,它从 CloudFormation 堆栈部署 SNS 主题。 我导出了 SNS 主题的 ARN,但我正在努力消耗 TF 中的输出

      "Outputs" : {
    "ARN" : {
      "Description" : "Email SNS Topic ARN",
      "Value" : { "Ref" : "EmailSNSTopic" }
    }
  }
}

输出是 ARN。 但是我如何构造 output.tf 文件以允许我在其他 TF 文件中获取 cf 输出?

cf 堆栈名称命名为“TOPIC”作为导出“ARN”的示例 阅读 TF 文档,他们有一个例子,但它失败了:

data "aws_cloudformation_export" "arn" {
  name = "myARN"
}

resource "aws_instance" "web" {
  ami           = "ami-abb07bcb"
  instance_type = "t2.micro"
  subnet_id     = data.aws_cloudformation_export.myarn.value
}

这是正确的吗?

谢谢 尼克

【问题讨论】:

  • 您必须使用 Cloudformation 输出吗?您是否考虑过在 Terraform 中直接使用 aws_sns_topic resource 或使用 aws_sns_topic data source 创建 SNS 主题?你的工作示例有什么错误?看起来您正在尝试使用 SNS 主题 ARN 作为子网 ID,但我不确定您的示例是否不完整?

标签: terraform amazon-cloudformation


【解决方案1】:

TF 模块看起来不正确。试试这个

data "aws_cloudformation_export" "my_subnet_id" {
  name = "[NAME_OF_EXPORT]"
}

resource "aws_instance" "web" {
  ami           = "ami-abb07bcb"
  instance_type = "t2.micro"
  subnet_id     = data.aws_cloudformation_export.my_subnet_id.value
}

并确保 name 属性拼写为aws cloudformation list-exports 的输出并且您在同一区域!!

CF 导出的值在每个账户的给定区域中必须是唯一的,因此您无需引用 CF 模板或堆栈名称。

【讨论】:

    【解决方案2】:

    您的Ref 属性的输出值是错误的。尝试使用SNSTopic 而不是EmailSNSTopic

    完整示例:

    CloudFormationtemplates/cf_aws_sns_email_stack.json.tpl

    {
     "AWSTemplateFormatVersion": "2010-09-09",
      "Resources": {
        "SNSTopic": {
          "Type": "AWS::SNS::Topic",
          "Properties": {
            "TopicName": "${sns_topic_name}",
            "DisplayName": "${sns_display_name}",
            "Subscription": [
              ${alarms_recipients_emails}
            ]
          }
        }
      },
      "Outputs" : {
        "ARN" : {
          "Description" : "SNS Topic ARN",
          "Value" : { "Ref" : "SNSTopic" }
          }
        }
    }
    

    如果你在 terraform 中有这个:

    data "template_file" "aws_cf_sns_stack" {
       template = file("${path.module}/templates/cf_aws_sns_email_stack.json.tpl")
       vars = {
         sns_topic_name        = "your_topic_name"
         sns_display_name      = "your_topic_display_name"
         alarms_recipients_emails = join(",", formatlist("{\"Endpoint\": \"%s\",\"Protocol\": \"%s\"}", var.alarms_recipients_emails, "email"))
       }
     }
     resource "aws_cloudformation_stack" "tf_sns_topic" {
       name = "your_cf_name"
       template_body = data.template_file.aws_cf_sns_stack.rendered
     }
    

    在 terraform 中,您可以使用带有 aws_cloudformation_stack.tf_sns_topic.outputs.ARN 的 ARN 输出。假设您想要一个使用上面创建的主题的 cloudwatch 警报资源:

    resource "aws_cloudwatch_metric_alarm" "foobar" {
      // ...
      alarm_actions       = [aws_cloudformation_stack.tf_sns_topic.outputs.ARN]
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-01
      • 2022-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-23
      • 2020-12-17
      • 2019-01-22
      相关资源
      最近更新 更多