AWS Lambda Web Adapter
The AWS Lambda Web Adapter is a versatile tool designed for developers who want to run web applications using various modern frameworks on AWS Lambda. This includes popular frameworks like Express.js, Next.js, Flask, SpringBoot, ASP.NET, and Laravel—essentially, any framework that communicates via HTTP 1.1 or 1.0. One of the significant advantages of this tool is its flexibility, allowing the same web application to be deployed across AWS Lambda, Amazon EC2, AWS Fargate, or even locally on a personal computer.
Features
- Flexible Web Hosting: It supports hosting web applications on AWS Lambda.
- Endpoint Integration: Seamlessly integrates with AWS services including Amazon API Gateway, Lambda Function URLs, and Application Load Balancer.
- Wide Compatibility: Offers support for both Lambda managed runtimes and custom Docker images without needing to modify existing code.
- Optimized Responses: Automatically encodes binary responses, supports response compression and streaming.
- Graceful Shutdown: Ensures smooth shutdown procedures for running applications.
- Versatile Event Handling: Extends beyond HTTP to handle a variety of non-HTTP event triggers like SQS, SNS, and more.
Usage
The AWS Lambda Web Adapter can be utilized with Lambda functions packaged as either Docker or OCI images, as well as Zip packages for AWS managed runtimes.
Docker Images
If using Docker, developers are required to include the Lambda Web Adapter binary in their Dockerfile. This can be done with a simple copy command:
COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.8.4 /lambda-adapter /opt/extensions/lambda-adapter
By default, the adapter expects the web app to listen on port 8080, though this can be customized. It supports both x86_64 and arm64 CPU architectures.
Zip Packages
For AWS managed runtime, developers need to attach the adapter layer to their functions, set specific environment variables, and designate a script to start their web application.
Readiness and Configurations
The adapter comes with a built-in readiness check mechanism, ensuring that applications are prepared to handle requests by pinging them repeatedly until a valid response is received or a timeout occurs. Developers have the ability to configure various parameters, such as port numbers and the readiness check path, through environment variables.
Main Configurations
- AWS_LWA_PORT: Defines the main port for web traffic.
- AWS_LWA_ENABLE_COMPRESSION: Toggles gzip compression for responses.
- AWS_LWA_READINESS_CHECK_PROTOCOL: Determines the protocol for readiness checks—either HTTP or TCP.
Request and Lambda Context
The AWS Lambda Web Adapter helps in transferring metadata from API Gateway to the web application, providing essential context in HTTP headers such as x-amzn-request-context
and x-amzn-lambda-context
. This aids in extracting useful information like request IDs and user identity.
Graceful Shutdown and Local Debugging
The adapter facilitates a graceful shutdown, allowing applications to handle shutdown signals gracefully. It also supports local debugging by allowing developers to run and test their web applications locally before deploying them to AWS Lambda.
Examples and Acknowledgements
The AWS Lambda Web Adapter includes examples for various frameworks and configurations, encouraging adaptation and showcasing versatility. The project draws inspiration from other community tools and stands alongside similar efforts to streamline serverless web application hosting.
License
The AWS Lambda Web Adapter is released under the Apache-2.0 License, promoting open-source collaboration and contribution.
In summary, the AWS Lambda Web Adapter is a robust solution for developers wishing to deploy their web applications in serverless environments, emphasizing flexibility, seamless integration, and comprehensive functionality.