Continuous Delivery with CloudBees Jenkins Platform and AWS Lambda

Last month, CloudBees announced its CloudBees Jenkins Platform now runs on AWS. The extension is a new level of integration that enables customers to easily install the CloudBees Jenkins Platform and run it on Amazon Web Services’ EC2 cloud service.

Building off this initial integration, CloudBees and AWS are actively working to rollout additional integration points - the latest of which being the integration with AWS Lambda. This blog will present how to implement a continuous delivery pipeline with Jenkins and AWS Lambda service.

AWS Lambda

AWS Lambda is a new service (Nov 2014) proposed by AWS to help implement event driven architectures on the cloud such as:

  • Transforming data as it reaches the cloud,
  • Perform notification, audit and analysis,
  • Start workflows.

AWS Lambda function can be written in javascript/Node.js and Java languages.

The new style of programming introduced by AWS Lambda requires new best practices to build, test and deploy these services.

Let’s implement a continuous delivery pipeline for AWS Lambda using CloudBees Jenkins Platform and the CloudBees AWS CLI Plugin for Jenkins.

We will use the CreateThumbnail sample described in AWS Lambda Walkthrough 2: Handling Amazon S3 Events Using the AWS CLI (Node.js). The source code is available at https://github.com/CloudBees-community/aws-lambda-create-thumbnail.

AWS Lambda function CreateThumbail.js

Jenkins project to build, test and deploy an AWS Lambda function

CloudBees Jenkins Platform
 

Jenkins Project

Create a Freestyle project named “aws-lambda-create-thumbnail”.

Source code configuration

In the “Source Code Management” section, select “Git” enter the repository URL “https://github.com/CloudBees-community/aws-lambda-create-thumbnail.git

Git configuration

 

AWS CLI Configuration

In the “Build Environment” section, tick the “Setup AWS CLI”.
Define the desired set of credentials and AWS region.

AWS CLI configuration in Jenkins

 

 

 

 

 

If the desired credentials are not yet created, click on the “Add” button of the field “API credentials”.
In the “Add Credentials” screen, select the kind “Amazon Web Services Basic Credentials”, enter the Access Key ID and the Secret Access Key and then click on “Add”.
Please note that you can also access the advanced configuration to define a human readable ID for the created Jenkins credentials and help readability when using Jenkins workflow.

Add AWS credentials

 

Build steps and AWS CLI

For this Node.js function demo, we will use a build automation based on simple shell scripts rather than on a specific build framework.

In the Build section, click on “Add build step” and select “Execute shell”.

The aws-lambda-create-thumbnail project contains a “build” script to package the Node.js function. Invoke this build script in the command.

# BUILD
./build

Once the AWS Lambda function is packaged, we upload it to AWS Lambda invoking the AWS CLI command “Build shell steps and AWS CLI invocations

# UPLOAD AMAZON LAMBDA FUNCTION TO AWS
aws lambda update-function-code \
--region us-east-1 \
--function-name CreateThumbnail \
--zip-file file://./target/CreateThumbnail.zip

It is time now to test the deployed function using the AWS CLI “aws lambda invoke”:

# TEST AMAZON LAMBDA FUNCTION ON AWS
aws lambda invoke \
--invocation-type Event \
--function-name CreateThumbnail \
--payload file://./src/test/input.json \
target/test-outputfile.json

Build shell steps and AWS CLI invocations

Creating an AWS Lambda function

It is time to create the AWS Lambda function if it doesn’t already exists.

AWS Lambda Function

In AWS Management Console, create an AWS Lambda function named “CreateThumbnail”:

New AWS Lambda function

Lambda function code with Node.js

Select the Runtime type Node.js, you can keep the Code entry type and Code template to their default values Edit code inline and Hello World. Define the handler to CreateThumbnail.handler as defined in CreateThumbnail.js.

Select the Role IAMLambdaExecute as defined in AWS Lambda Walkthroughs (Node.js) » … » Step 2.2: Create an IAM Role (execution role) (Amazon S3 Events).

AWS Lambda Function creation

We can now create the function clicking on Create Lambda function:

 
 

The AWS Management Console renders a dashboard of the function with valuable graphs and logs:

 

Conclusion

AWS Management Console dashboard for AWS Lambda functions

In this blog we have shown you how easy it is to create a continuous delivery pipeline to continuously build, test and deploy an AWS Lambda service with CloudBees Jenkins Platform and the CloudBees AWS CLI Plugin.

The CloudBees AWS CLI plugin is intended to be the Swiss Army knife to automate from a Jenkins job any action on an AWS infrastructure such as:

  • Copy generated artifacts to Amazon S3
  • Deploy a new version of an AWS Elastic Beanstalk application
  • Restore a snapshot of an Amazon RDS database before a test
  • Create an ephemeral environment (EC2, RDS …) to run an integrated test

Resources

Blog Categories: 

Add new comment