CommaDelimitedList パラメータを AWS CloudFormation のネストされたスタックに渡したいと考えています。
簡単な説明
ネストされたスタックに CommaDelimitedList タイプの値を渡すことはできません。代わりに、親スタックで Fn::Join 組み込み関数を使用して、CommaDelimitedList タイプを String タイプに変換します。
解決方法
次の例では、親スタックからネストされたスタックに SecurityGroupIds のリストを渡す方法を示します。
1. 親スタックの JSON または YAML ファイルを開き、SecurityGroupIds の [Type] (タイプ) を CommaDelimitedList に設定します。
JSON ファイルのリソースセクションで、Fn::Join 関数は結合された文字列を返します。YAML ファイルのリソースセクションで、!Join 関数は結合された文字列を返します。JSON ファイルと YAML ファイルの両方で、結合された文字列は SecurityGroupIds パラメータータイプを CommaDelimitedList から文字列に変換します。
親 JSON ファイルの例:
{
"AWSTemplateFormatVersion": "2010-09-09",
"Parameters": {
"SubnetId": {
"Type": "AWS::EC2::Subnet::Id"
},
"SecurityGroupIds": {
"Type": "List<AWS::EC2::SecurityGroup::Id>"
},
"KeyName": {
"Type": "AWS::EC2::KeyPair::KeyName"
},
"ImageId": {
"Type": "String"
}
},
"Resources": {
"Instance": {
"Type": "AWS::CloudFormation::Stack",
"Properties": {
"TemplateURL": "https://s3.amazonaws.com/cloudformation-templates-us-east-2/nested.yml",
"Parameters": {
"SubnetId": {
"Ref": "SubnetId"
},
"SecurityGroupIds": {
"Fn::Join": [
",",
{
"Ref": "SecurityGroupIds"
}
]
},
"KeyName": {
"Ref": "KeyName"
},
"ImageId": {
"Ref": "ImageId"
}
}
}
}
}
}
親 YAML ファイルの例:
AWSTemplateFormatVersion: 2010-09-09
Parameters:
SubnetId:
Type: 'AWS::EC2::Subnet::Id'
SecurityGroupIds:
Type: 'List<AWS::EC2::SecurityGroup::Id>'
KeyName:
Type: 'AWS::EC2::KeyPair::KeyName'
ImageId:
Type: String
Resources:
Instance:
Type: 'AWS::CloudFormation::Stack'
Properties:
TemplateURL: 'https://s3.amazonaws.com/cloudformation-templates-us-east-2/nested.yml'
Parameters:
SubnetId: !Ref SubnetId
SecurityGroupIds: !Join
- ','
- !Ref SecurityGroupIds
KeyName: !Ref KeyName
ImageId: !Ref ImageId
注意: ["subnet-aaaa, subnet-bbbb"] などの 2 つのサブネットを渡した場合、Fn::Join の出力は {"subnet-aaaa, subnet-bbbb"} になります。
2. ネストされたスタックの JSON または YAML ファイルで、SecurityGroupIds のタイプを CommaDelimitedList に設定します。
ネストされた JSON ファイルの例:
{
"AWSTemplateFormatVersion": "2010-09-09",
"Parameters": {
"SubnetId": {
"Type": "String"
},
"SecurityGroupIds": {
"Type": "CommaDelimitedList"
},
"KeyName": {
"Type": "String"
},
"ImageId": {
"Type": "String"
}
},
"Resources": {
"Ec2instance": {
"Type": "AWS::EC2::Instance",
"Properties": {
"ImageId": {
"Ref": "ImageId"
},
"KeyName": {
"Ref": "KeyName"
},
"SecurityGroupIds": {
"Ref": "SecurityGroupIds"
},
"SubnetId": {
"Ref": "SubnetId"
}
}
}
}
}
ネストされた YAML ファイルの例:
AWSTemplateFormatVersion: 2010-09-09
Parameters:
SubnetId:
Type: String
SecurityGroupIds:
Type: CommaDelimitedList
KeyName:
Type: String
ImageId:
Type: String
Resources:
Ec2instance:
Type: 'AWS::EC2::Instance'
Properties:
ImageId: !Ref ImageId
KeyName: !Ref KeyName
SecurityGroupIds: !Ref SecurityGroupIds
SubnetId: !Ref SubnetId
注: ネストされたスタックでは、親スタックから結合された文字列が CommaDelimitedList として SecurityGroupIds に渡されます。例えば、値 {"sg-aaaaa, sg-bbbbb"} は ["sg-aaaaa", "sg-bbbbb"] に変換されます。したがって、SecurityGroupIds は、文字列のリストとしてではなく、SecurityGroupIds: !Ref SecurityGroupIds によって直接参照される必要があります。
関連情報
ネストされたスタックの使用
AWS::CloudFormation::Stack