An Interest In:
Web News this Week
- March 21, 2024
- March 20, 2024
- March 19, 2024
- March 18, 2024
- March 17, 2024
- March 16, 2024
- March 15, 2024
Serverless and Containers
Often when we are talking about container and docker images the things that comes to mind in terms to deployment is
- Kubernetes
- ECS or Fargate
- Docker Swarm or Docker Daemon
Although these are the most commonly used technologies for deployments but for a small set of stateless applications this is comes over with a lot of management overheard.
At this point AWS has given the leverage to deploy and build container images on Lambda and run as any simple application.
Lets look into building and deploying a DOCKER application using serverless.
Pre-Req:
- Docker Daemon
- Serverless utility
- ECR and AWS Login Details
Install Serverless Utility
npm install -g serverless
Once installed lets create a structure using serverless utility for our node application
Create a Dockerfile to run your application
faisalnizam@Muhammads-MBP sample % serverless create --template aws-nodejs-docker --path aws-nodejs-docker-demo Project successfully created in "aws-nodejs-docker-demo" from "aws-nodejs-docker" template (3s)
Once configured we should have a structure like this
faisalnizam@Muhammads-MBP aws-nodejs-docker-demo % tree. Dockerfile README.md app.js serverless.yml0 directories, 4 files
Once we do cat on serverless.yml we would see the provider section just like terraform
provider: name: aws ecr: # In this section you can define images that will be built locally and uploaded to ECR images: appimage: path: ./
This will do the exact magic as docker build does and will build the application and tells where to get the content of the docker image from and where to fetch the image from (ECR in our case)
Now we can use the serverless utility to easily build and deploy the container by adding the profile in serverless.yml and running a simple command
faisalnizam@Muhammads-MBP aws-nodejs-docker-demo % serverless Onboarding "aws-nodejs-docker-demo" to the Serverless Dashboard? Do you want to login/register to Serverless Dashboard? No? Do you want to deploy now? YesDeploying aws-nodejs-docker-demo to stage dev (us-east-1) Service deployed to stack aws-nodejs-docker-demo-dev (237s)functions: hello: aws-nodejs-docker-demo-dev-hello
As you see above this does the build of the image and uploaded it it. We can attach resources like API Gateway to the lambda to start serveing us as well by un-commenting the following lines in serverless.yml
events: - httpApi: path: /users/create method: get
Run serveless deploy
faisalnizam@Muhammads-MBP aws-nodejs-docker-demo % serverless deploy Deploying aws-nodejs-docker-demo to stage dev (us-east-1) Service deployed to stack aws-nodejs-docker-demo-dev (56s)endpoint: GET - https://lkvr0lkbf6.execute-api.us-east-1.amazonaws.com/users/createfunctions: hello: aws-nodejs-docker-demo-dev-hello
Now we do have an endpoint configured for our docker container which has a proxy GET to the lambda using API Gateway
endpoint: GET - https://lkvr0lkbf6.execute-api.us-east-1.amazonaws.com/users/create
Now we have seen all the automated way lets try to decipher it and build all manually
Building our docker container manually for Lambda
Now we have a small structure of our application up so we can try building and deploying it
Login to your ECR repository
aws --region=me-south-1 --profile=aws-test ecr get-login-password --region | docker login --username AWS --password-stdin .dkr.ecr..amazonaws.com
Just substitute the right region and account ID with profile as needed, and you should see the message "Login Succeeded".
Setup a lambda ready Docker image
docker pull public.ecr.aws/lambda/nodejs:12
Lets build our docker using the docker cli
docker build -t
Create ECR Repository to push the image to
aws ecr create-repository --repository-name --image-scanning-configuration scanOnPush=true
Tag and Push docker
docker tag :latest .dkr.ecr..amazonaws.com/:latestdocker push .dkr.ecr..amazonaws.com/:latest
Now point serverless to the correct docker registry by adding the information to serverless.yml
functions: someFunction: image: .dkr.ecr..amazonaws.com/@
Once this part is done we just run serverless deploy to deploy our docker container in lambda
Original Link: https://dev.to/aws-builders/serverless-and-containers-274h
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To