2

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:

  1. I have Docker installed and running
  2. I have installed the SAM CLI
  3. My AWS credentials are in place and working
  4. I have no connectivity issues and I'm able to connect to AWS normally
  5. I create the SAM application (HelloWorld) with all the files and resources, I didn't change anything.
  6. I run "sam build" and it finishes sucessfully
  7. 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:\xxxxxxx\lambda-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:\xxxxxxx\lambda-python3.8\hello_world runtime: python3.8 metadata: {} architecture: x86_64 functions: ['HelloWorldFunction'] Running PythonPipBuilder:ResolveDependencies Running PythonPipBuilder:CopySource

Build Succeeded

Built Artifacts : .aws-sam\build Built Template : .aws-sam\build\template.yaml

C:\xxxxxxx\lambda-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:\xxxxxxx\lambda-python3.8.aws-sam\build\HelloWorldFunction 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.

Christian Dechery
  • 876
  • 10
  • 31

2 Answers2

1

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:

  1. create samconfig.toml in the root dir of the project with the following contents. This will allow running sam local invoke from the terminal without having to add the command line argument:
   version=0.1
   [default.local_invoke.parameters]
   container_host_interface = "0.0.0.0"
  1. update launch configuration as follows to enable VSCode debugging:
...
"sam": {
   "localArguments": ["--container-host-interface","0.0.0.0"]
     }
...
Michal
  • 2,078
  • 23
  • 36
  • Ran into the same problem on Windows where "sam local start-api" and "sam local invoke" both yelled `Function 'LambdaHandler' timed out after 3 seconds` for my app. Adding the `samconfig.toml` with the configuration you suggested totally fixed it. Similarly, add `[default.local_start_api.parameters] container_host_interface = "0.0.0.0"` if OP wants to use "sam local start-api". – Mint Apr 07 '23 at 21:04
0

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.

  • No, nothing. As I said in my post, I'm using the "HelloWorld" example that comes embeded in the SAM setup. The code could not be any simpler. It just prints out HelloWorld and returns 200. – Christian Dechery Oct 05 '22 at 15:20
  • Then must be some permission issue, try running the vs code as admin or some other IDE maybe InteliJ Idea(It is much better for this use case) – Sumit Kumar Singh Oct 09 '22 at 15:38