我想要使用個別參數的多個值,從 AWS CloudFormation 範本建立或更新堆疊。
簡短描述
可以使用下列其中一種方式,在 AWS CloudFormation 範本中為個別參數傳遞多個值:
解決方法
使用 AWS 特定的參數類型,從 AWS 帳戶中預先填入的現有 AWS 值清單中選取值
**重要事項:**AWS CloudFormation 會根據帳戶中的現有值驗證您選取的輸入值。
在這些 AWS CloudFormation 範本範例中,具有 SecurityGroups 金鑰的參數可指定 AWS 特定的參數類型,該參數類型可接受 SecurityGroupIds 的多個值。
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
若要使用 AWS CLI 部署堆疊,請使用下列命令:
**注意:**用堆疊的名稱取代 StackName。用檔案的名稱取代 TemplateFileName。對於 ParameterValue,請輸入安全群組 ID。
aws cloudformation create-stack --stack-name StackName --template-body file://TemplateFileName
--parameters ParameterKey=SecurityGroups,ParameterValue="sg-0123456789\,sg-2345678901"
使用 CommaDelimitedList 參數類型來輸入輸入值
在下列 AWS CloudFormation 範本範例中,具有 SecurityGroups 金鑰的參數可指定 CommaDelimitedList 類型,該類型可接受 SecurityGroupIds 的多個值。
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