aws-sam-cli icon indicating copy to clipboard operation
aws-sam-cli copied to clipboard

sam local commands fail for Hello World lambda

Open atoone opened this issue 5 years ago • 7 comments

sam local functions all fail for default HelloWorld function, reporting "No response from invoke container for HelloWorldFunction" / "Invalid lambda response received: Lambda response must be valid json"

Environment:

  • aws-cli : SAM CLI, version 1.18.0
  • docker: Docker version 19.03.1, build 74b1e89e8a
  • python 3.7.9
  • Virtualbox 6.1.18
  • Windows 7

Set Up: Create the default HelloWorld application, using python3.7 :

> sam init Template Source Choice 1: AWS Quick Start Templates Package Choice 1: zip Runtime Choice 9: Python 3.7 Template choice 1: Hello World example

> sam build

> sam local invoke --debug

2021-02-12 14:57:04,630 | local invoke command is called                                                                                                                                                                                                                 
2021-02-12 14:57:04,643 | No Parameters detected in the template                                                                                                                                                                                                         
2021-02-12 14:57:04,736 | 2 resources found in the template                                                                                                                                                                                                              
2021-02-12 14:57:04,736 | Found Serverless function with name='HelloWorldFunction' and CodeUri='HelloWorldFunction'                                                                                                                                                      
2021-02-12 14:57:04,795 | Found one Lambda function with name 'HelloWorldFunction'                                                                                                                                                                                       
2021-02-12 14:57:04,795 | Invoking app.lambda_handler (python3.7)                                                                                                                                                                                                        
2021-02-12 14:57:04,796 | No environment variables found for function 'HelloWorldFunction'                                                                                                                                                                               
2021-02-12 14:57:04,796 | Environment variables overrides data is standard format                                                                                                                                                                                        
2021-02-12 14:57:04,797 | Loading AWS credentials from session with profile 'None'                                                                                                                                                                                       
2021-02-12 14:57:04,829 | Resolving code path. Cwd=F:\projects\sam-app\.aws-sam\build, CodeUri=HelloWorldFunction                                                                                                                                    
2021-02-12 14:57:04,829 | Resolved absolute path to code is F:\projects\sam-app\.aws-sam\build\HelloWorldFunction                                                                                                                                    
2021-02-12 14:57:04,830 | Code F:\projects\sam-app\.aws-sam\build\HelloWorldFunction is not a zip/jar file                                                                                                                                           
2021-02-12 14:57:04,889 | Skip pulling image and use local one: amazon/aws-sam-cli-emulation-image-python3.7:rapid-1.18.0.                                                                                                                                               
                                                                                                                                                                                                                                                                         
