CloudFormation向SSM文档传递多参数触发Parameters类型校验报错
报错成因分析
- 直接触发校验失败的核心问题:
AWS::SSM::Association资源的Parameters属性格式不符合CloudFormation规范- 该字段要求传入JSONObject(键值对映射结构),每个映射的键为SSM文档中定义的参数名,值为字符串数组形式的参数实际值。
- 当前模板采用数组(JSONArray)结构承载参数,每一项为包含
Key、Value字段的独立对象,与要求的结构完全不匹配,因此触发expected type: JSONObject, found: JSONArray的校验错误。
- 其他会导致后续部署失败的结构问题:
- SSM Document层级结构错误:
mainSteps是SSM文档内容的核心组成模块,应当作为Content属性的子节点,当前写法将mainSteps与Content同级放置,会导致SSM文档格式校验失败。 - PowerShell脚本块误用HTML实体转义:
"、>是网页场景的转义字符,在CloudFormation模板的脚本字符串中需要直接使用原生双引号"、大于号>,否则脚本执行时会触发语法错误。 - 模板引用了未定义的参数
Node1、Node2,未在全局Parameters块中声明,会触发未定义引用的校验错误。
- SSM Document层级结构错误:
正确实现方案
修正所有格式与结构问题后的可运行模板如下:
AWSTemplateFormatVersion: 2010-09-09 Parameters: EnvNames: Description: List of parameters Type: CommaDelimitedList Node1: Type: String Default: "target-node-1" Node2: Type: String Default: "target-node-2" Resources: Document: Type: AWS::SSM::Document Properties: DocumentType: Command Content: schemaVersion: '2.2' description: Run a PowerShell script parameters: EnvName1: type: String EnvName2: type: String # 修正mainSteps缩进,使其成为Content的子节点 mainSteps: - action: aws:runPowerShellScript name: devDocument precondition: StringEquals: - platformType - Windows inputs: runCommand: - !Sub | # Retrieve values from parameters $EnvName1 = "{{EnvName1}}" $EnvName2 = "{{EnvName2}}" New-Item -ItemType directory -Path C:\temp -Force $EnvName1 > C:\temp\EnvName1.txt $EnvName2 > C:\temp\EnvName2.txt SSM: Type: AWS::SSM::Association Properties: Name: !Ref Document # 修正Parameters为JSONObject键值对结构 Parameters: EnvName1: - !Select [0, !Ref EnvNames] EnvName2: - !Select [1, !Ref EnvNames] Targets: - Key: tag:id Values: - !Ref Node1 - !Ref Node2
修正说明:
- 移除了SSM Association参数中错误的数组式Key/Value封装,直接按
参数名: [参数值数组]的要求构造JSONObject结构,匹配校验规则。 - 调整
mainSteps的缩进层级,使其正确归属到SSM文档的Content结构下。 - 替换脚本中的HTML转义字符为原生语法,同时为目录创建命令添加
-Force参数避免目录已存在时的执行报错。 - 补充缺失的
Node1、Node2全局参数定义,消除未定义引用错误。
内容的提问来源于stack exchange,提问作者Orest Gulman
相关产品推荐
相关产品推荐

