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.

OPTIONS endpoint does not invoke lambda code

See original GitHub issue

Description

We have implemented OPTIONS endpoints that do some CORS related processes. With the release of version 0.20.0 the OPTIONS endpoints return 200 without invoking the lambda functions.

Steps to reproduce

  1. Start the api with sam local start-api -p 3002
  2. hit the endpoint with method OPTIONS http://localhost:3002/api/hello
  3. hit the endpoint with method GET http://localhost:3002/api/hello

Observed result

Using SAM Template at C:\development\centimark\hello-simple\options\template.yaml
Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
local start-api command is called
Collected default values for parameters: {'ENV': 'dev'}
2 resources found in the template
Found Serverless function with name='HelloWorldFunction' and CodeUri='hello/'
Collected default values for parameters: {'ENV': 'dev'}
2 resources found in the template
Detected Inline Swagger definition
Lambda function integration not found in Swagger document at path='/api/hello' method='options'
Lambda function integration not found in Swagger document at path='/api/hello' method='get'
Lambda function integration not found in Swagger document at path='/api/hello/{name}' method='get'
Found '0' APIs in resource 'ServerlessApi'
Found '3' API Events in Serverless function with name 'HelloWorldFunction'
Removed duplicates from '3' Explicit APIs and '0' Implicit APIs to produce '3' APIs
2 APIs found in the template
Mounting HelloWorldFunction at http://127.0.0.1:3002/api/hello [GET, OPTIONS]
Mounting HelloWorldFunction at http://127.0.0.1:3002/api/hello/{name} [GET]
You can now browse to the above endpoints to invoke your functions. You do not need to restart/reload SAM CLI while working on your functions, changes will be reflected instantly/automatically. You only need to restart SAM CLI if you update your AWS SAM template
Localhost server is starting up. Multi-threading = True
2019-09-27 16:20:58  * Running on http://127.0.0.1:3002/ (Press CTRL+C to quit)
2019-09-27 16:21:09 127.0.0.1 - - [27/Sep/2019 16:21:09] "OPTIONS /api/hello HTTP/1.1" 200 -
Constructed String representation of Event to invoke Lambda. Event: {"httpMethod": "GET", "body": null, "resource": "/api/hello", "requestContext": {"resourceId": "123456", "apiId": "1234567890", "resourcePath": "/api/hello", "httpMethod": "GET", "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef", "accountId": "123456789012", "stage": "dev", "identity": {"apiKey": null, "userArn": null, "cognitoAuthenticationType": null, "caller": null, "userAgent": "Custom User Agent String", "user": null, "cognitoIdentityPoolId": null, "cognitoAuthenticationProvider": null, "sourceIp": "127.0.0.1", "accountId": null}, "extendedRequestId": null, "path": "/api/hello"}, "queryStringParameters": null, "multiValueQueryStringParameters": null, "headers": {"User-Agent": "PostmanRuntime/7.17.1", "Accept": "*/*", "Cache-Control": "no-cache", "Postman-Token": "9200a05a-495d-41e6-8135-bfb0a60f6be1", "Host": "localhost:3002", "Accept-Encoding": "gzip, deflate", "Connection": "keep-alive", "X-Forwarded-Proto": "http", "X-Forwarded-Port": "3002"}, "multiValueHeaders": {"User-Agent": ["PostmanRuntime/7.17.1"], "Accept": ["*/*"], "Cache-Control": ["no-cache"], "Postman-Token": ["9200a05a-495d-41e6-8135-bfb0a60f6be1"], "Host": ["localhost:3002"], "Accept-Encoding": ["gzip, deflate"], "Connection": ["keep-alive"], "X-Forwarded-Proto": ["http"], "X-Forwarded-Port": ["3002"]}, "pathParameters": null, "stageVariables": null, "path": "/api/hello", "isBase64Encoded": false}
Found one Lambda function with name 'HelloWorldFunction'
Invoking app.lambdaHandler (nodejs10.x)
Environment variables overrides data is standard format
Loading AWS credentials from session with profile 'None'
2019-09-27 16:21:31 Found credentials in shared credentials file: ~/.aws/credentials
Resolving code path. Cwd=C:\development\centimark\hello-simple\options, CodeUri=hello/
Resolved absolute path to code is C:\development\centimark\hello-simple\options\hello
Code C:\development\centimark\hello-simple\options\hello is not a zip/jar file
Skipping building an image since no layers were defined

Fetching lambci/lambda:nodejs10.x Docker container image......
Mounting C:\development\centimark\hello-simple\options\hello as /var/task:ro,delegated inside runtime container
Starting a timer for 3 seconds for function 'HelloWorldFunction'
?[32mSTART RequestId: 4b1797c8-6428-19d4-3401-e55eced3e107 Version: $LATEST?[0m
2019-09-27T20:21:33.456Z        4b1797c8-6428-19d4-3401-e55eced3e107    INFO    invoked hello world { httpMe  isBase64Encoded: false } '3002' ] },te' ],8135-bfb0a60f6be1' ],
?[32mEND RequestId: 4b1797c8-6428-19d4-3401-e55eced3e107?[0m
?[32mREPORT RequestId: 4b1797c8-6428-19d4-3401-e55eced3e107     Duration: 16.15 ms      Billed Duration: 100 ms     Memory Size: 128 MB     Max Memory Used: 42 MB  ?[0m
No Content-Type given. Defaulting to 'application/json'.
2019-09-27 16:21:34 127.0.0.1 - - [27/Sep/2019 16:21:34] "GET /api/hello HTTP/1.1" 200 -

Expected result

The expectation was that “invoked hello world” would be printed twice - once for the OPTIONS and once for the GET request.

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

  1. OS: Windows
  2. sam --version: SAM CLI, version 0.22.0

More details

Template File

Transform: AWS::Serverless-2016-10-31
Description: >
  The API gateway definition

Globals:
  Function:
    Timeout: 3

Parameters:
  ENV:
    Type: String
    Default: dev

Resources:

  ServerlessApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: !Ref ENV

  HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: hello/
      Handler: app.lambdaHandler
      Runtime: nodejs10.x
      Environment:
        Variables:
          ENV: !Ref ENV
      Events:
        HelloOptions:
          Type: Api
          Properties:
            RestApiId: !Ref ServerlessApi
            Path: /api/hello
            Method: options
        HelloWorld:
          Type: Api
          Properties:
            RestApiId: !Ref ServerlessApi
            Path: /api/hello
            Method: get
        HelloName:
          Type: Api
          Properties:
            RestApiId: !Ref ServerlessApi
            Path: /api/hello/{name}
            Method: get

hello/app.js


exports.lambdaHandler = async (event, context) => {
  let name = 'world';

  console.log(`invoked hello world`, event);

  if (event.pathParameters && event.pathParameters.name) {
    name = event.pathParameters.name;
  }

  try {
    response = {
      'statusCode': 200,
      'body': JSON.stringify({
        message: 'Hello ' + name,
        name
      })
    }
  } catch (err) {
    console.log(err);
    return err;
  }

  return response;
};

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
jfusscommented, Feb 21, 2020

Was released in v0.42.0.

Closing

0reactions
douglasnaphascommented, Feb 6, 2020

@awood45 I’ve got a fix over on #1464, just need a little help getting it over the line.

@gordonmleigh I pulled your commits into #1649 and was able to get the integration tests passing in that PR.

Read more comments on GitHub >

github_iconTop Results From Across the Web

OPTIONS endpoint does not invoke lambda code #1434
Description We have implemented OPTIONS endpoints that do some CORS related processes. With the release of version 0.20.0 the OPTIONS ...
Read more >
Troubleshoot invocation issues in Lambda
If you invoke your function directly, you see any invocation errors in the response from Lambda. If you invoke your function asynchronously with...
Read more >
Invoking AWS Lambda endpoint
In API Gateway configs I see next options: Endpoint: https://xyz.execute-api.us-east-2.amazonaws.com/dummy/test-store. API key: QW123E45RTY6. ...
Read more >
AWS Lambda Function URLs — Another way to create ...
AWS released Lambda Function URLs (aka Lambda FURLs or just Lambda URLs), which gives you another ability to create a HTTP endpoint for...
Read more >
REST API (API Gateway v1)
Whereas, the lambda method makes you explicitly define headers, status codes, and more in the configuration of each API Gateway Endpoint (not in...
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