4
votes

I am defining my API Gateway APIs using AWS SAM

ApiGatewayApi:
  Type: AWS::Serverless::Api
  Properties:
    DefinitionUri: swagger.yml
    StageName: prod
    Variables:
      Region: !Ref AWS::Region
      AccountId: !Ref AWS::AccountId
      Ec2Index: !Ref Ec2Index
      AuthLogin: !Ref AuthLogin
      Ec2Patch: !Ref Ec2Patch
      AutoScalingIndex: !Ref AutoScalingIndex
      AutoScalingPatch: !Ref AutoScalingPatch
      AutoScalingScale: !Ref AutoScalingScale

In my swagger file:

paths: /auth/session: post: produces: - application/json x-amazon-apigateway-integration: uri: arn:aws:apigateway:ap-southeast-1:lambda:path/2015-03-31/functions/arn:aws:lambda:ap-southeast-1:598545985414:function:${stageVariables.AuthLogin}/invocations passthroughBehavior: when_no_match httpMethod: POST type: aws_proxy responses: 200: description: App token 401: description: 401 403: description: 403

AWS CloudFormation errors saying

Errors found during import: Unable to put integration on 'POST' for resource at path '/auth/session': Lambda function ARN must be in same account Unable to put integration on 'GET' for resource at path '/autoscaling': Lambda function ARN must be in same account Unable to put integration on 'PATCH' for resource at path '/autoscaling/{groupName}': Lambda function ARN must be in same account Unable to put integration on 'POST' for resource at path '/autoscaling/{groupName}/scale': Lambda function ARN must be in same account Unable to put integration on 'GET' for resource at path '/ec2': Lambda function ARN must be in same account Unable to put integration on 'PATCH' for resource at path '/ec2/{id}': Lambda function ARN must be in same account

Seems like my ARN is invalid. This is resolved once I remove the variables. Whats wrong here?

2

2 Answers

2
votes

As of now swagger does not allow AccountId in stage variables. It's a limitation of the API Gateway.

You can get around this by just using a stage variable for the function name only and piece the rest together like this:

//does not get passed in. This is just a placeholder for the stage variable
Parameters: 
    ApiFunctionName:
        Type: String
        Description: Function name of the api lambda function
        Default: ${stageVariables.yourFunctionNameVar}

//in your gateway path   
    x-amazon-apigateway-integration:
        httpMethod: POST
        type: aws_proxy
        uri: 
          !Sub "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${ApiFunctionName}/invocations"
0
votes

Passing variables to swagger is not possible to do at this moment from a SAM template, but you can copy entire swagger file in the template file as DefinitionBody and reference variables the same way provided swagger definition is not huge.

SAM template is currently limited to 51.2 KB