CommaDelimitedList 파라미터를 AWS CloudFormation의 중첩 스택으로 전달하려고 합니다.
간략한 설명
CommaDelimitedList 유형의 값은 중첩 스택에 전달할 수 없습니다. 대신 상위 스택에서 Fn:: Join 내장 함수를 사용하여 CommaDelimitedList 형식을 String 형식으로 변환합니다.
해결 방법
다음 예제에서는 상위 스택에서 중첩 스택으로 SecurityGroupIds 목록을 전달하는 방법을 보여줍니다.
1. 상위 스택의 JSON 또는 YAML 파일을 연 다음, [SecurityGroupIds]의 [Type]을 [CommaDelimitedList]로 설정합니다.
JSON 파일의 [Resources] 섹션에서 [Fn:: Join] 함수는 결합된 문자열을 반환합니다. YAML 파일의 [Resources] 섹션에서 [!Join] 함수는 결합된 문자열을 반환합니다. JSON 및 YAML 파일 모두에서 결합된 문자열은 [SecurityGroupIds] 파라미터 유형을 [CommaDelimitedList]에서 [String]으로 변환합니다.
상위 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-aaa, subnet-bbb"]**와 같은 두 개의 서브넷을 전달하면 [Fn:: Join]의 출력은 **{"subnet-aaaa, subnet-bbb"}**입니다.
2. 중첩 스택의 JSON 또는 YAML 파일에서 [SecurityGroupIds]의 [Type]을 [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