簡體   English   中英

如何將 AZ 列表從 Terraform 傳遞到 CloudFormation 模板?

[英]How to pass a list of AZs from Terraform to a CloudFormation template?

我有一個定義此參數的 CloudFormation 模板:

        "AvailabilityZones" : {
            "Description" : "List of Availability Zones used ... ",
            "Type" : "List<AWS::EC2::AvailabilityZone::Name>"
        },

我像這樣調用 CF 模板:

resource "aws_cloudformation_stack" "xxx" {

  name = "xxx-stack"

  template_body = file("../cloudformation/xxx_CloudFormation_template_2.1.1.json")

  parameters = {
    VpcId                = aws_vpc.xxxvpc.id
    SubnetCidrBlock      = var.aws_vpc_cidr 
    AvailabilityZones    = var.aws_azs
    InstanceType         = "m5.4xlarge"
    ExternalNet          = "0.0.0.0/0"
    ....
  }

  on_failure = "DELETE"
}

在哪里:

variable "aws_azs" {
  default = ["us-east-1a", "us-east-1b", "us-east-1c"]
}

我收到錯誤:

Inappropriate value for attribute "parameters": element "AvailabilityZones": string required.

我在這個主題上嘗試了很多變體,但我看不到如何將 AZ 列表從 TF 傳遞給 CF 模板。

我也對錯誤消息中需要字符串的斷言感到有點困惑,因為 CF 模板中的參數類型被定義為列表。

請問有什么想法嗎?

我更喜歡使用templatefile函數而不是您在template_body字段中使用的file 您可以在那里傳遞變量,並使用漂亮的模板引擎用傳遞的變量填充模板。 您可以在那里使用變量、函數、語句和循環。

示例在這里https://www.terraform.io/docs/language/functions/templatefile.html

你的榜樣

resource "aws_cloudformation_stack" "xxx" {

  ....

  template_body = templatefile("../cloudformation/xxx_CloudFormation_template_2.1.1.json", {
 aws_azs = var. aws_azs
})

  ....
}

和你的 cf 文件:

 "AvailabilityZones" : {
            "Description" : "List of Availability Zones used ${join(',', aws_azs)} ",
            "Type" : "List<AWS::EC2::AvailabilityZone::Name>"
        },

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM