云形成模板中的 Json 格式错误 错误带有 json 的模板带有 yaml 的模板另一个 YAML 版本替换与

问题描述

我的云形成模板出现以下错误。使用 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>

值为字符串的 Systems Manager 参数。这对应 到 Parameter Store 中的 String 参数类型。

该链接还提到了以下内容,如果您想获取安全字符串,还提供了替代方法:

AWS CloudFormation 不支持将模板参数定义为 SecureString Systems Manager 参数类型。

另外,您可能需要将默认格式设置为不以斜杠开头。 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