AllowedPattern 参数行为显然无法匹配简单的正则表达式

AllowedPattern parameter behaviour apparently failing to match a simple regexp

在 Cloudformation 中,我使用简单的 AllowedPattern:

验证参数输入
"ServicePassword": {
  "Description":           "Password for the AD service account",
  "Type":                  "String",
  "AllowedPattern":        "^."
},

variables.json 文件包含一行(已编辑密码):

{"ParameterKey": "ServicePassword",  "ParameterValue": "E_redacted"},

(第一个字母大写 E;字符串的其余部分已编辑。)

调用 Cloudformation 时:

$ aws cloudformation create-stack --stack-name bastion_redacted --template-body file://Bastion.json --parameters file://variables.json --capabilities CAPABILITY_IAM --disable-rollback
An error occurred (ValidationError) when calling the CreateStack operation: Parameter 'ServicePassword' must match pattern ^.

我使用更简单的正则表达式看到了同样的问题,它只是一个字符串 - "AllowedPattern": "hello"

如果我只是从模板中删除 AllowedPattern 行,它就可以正常工作。

这是一个错误吗?我做错了什么吗?

CloudFormation regexes use java.util.regex.Pattern for their syntax and behavior, so you can look to Java's documentation for a reference. The AllowedPattern 参数 属性 要求模式匹配 整个 输入字符串(不仅仅是部分),否则它将拒绝输入。

模式 ^ 匹配一行的开头,. 匹配任何 单个 字符,因此您现有的正则表达式将匹配 x0。要匹配多个字符,您需要添加一个 "greedy quantifier",例如 * 将匹配任意数量的字符,或 + 将匹配一个或多个字符。 (由于 AllowedPattern 无论如何都匹配整个字符串,因此不需要 ^,因此可以删除。)

像这样的东西应该作为一个简单的正则表达式来匹配任何类型的任意数量的字符:

"ServicePassword": {
  "Description":           "Password for the AD service account",
  "Type":                  "String",
  "AllowedPattern":        ".*"
},

或者您可以使用 ".+" 作为一个同样简单的正则表达式,匹配任何类型的一个或多个字符。