I’m trying to get a local env to run/debug Python Lambdas with VSCode (windows). I’m using a provided HelloWorld example to get the hang of this but I’m not being able to invoke.
Steps used to setup SAM and invoke the Lambda:
- I have Docker installed and running
- I have installed the SAM CLI
- My AWS credentials are in place and working
- I have no connectivity issues and I’m able to connect to AWS normally
- I create the SAM application (HelloWorld) with all the files and resources, I didn’t change anything.
- I run "sam build" and it finishes sucessfully
- I run "sam local invoke" and it fails with timeout. I increased the timeout to 10s, still times out. The HelloWorld Lambda code only prints and does nothing else, so I’m guessing the code isn’t the problem, but something else relating to the container or the SAM env itself.
C:xxxxxxxlambda-python3.8>sam build Your template contains a
resource with logical ID "ServerlessRestApi", which is a reserved
logical ID in AWS SAM. It could result in unexpected behaviors and is not recommended.Building codeuri:
C:xxxxxxxlambda-python3.8hello_world runtime: python3.8 metadata:
{} architecture: x86_64 functions: [‘HelloWorldFunction’] Running
PythonPipBuilder:ResolveDependencies Running
PythonPipBuilder:CopySourceBuild Succeeded
Built Artifacts : .aws-sambuild Built Template :
.aws-sambuildtemplate.yamlC:xxxxxxxlambda-python3.8>sam local invoke Invoking
app.lambda_handler (python3.8) Skip pulling image and use local one:
public.ecr.aws/sam/emulation-python3.8:rapid-1.51.0-x86_64.Mounting C:xxxxxxxlambda-python3.8.aws-sambuildHelloWorldFunction
as /var/task:ro,delegated inside runtime container Function‘HelloWorldFunction’ timed out after 10 seconds
No response from invoke container for HelloWorldFunction
Any hints on what’s missing here?
Thanks.
2
Answers
Mostly, a lambda function gets timed out because of some resource dependency. Are you using any external resource, maybe db connection or some REST API call ?
Please put more prints in lambda_handler(your function handler), before calling any resource, then you might know where exactly it is waiting. Also increase the timeout to 1 minute or more because most of the external resource call over HTTPS will have 30 secs timeouts.
The log suggests that either the container wasn’t started, or SAM couldn’t connect to it.
Sometimes the hostname resolution on Windows can be affected by hosts file or system settings.
Try running the invoke command as follows (this will make the container ports bind to all interfaces):
sam local invoke --container-host-interface 0.0.0.0
…additionally try setting the container-host parameter (set to
localhost
by default):sam local invoke --container-host-interface 0.0.0.0 --container-host host.docker.internal
The next piece of puzzle is incorporating these settings into VSCODE. This can to be done in two places:
samconfig.toml
in the root dir of the project with the following contents. This will allow runningsam local invoke
from the terminal without having to add the command line argument: