run a docker web server on AWS Lambda with Web Adapter 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
- 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
- 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 lineCOPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.9.0 /lambda-adapter /opt/extensions/lambda-adapter
your dockerfile should look like thisImage not found - now build your container and push it into ecr , use the commands obtained in the first step
- 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
- 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"
- 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 :Image not found
- 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.
- 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 .
- 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
- 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
- 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 .