如何将多个值用于 AWS CloudFormation 模板中的单个参数?

上次更新时间:2020 年 8 月 20 日

我想为单个参数使用多个值从 AWS CloudFormation 模板创建或更新堆栈。

简短描述

您可以通过以下方式将多个值用于 AWS CloudFormation 模板中的单个参数

解决方法

使用 AWS 特定的参数类型,从 AWS 账户的现有 AWS 值预填充列表中选择值

重要提示:AWS CloudFormation 会根据您账户中现有的值来验证您选择的输入值。

在以下示例 AWS CloudFormation 模板中,含有 SecurityGroups 键的参数指定了一个可接受将多个值用于 SecurityGroupIds 的 AWS 特定的参数类型。

JSON 模板:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Parameters": {
    "SecurityGroups": {
      "Type": "List<AWS::EC2::SecurityGroup::Id>",
      "Description": "The list of SecurityGroupIds in your Virtual Private Cloud (VPC)"
    }
  },
  "Resources": {
    "MyEC2Instance": {
      "Type": "AWS::EC2::Instance",
      "Properties": {
        "ImageId": "ami-79fd7eee",
        "KeyName": "testkey",
        "SecurityGroupIds": {
          "Ref": "SecurityGroups"
        }
      }
    }
  }
}

YAML 模板:

AWSTemplateFormatVersion: 2010-09-09
Parameters:
  SecurityGroups:
    Type: 'List<AWS::EC2::SecurityGroup::Id>'
    Description: The list of SecurityGroupIds in your Virtual Private Cloud (VPC)
Resources:
  MyEC2Instance:
    Type: 'AWS::EC2::Instance'
    Properties:
      ImageId: ami-79fd7eee
      KeyName: testkey
      SecurityGroupIds: !Ref SecurityGroups

使用 CommaDelimitedList 参数类型键入输入值

在以下示例 AWS CloudFormation 模板中,含有 SecurityGroups 键的参数指定了一个可接受将多个值用于SecurityGroupIdsCommaDelimitedList 参数类型。

JSON 模板:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Parameters": {
    "SecurityGroups": {
      "Type": "CommaDelimitedList",
      "Description": "The list of SecurityGroupIds in your Virtual Private Cloud (VPC)",
      "Default": "sg-a123fd85, sg-b456ge94"
    }
  },
  "Resources": {
    "MyEC2Instance": {
      "Type": "AWS::EC2::Instance",
      "Properties": {
        "ImageId": "ami-79fd7eee",
        "KeyName": "testkey",
        "SecurityGroupIds": {
          "Ref": "SecurityGroups"
        }
      }
    }
  }
}

YAML 模板:

AWSTemplateFormatVersion: 2010-09-09
Parameters:
  SecurityGroups:
    Type: CommaDelimitedList
    Description: The list of SecurityGroupIds in your Virtual Private Cloud (VPC)
    Default: sg-a123fd85, sg-b456ge94
Resources:
  MyEC2Instance:
    Type: 'AWS::EC2::Instance'
    Properties:
      ImageId: ami-79fd7eee
      KeyName: testkey
      SecurityGroupIds: !Ref SecurityGroups

这篇文章对您有帮助吗?


您是否需要账单或技术支持?