如何解决 SAM 模板中的“转换 AWS::Include 失败:指定的 S3 对象的内容应该是有效的 Yaml/JSON”

Je *_*ick 6 yaml amazon-web-services aws-sam-cli aws-sam

我正在尝试将 OpenApi 规范包含在我的代码中,AWS::Serverless::Api DefinitionBody如下所示:

  MyApi:
    Type: "AWS::Serverless::Api"
    Properties:
      StageName: 'dev'
      Domain:
        DomainName: 'mydomain.com'
        CertificateArn: 'my-arn'
        EndpointConfiguration: REGIONAL
        Route53:
          HostedZoneId: 'HOSTEDZONEID'
        BasePath:
          - /api
      DefinitionBody:
        'Fn::Transform':
          Name: 'AWS::Include'
          Parameters:
            Location: !Sub 'open-api.yml'
Run Code Online (Sandbox Code Playgroud)

我曾经Fn:Transform确保我的速记符号得到评估。我正在使用一些 AWS API Gateway 扩展,如下所示open-api.yml

        ...
        x-amazon-apigateway-integration:
            uri: !Sub 'arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${FunctionName}/invocations'
            responses:
                default:
                    statusCode: "200"
            passthroughBehavior: "when_no_templates"
            httpMethod: "GET"
            type: "aws_proxy"
Run Code Online (Sandbox Code Playgroud)

当我运行时,sam deploy --debug出现以下错误:

转换 AWS::Include 失败:指定的 S3 对象的内容应该是有效的 Yaml/JSON

Je *_*ick 10

您收到此错误是因为您在 YAML 片段中使用速记符号。AWS::Include在撰写本文时,不支持通过转换包含的 YAML 片段的简写符号。

这意味着,而不是这个:

uri: !Sub 'arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${FunctionName}/invocations'
Run Code Online (Sandbox Code Playgroud)

你必须这样做:

uri:
  Fn::Sub:
     - 'arn:aws:apigateway:${AWSRegion}:lambda:path/2015-03-31/functions/${FunctionArn}/invocations'
     - AWSRegion:
           Ref: AWS::Region
       AWSAccountId:
           Ref: AWS::AccountId
       FunctionArn:
           Fn::GetAtt: [UserServicesFunction, Arn]
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅AWS::Include 转换