Json云形成模板格式错误

Json format error in cloud formation template

我的云形成模板出现以下错误。使用 json 和纯 yaml 时会发生这种情况。

错误

Resource handler returned message: "Invalid request provided: JSON not well-formed. at Line: 13, Column: 10 (Service: Ssm, Status Code: 400,

模板 json

AWSTemplateFormatVersion: "2010-09-09"
Description: "AWS CloudFormation Template for Response Plans"
Parameters:
  Environment:
    Type: String
  Domain:
    Type: String
  Team:
    Type: String
  NotificationARN:
    Type: AWS::SSM::Parameter::Value<String>
    Default: /sandbox06/Topics/PolicyData/arn
Resources:
  UpdateAliasResponsePlan:
    Type: AWS::SSMIncidents::ResponsePlan
    Properties:
      Actions:
        - SsmAutomation:
            RoleArn: !Ref Role
            DocumentName: UpdateAliasDocument
            # ActionType: UpdateAlias
      DisplayName: "UpdateLambdaAlias"
      # Engagements:
      #   Engagements
      IncidentTemplate:
        Impact: 3
        NotificationTargets:
          - SnsTopicArn:
              Ref: NotificationARN
        Summary: "String"
        Title: "String"
      Name: "UpdateLambdaAlias"
      Tags:
        - Key: "Team"
          Value: !Ref Team
        - Key: "Domain"
          Value: !Ref Domain
        - Key: "Environment"
          Value: !Ref Environment
  UpdateAliasDocument:
    Type: AWS::SSM::Document
    Properties:
      Content: |
        {
          "schemaVersion": "2.2",
          "parameters": {
            "Environment": { "type": "string"},
            "Domain": { "type": "string"},
            "Team": { "type": "string"},
            "NotificationARN": { "type": "string", "default": "/sandbox06/Topics/PolicyData/arn"}
          },
          "mainSteps": [
            { "action": "aws:runShellScript",
              "name": "runCommands",
              "inputs": {
                "runCommand": ["aws lambda update-functionconfiguration --function-name $FunctionArn --version $FunctionVersion"]
            }
          ]
        }
  Role:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - lambda.amazonaws.com
            Action: sts:AssumeRole
      Path: /
      Policies:
        - PolicyName: UpdateAliasPolicy
          PolicyDocument:
            Statement:
              - Effect: Allow
                Action:
                  - lambda:UpdateFunctionConfiguration
                Resource:
                  - !Sub arn:${AWS::Partition}:lambda:${AWS::Region}:${AWS::AccountId}:function:${Environment}-*

带有 yaml 的模板

AWSTemplateFormatVersion: "2010-09-09"
Description: "AWS CloudFormation Template for Response Plans"
Parameters:
  Environment:
    Type: String
  Domain:
    Type: String
  Team:
    Type: String
  NotificationARN:
    Type: AWS::SSM::Parameter::Value<String>
    Default: /sandbox06/Topics/PolicyData/arn
Resources:
  UpdateAliasResponsePlan:
    Type: AWS::SSMIncidents::ResponsePlan
    Properties:
      Actions:
        - SsmAutomation:
            RoleArn: !Ref Role
            DocumentName: UpdateAliasDocument
            # ActionType: UpdateAlias
      DisplayName: "UpdateLambdaAlias"
      # Engagements:
      #   Engagements
      IncidentTemplate:
        Impact: 3
        NotificationTargets:
          - SnsTopicArn:
              Ref: NotificationARN
        Summary: "String"
        Title: "String"
      Name: "UpdateLambdaAlias"
      Tags:
        - Key: "Team"
          Value: !Ref Team
        - Key: "Domain"
          Value: !Ref Domain
        - Key: "Environment"
          Value: !Ref Environment
  UpdateAliasDocument:
    Type: AWS::SSM::Document
    Properties:
      Content:
        schemaVersion: "2.2"
        parameters:
          - name: FunctionVersion
            type: "String"
            defaultValue: "1"
          - name: FunctionArn
            type: "String"
        mainSteps:
          - action: aws:runShellScript
            name: "runCommand"
            inputs:
              runCommand: "aws lambda update-function-configuration --function-name $FunctionArn --version $FunctionVersion"
  Role:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - lambda.amazonaws.com
            Action: sts:AssumeRole
      Path: /
      Policies:
        - PolicyName: UpdateAliasPolicy
          PolicyDocument:
            Statement:
              - Effect: Allow
                Action:
                  - lambda:UpdateFunctionConfiguration
                Resource:
                  - !Sub arn:${AWS::Partition}:lambda:${AWS::Region}:${AWS::AccountId}:function:${Environment}-*

另一个 YAML 版本

AWSTemplateFormatVersion: "2010-09-09"
Description: "AWS CloudFormation Template for Response Plans"
Parameters:
  Environment:
    Type: String
  Domain:
    Type: String
  Team:
    Type: String
  NotificationARN:
    Type: AWS::SSM::Parameter::Value<String>
    Default: /sandbox06/Topics/PolicyData/arn
Resources:
  UpdateAliasResponsePlan:
    Type: AWS::SSMIncidents::ResponsePlan
    Properties:
      Actions:
        - SsmAutomation:
            RoleArn: !Ref Role
            DocumentName: UpdateAliasDocument
            # ActionType: UpdateAlias
      DisplayName: "UpdateLambdaAlias"
      # Engagements:
      #   Engagements
      IncidentTemplate:
        Impact: 3
        NotificationTargets:
          - SnsTopicArn:
              Ref: NotificationARN
        Summary: "String"
        Title: "String"
      Name: "UpdateLambdaAlias"
      Tags:
        - Key: "Team"
          Value: !Ref Team
        - Key: "Domain"
          Value: !Ref Domain
        - Key: "Environment"
          Value: !Ref Environment
  UpdateAliasDocument:
    Type: AWS::SSM::Document
    Properties:
      Content:
        schemaVersion: "2.2"
        parameters:
          - name: FunctionVersion
            type: "String"
            defaultValue: "1"
          - name: FunctionName
            type: "String"
        mainSteps:
          - name: UpdateLambdaAlias
            action: aws:executeAWSApi
            inputs:
              Service: "lambda"
              Api: UpdateFunctionConfiguration
              FunctionName: $FunctionName
              FunctionVersion: $FunctionVersion
  Role:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - lambda.amazonaws.com
            Action: sts:AssumeRole
      Path: /
      Policies:
        - PolicyName: UpdateAliasPolicy
          PolicyDocument:
            Statement:
              - Effect: Allow
                Action:
                  - lambda:UpdateFunctionConfiguration
                Resource:
                  - !Sub arn:${AWS::Partition}:lambda:${AWS::Region}:${AWS::AccountId}:function:${Environment}-*

您在尝试解析 SSM 参数时遇到错误。是400错误,可能是你没有权限从SSM获取参数。在本例中,它正在寻找 /sandbox06/Topics/PolicyData/arn,因此请验证您用于创建堆栈的帐户是否有权检索该参数。 This article 显示所需的权限。

如果是这样,还要验证如果将 SSM 中的该参数值粘贴到模板中,该参数值是否会生成有效模板。验证 SSM parameterString 类型,因为 AWS::SSM::Parameter::Value<String>

A Systems Manager parameter whose value is a string. This corresponds to the String parameter type in Parameter Store.

link 还提到了以下内容并提供了一个替代方法,如果你想获取安全字符串:

AWS CloudFormation does not support defining template parameters as SecureString Systems Manager parameter types.

另外,可能需要格式化默认不以斜杠开头。 This page 显示了一个不以斜杠开头的示例,或者对于以斜杠开头的分层参数,它可能需要用单引号引起来(示例 2 以这种方式显示)

问题出在我定义参数的方式上。我需要删除名称键。

替换

 UpdateAliasDocument:
    Type: AWS::SSM::Document
    Properties:
      Content:
        schemaVersion: "2.2"
        parameters:
          - name: FunctionVersion
            type: "String"
            defaultValue: "1"
          - name: FunctionName
            type: "String"
        mainSteps:
          - name: UpdateLambdaAlias
            action: aws:executeAWSApi
            inputs:
              Service: "lambda"
              Api: UpdateFunctionConfiguration
              FunctionName: $FunctionName
              FunctionVersion: $FunctionVersion

 UpdateAliasDocument:
    Type: AWS::SSM::Document
    Properties:
      Content:
        schemaVersion: "2.2"
        parameters:
          FunctionVersion
            type: "String"
            defaultValue: "1"
          FunctionName
            type: "String"
        mainSteps:
          - name: UpdateLambdaAlias
            action: aws:executeAWSApi
            inputs:
              Service: "lambda"
              Api: UpdateFunctionConfiguration
              FunctionName: $FunctionName
              FunctionVersion: $FunctionVersion