我想要使用個別參數的多個值,從 AWS CloudFormation 範本建立或更新堆疊。
解決方法
**注意:**如果您在執行 AWS Command Line Interface (AWS CLI) 命令時收到錯誤訊息,請參閱對 AWS CLI 錯誤進行疑難排解。此外,請確定您使用的是最新的 AWS CLI 版本。
若要為 CloudFormation 範本中的個別參數使用多個值,請執行下列其中一項動作:
使用特定 AWS 的參數類型
**注意:**CloudFormation 會根據您帳戶中的現有值,確認您選取的輸入值。
在下列 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
若要部署堆疊,請執行 create-stack AWS CLI 命令:
aws cloudformation create-stack --stack-name StackName --template-body file://TemplateFileName
--parameters ParameterKey=SecurityGroups,ParameterValue="sg-0123456789\,sg-2345678901"
**注意:**將 StackName 替換為您的堆疊名稱,並將 TemplateFileName 替換為您的檔案名稱。在 ParameterValue 中,請輸入安全群組 ID。
使用 CommaDelimitedList 參數類型
在下列 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