简体   繁体   中英

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

I have a CloudFormation template that defines this parameter:

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

I invoke the CF template like this:

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.

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.

Any ideas please?

I would prefer to use the templatefile function instead of the file you are using in the template_body field. 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

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM