amazon-web-services - 如何解决 SAM 模板中 "Transform AWS::Include failed with: The specified S3 object' 的内容应该是有效的 Yaml/JSON"

标签 amazon-web-services yaml aws-sam aws-sam-cli

我正在尝试将 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'

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

        ...
        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"

当我运行 sam deploy --debug 时,我收到以下错误:

Transform AWS::Include failed with: The specified S3 object's content should be valid Yaml/JSON

最佳答案

您收到此错误是因为您在 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'

你必须这样做:

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]

AWS::Include transform了解更多信息。

关于amazon-web-services - 如何解决 SAM 模板中 "Transform AWS::Include failed with: The specified S3 object' 的内容应该是有效的 Yaml/JSON",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67178860/

相关文章:

java - 如何在亚马逊 EC2 中启用 mod_proxy?

amazon-web-services - AWS Lambda 函数需要的内存超过 CloudWatch 中报告的最大内存

postgresql - 我们可以将AWS RDS只读副本视为备份吗?

aws-lambda - 如何引用 SAM 模板中定义的角色?

amazon-web-services - CFN 更新后如何自动终止 EC2 实例?

yaml - 如何调试 Helm 图表错误,如 "error converting YAML to JSON: yaml: mapping values are not allowed in this context"?

python - 在 yaml 文件中添加多个文档 | PyYAML

parsing - 如何在 Go 中解码和编码 YAML,同时保留空白?

amazon-web-services - 如何在 AWS SAM Cloud-Formation 中使用 Route53 设置自定义域名

aws-lambda - lambda (EventSource MQTT) 的 SAM 部署失败,参数 VIRTUAL_HOST 无效