AWS Lambda Local Testing with RIE in Custom Docker Image Results in Socket Hang Up
I’m trying to test an AWS Lambda function locally using a custom Docker image that includes the AWS Lambda Runtime Interface Emulator (RIE). I'm working with Python on Windows 11.
Here's what I’ve done:
Created a custom Dockerfile that installs dependencies and includes AWS RIE.
The Lambda function code runs as expected if I don’t include RIE.
After adding RIE, the container starts correctly, and I can see the initial logs (e.g., current working directory and handler path).
I’m exposing the container port (e.g., -p 9000:8080) and sending a POST request with a sample payload to http://localhost:9000/2015-03-31/functions/function/invocations.
Despite the container running and showing initial logs, the request results in a socket hang up error. I confirmed with docker ps that the container remains in the running state throughout.
What could be causing this socket hang up when using RIE in the Docker container? Are there any platform-specific (Windows) considerations or common misconfigurations that I might be missing?
Any insights or debugging tips would be appreciated.
- Topics
- ServerlessCompute
- Tags
- AWS Lambda
- Language
- English
asked a year ago629 views
- Newest
- Most votes
- Most comments
The "socket hang up" error you're experiencing when testing your AWS Lambda function locally with the Runtime Interface Emulator (RIE) could be caused by several issues:
-
Network Connectivity Issues: The error suggests there might be a problem with the network connection between your local environment and the containerized Lambda function. This could be related to how the Docker networking is configured on Windows 11.
-
Environment Variables: Make sure you've set the necessary environment variables for your Lambda function. For OpenTelemetry instrumentation, for example, you might need variables like
AWS_LAMBDA_EXEC_WRAPPER,OPENTELEMETRY_TRACE_ENABLED, andOTEL_LOG_LEVEL. -
File System Permissions: Ensure that your Lambda function has the necessary permissions to write to the
/tmpdirectory if your code requires it. While/tmpshould be writable in Lambda, there might be permission issues in your Docker container setup. -
Docker Run Command: Check your Docker run command. For Linux/macOS, it should look like:
docker run --platform linux/amd64 -d -v ~/.aws-lambda-rie:/aws-lambda -p 9000:8080 \
--entrypoint /aws-lambda/aws-lambda-rie \
docker-image:test \
/usr/local/bin/python -m awslambdaric lambda_function.handler
For PowerShell on Windows:
docker run --platform linux/amd64 -d -v "$HOME\.aws-lambda-rie:/aws-lambda" -p 9000:8080 `
--entrypoint /aws-lambda/aws-lambda-rie `
docker-image:test `
/usr/local/bin/python -m awslambdaric lambda_function.handler
- Debugging Tips:
- Set debug logging for more detailed information
- Check if there are any timeout issues (the default Lambda timeout might be too short)
- Verify that your Docker container has the correct architecture (use
--platform linux/amd64) - Ensure your Lambda handler path is correctly specified
If you're using tools like AWS SAM CLI for local testing, they handle much of the RIE setup automatically, which might be an easier approach than configuring the Docker container manually.
Sources
Lambda, opentelemetry: socket hang up | AWS re:Post
/tmp not writable in lambda | AWS re:Post
Deploy Python Lambda functions with container images - AWS Lambda
Tutorial: Deploy a Hello World application with AWS SAM - AWS Serverless Application Model
answered a year ago
Relevant content
asked 2 years ago
