代码之家  ›  专栏  ›  技术社区  ›  Je Suis Alrick

如何解决SAM模板中的“Transform AWS::Include失败:位置参数不是有效的S3 uri”

  •  0
  • Je Suis Alrick  · 技术社区  · 5 年前

    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

            ...
            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 我得到以下错误:

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

    1 回复  |  直到 5 年前
        1
  •  2
  •   Je Suis Alrick    5 年前

    出现此错误是因为在YAML代码段中使用速记符号。包含的YAML代码段的速记符号 AWS::Include 撰写本文时不支持转换。

    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