CloudFormation中CommaDelimitedList参数无法使用AllowedPattern
问题分析与解决
原因推测
根据你的描述,问题出在CloudFormation对CommaDelimitedList类型参数的AllowedPattern验证逻辑上。虽然官方文档说明该模式会匹配列表中的每个值,但实际通过CLI传参时,可能存在两种情况导致验证失败:
- 正则匹配范围问题:CloudFormation要求
AllowedPattern必须完全匹配每个列表项,你的'\w+'未添加锚定符(^和$),部分场景下可能因隐式的完全匹配要求导致验证不通过。 - CLI参数解析异常:在某些Shell环境下,
CommaDelimitedList参数可能未被正确拆分为列表项,CloudFormation会用AllowedPattern去匹配整个传入的字符串(比如dev,test),而'\w+'无法匹配包含逗号的字符串,因此报错。
解决方案
方案1:修正正则表达式(推荐)
给AllowedPattern添加锚定符,确保每个列表项完全匹配单词字符:
Profiles: Description: The comma-separated list of active profiles, such as "dev,test". Type: CommaDelimitedList Default: "" AllowedPattern: '^\w+$' ConstraintDescription: Active profile identifiers must only use word characters, separated by commas.
注意YAML中的正则写法:
- 使用单引号时,直接写
'^\w+$'即可(单引号不会转义反斜杠); - 如果用双引号,需要转义反斜杠:
"^\\w+$"。
方案2:改用String类型参数
如果方案1无法解决问题,可将参数类型改为String,同时调整AllowedPattern匹配整个逗号分隔的字符串,在模板中手动拆分:
Profiles: Description: The comma-separated list of active profiles, such as "dev,test". Type: String Default: "" AllowedPattern: '^\w+(,\w+)*$' ConstraintDescription: Active profile identifiers must only use word characters, separated by commas.
在模板中使用该参数时,通过!Split函数拆分:
# 示例:将参数拆分为列表后使用 YourResource: Type: AWS::SomeService::Resource Properties: ProfilesList: !Split [ !Ref Profiles, "," ]
方案3:更新AWS CLI版本
部分旧版本AWS CLI可能存在参数解析bug,尝试更新到最新版本:
# 升级AWS CLI v2 aws --version curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" unzip awscliv2.zip sudo ./aws/install --update
内容的提问来源于stack exchange,提问作者Garret Wilson
相关产品推荐
相关产品推荐