2021-02-12 14:57:04,890 | Mounting F:\projects\sam-app\.aws-sam\build\HelloWorldFunction as /var/task:ro,delegated inside runtime container                                                                                                          
2021-02-12 14:57:05,375 | Starting a timer for 30 seconds for function 'HelloWorldFunction'                                                                                                                                                                              
2021-02-12 14:57:09,258 | Cleaning all decompressed code dirs                                                                                                                                                                                                            
2021-02-12 14:57:09,267 | No response from invoke container for HelloWorldFunction                                                                                                                                                                                       
2021-02-12 14:57:09,296 | Sending Telemetry: {'metrics': [{'commandRun': {'requestId': '8ebfedc3-168e-4b85-a900-55cfe5379a91', 'installationId': '124efb15-d1bd-47bb-8b98-e654f948fb6d', 'sessionId': '353b9e74-a108-489e-9083-519c0dce5779', 'executionEnvironment': 'CL
I', 'pyversion': '3.8.7', 'samcliVersion': '1.18.0', 'awsProfileProvided': False, 'debugFlagProvided': True, 'region': '', 'commandName': 'sam local invoke', 'duration': 4674, 'exitReason': 'success', 'exitCode': 0}}]}                                               
2021-02-12 14:57:09,876 | HTTPSConnectionPool(host='aws-serverless-tools-telemetry.us-west-2.amazonaws.com', port=443): Read timed out. (read timeout=0.1)

Running start api and using browser to access \hello reports similar error

sam local start-api --debug

2021-02-12 15:02:02,327 | local start-api command is called
2021-02-12 15:02:02,339 | No Parameters detected in the template
2021-02-12 15:02:02,418 | 2 resources found in the template
2021-02-12 15:02:02,419 | Found Serverless function with name='HelloWorldFunction' and CodeUri='HelloWorldFunction'
2021-02-12 15:02:02,452 | No Parameters detected in the template
2021-02-12 15:02:02,515 | 2 resources found in the template
2021-02-12 15:02:02,516 | Found '1' API Events in Serverless function with name 'HelloWorldFunction'
2021-02-12 15:02:02,516 | Detected Inline Swagger definition
2021-02-12 15:02:02,517 | Lambda function integration not found in Swagger document at path='/hello' method='get'
2021-02-12 15:02:02,517 | Found '0' APIs in resource 'ServerlessRestApi'
2021-02-12 15:02:02,518 | Removed duplicates from '0' Explicit APIs and '1' Implicit APIs to produce '1' APIs
2021-02-12 15:02:02,518 | 1 APIs found in the template
2021-02-12 15:02:02,547 | Mounting HelloWorldFunction at http://127.0.0.1:3000/hello [GET]
2021-02-12 15:02:02,547 | 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
2021-02-12 15:02:02,548 | Localhost server is starting up. Multi-threading = True
2021-02-12 15:02:02  * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)
2021-02-12 15:03:20,948 | Constructed String representation of Event to invoke Lambda. Event: {"body": null, "headers": {"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9", "Accept-Encoding": "gzip, deflate, br", "Accept-Language": "en-GB,en;q=0.9,en-US;q=0.8", "Connection": "keep-alive", "Host": "127.0.0.1:3000", "Sec-Ch-Ua": "\"Chromium\";v=\"88\", \"Google Chrome\";v=\"88\", \";Not A Brand\";v=\"99\"", "Sec-Ch-Ua-Mobile": "?0", "Sec-Fetch-Dest": "document", "Sec-Fetch-Mode": "navigate", "Sec-Fetch-Site": "none", "Sec-Fetch-User": "?1", "Upgrade-Insecure-Requests": "1", "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36", "X-Forwarded-Port": "3000", "X-Forwarded-Proto": "http"}, "httpMethod": "GET", "isBase64Encoded": false, "multiValueHeaders": {"Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"], "Accept-Encoding": ["gzip, deflate, br"], "Accept-Language": ["en-GB,en;q=0.9,en-US;q=0.8"], "Connection": ["keep-alive"], "Host": ["127.0.0.1:3000"], "Sec-Ch-Ua": ["\"Chromium\";v=\"88\", \"Google Chrome\";v=\"88\", \";Not A Brand\";v=\"99\""], "Sec-Ch-Ua-Mobile": ["?0"], "Sec-Fetch-Dest": ["document"], "Sec-Fetch-Mode": ["navigate"], "Sec-Fetch-Site": ["none"], "Sec-Fetch-User": ["?1"], "Upgrade-Insecure-Requests": ["1"], "User-Agent": ["Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36"], "X-Forwarded-Port": ["3000"], "X-Forwarded-Proto": ["http"]}, "multiValueQueryStringParameters": null, "path": "/hello", "pathParameters": null, "queryStringParameters": null, "requestContext": {"accountId": "123456789012", "apiId": "1234567890", "domainName": "127.0.0.1:3000", "extendedRequestId": null, "httpMethod": "GET", "identity": {"accountId": null, "apiKey": null, "caller": null, "cognitoAuthenticationProvider": null, "cognitoAuthenticationType": null, "cognitoIdentityPoolId": null, "sourceIp": "127.0.0.1", "user": null, "userAgent": "Custom User Agent String", "userArn": null}, "path": "/hello", "protocol": "HTTP/1.1", "requestId": "d282a32c-7b65-4a39-86ed-472b8dc50915", "requestTime": "12/Feb/2021:15:02:02 +0000", "requestTimeEpoch": 1613142122, "resourceId": "123456", "resourcePath": "/hello", "stage": "Prod"}, "resource": "/hello", "stageVariables": null, "version": "1.0"}
2021-02-12 15:03:20,953 | Found one Lambda function with name 'HelloWorldFunction'
2021-02-12 15:03:20,953 | Invoking app.lambda_handler (python3.7)
2021-02-12 15:03:20,954 | No environment variables found for function 'HelloWorldFunction'
2021-02-12 15:03:20,954 | Environment variables overrides data is standard format
2021-02-12 15:03:20,955 | Loading AWS credentials from session with profile 'None'
2021-02-12 15:03:20,985 | Resolving code path. Cwd=F:\projects\sam-app\.aws-sam\build, CodeUri=HelloWorldFunction
2021-02-12 15:03:20,985 | Resolved absolute path to code is F:\projects\sam-app\.aws-sam\build\HelloWorldFunction
2021-02-12 15:03:20,986 | Code F:\projects\sam-app\.aws-sam\build\HelloWorldFunction is not a zip/jar file
2021-02-12 15:03:21,032 | Skip pulling image and use local one: amazon/aws-sam-cli-emulation-image-python3.7:rapid-1.18.0.

2021-02-12 15:03:21,033 | Mounting F:\projects\sam-app\.aws-sam\build\HelloWorldFunction as /var/task:ro,delegated inside runtime container
2021-02-12 15:03:21,480 | Starting a timer for 30 seconds for function 'HelloWorldFunction'
2021-02-12 15:03:25,359 | Cleaning all decompressed code dirs
2021-02-12 15:03:25,360 | No response from invoke container for HelloWorldFunction
2021-02-12 15:03:25,361 | Invalid lambda response received: Lambda response must be valid json
2021-02-12 15:03:25 127.0.0.1 - - [12/Feb/2021 15:03:25] "GET /hello HTTP/1.1" 502 -
2021-02-12 15:03:25 127.0.0.1 - - [12/Feb/2021 15:03:25] "GET /favicon.ico HTTP/1.1" 403 -

Lambda can be deployed and run on AWS, but no local debugging or execution appears possible. This makes it very hard to develop functions.

(Note that timeout was increased to ensure any startup delay was not causing problems with execution).

atoone avatar Feb 12 '21 16:02 atoone

do you see any containers spinning up at all? Also I see that you are on virtualbox with Windows 7, are you using docker for windows? or docker toolbox?

sriram-mv avatar Feb 17 '21 21:02 sriram-mv

Yes, containers are running locally (under VirtualBox) - using docker toolbox

$ docker --version
Docker version 19.03.1, build 74b1e89e8a

atoone avatar Mar 08 '21 15:03 atoone

@atoone Are you still having this issue? If you fixed it, please let me know how did you fix it. Thanks!

xazhao avatar Aug 13 '21 00:08 xazhao

@xazhao , updated sam cli to version 1.33.0 and it got fixed

gurgennersesyan avatar Oct 04 '21 18:10 gurgennersesyan

I have the same issue: SAM CLI, version 1.36.0 Docker version 20.10.12, build e91ed5707e

Fresh project from SAM template.

michalborek avatar Dec 23 '21 09:12 michalborek

Make sure that in /etc/hosts, the default value of --container-host (which is localhost) is mapped to the default value of --container-host-interface (which is 127.0.0.1). Or explicitly set the --container-host and/or --container-host-interface value.

In my case, localhost was mapped to 127.0.1.1 and the hint about increasing the SAM_CLI_CONTAINER_CONNECTION_TIMEOUT, introduced in v1.47, was just a red herring.

alsa avatar May 05 '22 18:05 alsa

Reading above comments looks like this was fixed already. If you are still having trouble, please create a new issue (closed ones are hard to track).

jfuss avatar Aug 30 '22 21:08 jfuss