DEV Community

Faisal Nizam for AWS Community Builders

Posted on

Serverless and Containers

Often when we are talking about container and docker images the things that comes to mind in terms to deployment is

  1. Kubernetes
  2. ECS or Fargate
  3. 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:

  1. Docker Daemon
  2. Serverless utility
  3. ECR and AWS Login Details

Install Serverless Utility

npm install -g serverless
Enter fullscreen mode Exit fullscreen mode

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)

Enter fullscreen mode Exit fullscreen mode

Once configured we should have a structure like this

faisalnizam@Muhammads-MBP aws-nodejs-docker-demo % tree
.
├── Dockerfile
├── README.md
├── app.js
└── serverless.yml

0 directories, 4 files

Enter fullscreen mode Exit fullscreen mode

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: ./

Enter fullscreen mode Exit fullscreen mode

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? Yes

Deploying 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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/create
functions:
  hello: aws-nodejs-docker-demo-dev-hello
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

Lets build our docker using the docker cli

docker build -t 
Enter fullscreen mode Exit fullscreen mode

Create ECR Repository to push the image to

aws ecr create-repository --repository-name  --image-scanning-configuration scanOnPush=true
Enter fullscreen mode Exit fullscreen mode

Tag and Push docker

docker tag :latest .dkr.ecr..amazonaws.com/:latest
docker push .dkr.ecr..amazonaws.com/:latest

Enter fullscreen mode Exit fullscreen mode

Now point serverless to the correct docker registry by adding the information to serverless.yml

functions:
  someFunction:
    image: .dkr.ecr..amazonaws.com/@
Enter fullscreen mode Exit fullscreen mode

Once this part is done we just run serverless deploy to deploy our docker container in lambda

Top comments (0)