简体   繁体   English

如何将 AZ 列表从 Terraform 传递到 CloudFormation 模板?

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

I have a CloudFormation template that defines this parameter:我有一个定义此参数的 CloudFormation 模板:

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

I invoke the CF template like this:我像这样调用 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"
}

Where:在哪里:

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

I get the error:我收到错误:

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

I have tried many variations on this theme, but I can't see how to pass a list of AZs to the CF template from TF.我在这个主题上尝试了很多变体,但我看不到如何将 AZ 列表从 TF 传递给 CF 模板。

I am also more than a little puzzled by the assertion that a string is required in the error message, as the parameter type in the CF template is defined as a list.我也对错误消息中需要字符串的断言感到有点困惑,因为 CF 模板中的参数类型被定义为列表。

Any ideas please?请问有什么想法吗?

I would prefer to use the templatefile function instead of the file you are using in the template_body field.我更喜欢使用templatefile函数而不是您在template_body字段中使用的file You can pass variables there and fill the template with passed variables with nice template engine.您可以在那里传递变量,并使用漂亮的模板引擎用传递的变量填充模板。 You can use variables, functions, statements and loops there.您可以在那里使用变量、函数、语句和循环。

Example is here https://www.terraform.io/docs/language/functions/templatefile.html示例在这里https://www.terraform.io/docs/language/functions/templatefile.html

Your example你的榜样

resource "aws_cloudformation_stack" "xxx" {

  ....

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

  ....
}

and your cf file:和你的 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