How to solve "Transform AWS::Include failed with: The specified S3 object's content should be valid Yaml/JSON" in SAM template
Asked Answered
P

1

7

I'm trying to include an OpenApi specification into my AWS::Serverless::Api DefinitionBody like so:

  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'

I used Fn:Transform to ensure that my shorthand notation is evaluated. I'm using some AWS API Gateway extensions in my open-api.yml like below:

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

When I run sam deploy --debug I get the following error:

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

Pelletier answered 20/4, 2021 at 12:23 Comment(0)
P
10

You are getting this error because you are using shorthand notation in YAML snippets. Shorthand notations for YAML snippets included via AWS::Include transform is not support at the time of this writing.

That means, instead of this:

uri: !Sub 'arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${FunctionName}/invocations'

you have to do this instead:

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]

See AWS::Include transform for more info.

Pelletier answered 20/4, 2021 at 12:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.