Jenkins 2, AWS Lambda and Serverless Framework

2020-04-17 jenkins serverless docker jcasc

In this article, we are going to review how you can start using Jenkins and deliver your code running in AWS Lambda. AWS Lambda allows you to run an application without having to pay for dedicated server or infrastructure, you pay only what you use.

Serverless Framework allows you to deliver your application easily and fashionably to your AWS environments, now we will learn an approach to how you can use these tools along with Jenkins.

You can get in-depth by reviewing this documentation: https://serverless.com/framework/docs/

How does Serverless framework works?

Serverless will work by default with the serverless.yml file. In serverless.yml we will define our AWS Lambda functions by each class. For example

Implemeting CD

Let’s review the pipeline and it explanation in the comments:

Notes: The pipeline is generating on the fly a custom docker image with the minimum required tools: aws cli, npm and serverless. Below is the definition of the Dockerfile

Run it on your machine

Prior running this example you need docker and docker-compose installed.

git clone https://github.com/danielalejandrohc/jenkins-examples.git cd jenkins-examples/serverless-framework

Now you need to update ‘docker-compose.yaml’ with your set of keys of AWS account or generate a new pair. To start, I would recommend you use a user with admin privileges, and then you can begin to constraint the permissions with the specific services you use in your serverless.yml.

docker-compose up

At this moment we have all configured to run the job:

http://localhost:8080/job/aws-lambda-deploy/

You might expect a result as below in your CF stack:

Jenkins logs will look like this

In the output logs you will notice the AWS API Gateways endpoints so you can start to poke around with them.

How to test it?

In the output logs of the build you can notice the AWS API Gateway endpoints. so let’s give them a try:

That was it?

Yeah!, At this time, If you check your CloudFormation you’ll notice a new Stack created with all the resources required based on the serverless.yml template for the default region we have used (us-east-1). This example contains a DyanmoDb table that will interact with our lambda services.

I prefer to use the AWS Lambda services in front of an AWS Application Load Balancer out of the serverless.yml, which allows me to keep more consistency in how the endpoint of the API will look like in the long run.

I hope this might be able to help you!