Aws-sam-cli: OPTIONS endpoint does not invoke lambda code

Created on 27 Sep 2019  路  7Comments  路  Source: aws/aws-sam-cli

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

```sam local start-api -p 3002 --debug
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
```AWSTemplateFormatVersion: '2010-09-09'
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

```let response;

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

arelocastart-api stagwaiting-for-release typbug

All 7 comments

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

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

@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

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

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

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

Was released in v0.42.0.

Closing

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rhlsthrm picture rhlsthrm  路  4Comments

goldenbearkin picture goldenbearkin  路  3Comments

debuggins picture debuggins  路  4Comments

drumadrian picture drumadrian  路  3Comments

Caian picture Caian  路  3Comments