Skip to content

OPTIONS endpoint does not invoke lambda code #1434

Closed

Description

@caspian154

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;
};

Activity

  1. added
    area/local/start-api
    stage/needs-investigation
    on Sep 30, 2019
  2. awood45 commented on Sep 30, 2019

    @awood45
    Contributor

    Looking into this - for some background, have you tried this on Lambda as well and seen a different result?

  3. caspian154 commented on Sep 30, 2019

    @caspian154
    Author

    @awood45 this is only an issue running sam local. When the template is run through cloudformation the api gateway properly invokes our Lambda. This is only an issue with testing locally - which is still a serious issue.

  4. caspian154 commented on Oct 9, 2019

    @caspian154
    Author

    @awood45 should this ticket be in https://github.com/awslabs/serverless-application-model/issues I was just trying to find this issue in that repo and realized there is aws-sam-cli and servereless-application-model

  5. gordonmleigh commented on Oct 15, 2019

    @gordonmleigh

    As far as I can see the problem was introduced by #1242. In particular, the following added lines short-circuit all OPTIONS requests to the CORS behaviour.

    https://github.com/awslabs/aws-sam-cli/blob/4cdd4076d688d8699ca9b87584c8fa59799a20b5/samcli/local/apigw/local_apigw_service.py#L176-L179

  6. gordonmleigh commented on Oct 21, 2019

    @gordonmleigh

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

  7. douglasnaphas commented on Feb 6, 2020

    @douglasnaphas
    Contributor

    @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.

  8. added and removed
    stage/needs-investigation
    on Feb 19, 2020
  9. added
    stage/waiting-for-release
    on Feb 19, 2020
  10. jfuss commented on Feb 21, 2020

    @jfuss
    Contributor

    Was released in v0.42.0.

    Closing

  11. diegoddox commented on May 16, 2023

    @diegoddox

    Not sure if I should create a new issue for it but I'm facing this on version 1.83.0

  12. diegoddox commented on Nov 5, 2023

    @diegoddox

    Any update on this? still facing this issue on v1.100.0

  13. rpcsp commented on May 16, 2024

    @rpcsp

    Facing this issue too with v1.103.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/local/start-apistage/waiting-for-releasetype/bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions