Giedrius
Giedrius

Reputation: 8550

AWS SAM template with API gateway only?

We're planning to use API Gateway as REST API HTTP proxy only initially - as we have existing REST service and we wish to split it into smaller parts under covers without changing client interface (possibly later replacing some of those parts with lambda functions).

Before we do that, I wish to see if we will have any support running such scenario locally, hence I've configured API gateway in AWS management console and exported OpenAPI definition.

So my template.yaml looks like

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: Demo SAM API
Resources:
  MyApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: test
      DefinitionBody:
        'Fn::Transform':
          Name: AWS::Include
          Parameters:
            Location: swagger.yaml

and swagger.yaml looks following:

openapi: "3.0.1"
info:
  title: "Test.API"
  description: "Test.API"
  version: "2020-09-14T07:49:45Z"
servers:
- url: "https://dummydomain.com"
paths:
  /api/v3/Client:
    get:
      x-amazon-apigateway-integration:
        type: "http_proxy"
        uri: "https://dummydommain.com/api/v3/client"
        passthroughBehavior: "when_no_match"
        httpMethod: "GET"
        timeoutInMillis: 29000
components: {}

And running sam local start-api --debug this gives error:

Error: Template does not have any APIs connected to Lambda functions

I've added dummy lambda function just to pass through the error, but still no luck - it had message in debug output:

Lambda function integration not found in Swagger document at path='/api/v3/client' method='get'

and responded to `http://localhost:3000/api/v3/client' with:

{"message":"Missing Authentication Token"}

Does this mean, that SAM is very very coupled to lambda's and it won't run API gateway as proxy locally or I'm doing something stupid?

Upvotes: 3

Views: 2723

Answers (2)

Galeno de Melo
Galeno de Melo

Reputation: 36

Answering your question:

Does this mean, that SAM is very very coupled to lambda's and it won't run API gateway as proxy locally or I'm doing something stupid?

No, you're not stupid. It won't run as a proxy, and the documentation isn't clear about that.

It turns out that the only integration type accepted by local API is aws_proxy (for Lambda Functions only) or mock (which does nothing).

It's on a comment in their source code

Your type: "http_proxy" might work on production, but not on localhost

Upvotes: 0

Ihor Shylo
Ihor Shylo

Reputation: 672

Add Lambda integration to your template.yaml file:

YourLambdaFunction:
        Type: AWS::Serverless::Function 
        Properties:
            CodeUri: path/to/your/lambda/
            Role: arn:aws:iam::<ACCOUNT_NUMBER>:role/service-role/role-for-lambda
            Events:
                YourGetEvent:
                    Type: Api 
                    Properties:
                        Path: /api/v3/Client
                        Method: get
                        RestApiId:
                            Ref: MyApi

Upvotes: -1

Related Questions