Trying to get aws-sam-local to serve up my API

2.2k views Asked by At

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?

0

There are 0 answers