question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support swagger file to serve APIs (external & internal)

See original GitHub issue

I asked this on Stackoverflow too but not much of a response. I think aws-sam-local is still a little new.

I’ve got a simple project. The basic setup is like this…

project
 |- etc
 |   |- sam.yaml
 |   \- swagger.yaml
 |- src
 |   |- client
 |   |   \- Client files (VUE frontend)
 |   \- backend
 |       |- index.js
 |       \- package.json
 \- gulpfile.js

I run gulp and I end up with a build folder. It looks something like this…

build
 |- client
 |   |- index.html
 |   \- More static client files
 |- backend
 |   |- index.js
 |   |- package.json
 |   \_ node_modules
 |       \- Lots of stuff
 \- backend.zip

The build works great. I want to run it locally rather than uploading the ZIP to lambda and the client directory to S3 every time I make a change.

To do this I’m running aws-sam-local. Yes I have docker installed and running. From the project folder I run this…

sam local start-api -t etc/sam.yaml -s ../build/client/

Now I can go to my browser and visit http://localhost:3000/ to see things. As expected my static files (index.html) load up just fine. But when I try to access an API route http://localhost:3000/test I get a 404.

My sam.yaml

---
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: AWS SAM template with API defined in an external Swagger file along with Lambda integrations and CORS configurations
Resources:
  ApiGatewayApi:
    Type: AWS::Serverless::Api
    Properties:
      DefinitionUri: ../etc/swagger.yaml
      StageName: Prod
      Variables:
        LambdaFunctionName: !Ref LambdaFunction

  LambdaFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: ../build/backend/
      Handler: index.handler
      Runtime: nodejs6.10
      Events:
        ProxyApiRoot:
          Type: Api
          Properties:
            RestApiId: !Ref ApiGatewayApi
            Path: /
            Method: ANY
        ProxyApiGreedy:
          Type: Api
          Properties:
            RestApiId: !Ref ApiGatewayApi
            Path: /{proxy+}
            Method: ANY

Outputs:
  ApiUrl:
    Description: URL of your API endpoint
    Value: !Join
      - ''
      - - https://
        - !Ref ApiGatewayApi
        - '.execute-api.'
        - !Ref 'AWS::Region'
        - '.amazonaws.com/Prod'

My swagger.yaml

---
swagger: 2.0
basePath: /prod
info:
  title: HACC
schemes:
- https
paths:
  /:
    x-amazon-apigateway-any-method:
      produces:
      - application/json
      responses:
        200:
          description: 200 response
          schema:
            $ref: "#/definitions/Empty"
      x-amazon-apigateway-integration:
        responses:
          default:
            statusCode: 200
        uri: arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:<<accountId>>:function:${stageVariables.LambdaFunctionName}/invocations

        passthroughBehavior: when_no_match
        httpMethod: POST
        type: aws_proxy
    options:
      consumes:
      - application/json
      produces:
      - application/json
      responses:
        200:
          description: 200 response
          schema:
            $ref: "#/definitions/Empty"
          headers:
            Access-Control-Allow-Origin:
              type: string
            Access-Control-Allow-Methods:
              type: string
            Access-Control-Allow-Headers:
              type: string
      x-amazon-apigateway-integration:
        responses:
          default:
            statusCode: 200
            responseParameters:
              method.response.header.Access-Control-Allow-Methods: "'DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT'"
              method.response.header.Access-Control-Allow-Headers: "'Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token'"
              method.response.header.Access-Control-Allow-Origin: "'*'"
        passthroughBehavior: when_no_match
        requestTemplates:
          application/json: "{\"statusCode\": 200}"
        type: mock
  /{proxy+}:
    x-amazon-apigateway-any-method:
      x-amazon-apigateway-auth:
        type: aws_iam
      produces:
      - application/json
      parameters:
      - name: proxy
        in: path
        required: true
        type: string
      responses: {}
      x-amazon-apigateway-integration:
        uri: arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:<<accountId>>:function:${stageVariables.LambdaFunctionName}/invocations
        httpMethod: POST
        type: aws_proxy
    options:
      consumes:
      - application/json
      produces:
      - application/json
      responses:
        200:
          description: 200 response
          schema:
            $ref: "#/definitions/Empty"
          headers:
            Access-Control-Allow-Origin:
              type: string
            Access-Control-Allow-Methods:
              type: string
            Access-Control-Allow-Headers:
              type: string
      x-amazon-apigateway-integration:
        responses:
          default:
            statusCode: 200
            responseParameters:
              method.response.header.Access-Control-Allow-Methods: "'DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT'"
              method.response.header.Access-Control-Allow-Headers: "'Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token'"
              method.response.header.Access-Control-Allow-Origin: "'*'"
        passthroughBehavior: when_no_match
        requestTemplates:
          application/json: "{\"statusCode\": 200}"
        type: mock
definitions:
  Empty:
    type: object
    title: Empty Schema

How can I get aws-sam-local to run my lambda API?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:5
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
sapessicommented, Dec 7, 2017

We’ve just merged #222 - this adds support for Swagger definitions to load mounts in the router. I’ve tested this with inline Swagger templates.

0reactions
PaulMaddoxcommented, Dec 7, 2017

I’ve just released v0.2.3 to npm which includes this. You can use npm update -g aws-sam-local to update.

Read more comments on GitHub >

github_iconTop Results From Across the Web

API Documentation Made Easy with OpenAPI & Swagger
How to generate OpenAPI from existing APIs ... Head over to Swagger Inspector, and insert the end point of the resource you want...
Read more >
Support swagger file to serve APIs (external & internal) #101
From the project folder I run this... sam local start-api -t etc/sam.yaml -s ../build/client/.
Read more >
Integrating Swagger UI with the rest of your docs
When you start pushing your API documentation into another source file — in this case, a YAML or JSON file that is included...
Read more >
Add Swagger to Your Existing APIs - YouTube
How to add Swagger to Spring Boot - Brain Bytes · Adopting a Design First Approach with OAS 3.0 & Swagger · Migrate...
Read more >
ReadMe: OpenAPI and Swagger for API Documentation
Swagger and OpenAPI are API description formats that provide a common framework for teams to build and document APIs. These machine-readable ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found