Running Nginx on AWS Lambda

Running Nginx on AWS Lambda
Photo by Alex Kulikov / Unsplash

As we are aware, AWS Lambda has been a very interesting platform altogether to get your serverless applications or APIs running within minutes and scales in or out pretty quick. In this post, I will help you understand how exactly we can use AWS Lambda to run Nginx (Server Software) efficiently and with lower cost.

Understanding the Architecture of AWS Lambda

AWS Lambda runs on Firecracker. Firecracker is an open-source hypervisor that was created by AWS. Firecracker is a Micro VM. Firecracker's source code allows us to understand the behind-the-scenes methods adapted, and how the reverse engineering could have been performed.

Please watch the following Live YouTube video for more information:
https://www.youtube.com/live/xiw4DSfmTpg?si=8ZH7lDDWak07hcx3&t=3218

Solution

  1. Prepare and get your Nginx Config ready.
  2. Note that all loggings emitted by Nginx will be towards the /tmp folder.
  3. Clone the Nginx on Lambda GitHub repository.
  4. git clone https://github.com/InspectorGadget/nginx-on-lambda.git
  5. Review the lambda-runtime folder.
    1. This folder will contain a bootstrap file. The content in this file will ensure the Nginx service starts, to keep the site up and sharing the contents.

      Example:
      exec nginx -c /etc/nginx/nginx.conf -g "daemon off;" - This line will turn on Nginx while having the Daemon off. This will compensate with AWS Lambda's cold start feature.
  6. Review Dockerfile in the root directory. This Dockerfile serves all the bits and pieces needed to make this solution working.
  7. We're using Terraform to automate the Docker file build-up and also ensuring all the bits and pieces are made available for the deployment. Terraform will serve as an orchestrator to orchestrate the deployment end-to-end.
  8. Run terraform init to initialize Terraform
  9. Finally, run terraform apply to apply the infrastructure changes.
    1. You may need to acknowledge with a 'Yes' when prompted by Terraform.

Moving forward

  1. You can re-run the deployment step to pull in latest patches for the base operating system (Alpine).
  2. You may extend the configuration further to accomodate with any of your requirements.
  3. Anything to suggest? Please feel free to raise a PR.

Resources

GitHub - InspectorGadget/nginx-on-lambda: Nginx on Lambda? Yes, I’m serious.
Nginx on Lambda? Yes, I’m serious. Contribute to InspectorGadget/nginx-on-lambda development by creating an account on GitHub.
GitHub - firecracker-microvm/firecracker: Secure and fast microVMs for serverless computing.
Secure and fast microVMs for serverless computing. - firecracker-microvm/firecracker