AWS Logo
Menu

run a docker web server on AWS Lambda with and Cloudfront

stay within the free tier and and run a web server on lambda by using the AWS Lambda Web Adapter , a tutorial using littlelink-server , a self hosted alternative to linktree

Published Jan 24, 2025
while the popular choice to run a small container with a web server might be AppRunner or ecs on fargate , using those services is not cheap , the minimum monthly cost for running this container on AppRunner would be 14 $ , so why not run it on lambda ?
while theoretically you can run your container on lambda as is , what will actually happen is something like this :
your function would timeout before serving your webpage , because as a web server , there's nothing to stop it from running after getting the request and sending back the response .
here comes AWS Lambda Web Adapter's role , it does just what we want , stops our lambda function after serving the webpage that we want , but there's a small catch , we need to add one line to our docker file and build again .
  1. let's go to ecr and create a repository to host your image , once created , click on "View push commands" , as you will be using those commands shortly
  2. open cloudshell in a new tab , cloudshell comes with the AWS cli and docker already installed , then let's start by cloning littlelink-server's repo
    git clone https://github.com/techno-tim/littlelink-server.git
    now cd into the directory and let's modify the dockerfile to add the web adapter , open the dockerfile using a file editor like nano and add the line
    COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.9.0 /lambda-adapter /opt/extensions/lambda-adapter
    your dockerfile should look like this
  3. now build your container and push it into ecr , use the commands obtained in the first step
  4. go to lambda and create a new function , select "container image" then under "Container image URI" , wither enter the url of the image or choose "browse images" to find it , keep the architecture as "x86_64" and create the function
  5. let's give our function a url , go to "configuration" then "function url" and create it , if you want the lambda generated url to be used to trigger the function , choose your "auth type" as none , however , if you want to use your own domain , through cloudfront , choose "aws_iam"
  6. now go to "environment variables" and add a few , refer to littlelink-server's readme page , in addition , add the variable "AWS_LWA_PORT" with the value "3000" , here's a few I added :
  7. launch this cloudformation stack to create a cloudfront distribution for your function url , don't forget to Remove https:// and trailing slash from the Function URL while providing the input.
  8. once created to go "origins" then edit the origin , choose the protocol as http only and set the port to 80 , scroll a little bit further and click on "create new OAC" , keep the default settings and create it , you will get a cli command to add this OAC to your lambda function , COPY it , paste it in cloudshell and add your function name then run it , go back to cloudfront and save the changes .
  9. congratulations , your function can now only be run from cloudfront and the service is authorized to run it , if you have your own domain follow the next steps
  10. go to certificate manager and create a public ssl certificate to use with cloudfront , enter the fqdn you want to use them add the records to your DNS , the certificate should become valid within a few minutes
  11. go back to cloudfront , on the "general" tab scroll down to settings and click on "edit" , enter the fqdn you requested the certificate for in the text box under " Alternate domain name (CNAME) - optional " and choose your certificate from under " Custom SSL certificate - optional" , scroll down and save the changes , wait for the distribution to finish modifying .

voila , you can now visit your the link and you'll be greeted with your own picture and links , if you want to add more , just edit your lambda environment variables then invalidate your cloudfront distribution . the best part you'll now be billed for less then 0.1 second for most invocations and less than 1 second for invocations that include initialization.

Comments