How to Trigger Lambda for triggering Jenkins job

men sitting at table with computers
Reading Time: 5 minutes

Hello Folks !!! I hope you all are doing great, again I’m here with the new post that is useful and for you. In this blog we’ll see how to Trigger Lambda for triggering Jenkins job as it will help developer to work efficiently . AWS Lambda Function is an easy and efficent way to trigger pipeline.

So, Let’s get started !!!

Overview

We need an efficient way to trigger a Jenkins Job that will work efficiently and make it easy for the team to work. For that we’ll use AWS LAMBDA that will trigger Jenkins Job triggered by EventBridge(Cloud Watch Events).

Workflow

The following workflow is a part of AWS Support in which AWS EventBridge Rules will helps us to trigger AWS Lambda as:

The EventBridge Rules will be created.

EventBridge triggers a Lambda function.

AWS Lambda Function starts Jenkins job using Jenkins API.

Below is the workflow that how we can trigger the Jenkins Job using AWS Lambda.

EventBridge – AWSEventBridge is a serverless service that uses events to link application components together.

AWS Lambda – AWS Lambda is a serverless computing service provided by Amazon Web Services (AWS) that lets you run your code without provisioning or managing servers.

Jenkins Job – It is a collection of plugins that enables the integration of continuous delivery pipelines into Jenkins.

Steps to Trigger Jenkins Job through AWS Lambda

1. Generate API Token

We need an API Token to authenticate the API call to trigger the job.

Open Jenkins

Go to People >> User-Name >> Configure >> Add New Token under API Token >> Generate >> Save

Now add the authentication token to the job we want to trigger remotely.

Note: API token is equal to the Authentication token

Open the configuration of the job OR pipeline you want to trigger.

Go to job >> Configure >> Trigger builds remotely under Build Triggers >> add API Token to Authentication Token Field >> Save

With this information, you can now create a URL that looks like this:

http://user-name/:<api_token>@jenkins-web-url.com/job/<JobName>/build?token=<authentication_token&gt;

<user-name> It should be the name of the jenkins user.

<JobName> should be replaced with the Jenkins project name.

<api_token> should be replaced with API Token we created for the user.

<authentication_token> should be replaced with the API Token. If we want to trigger a parameterized job, replace the build with Buildwithparameters in the URL.

If you want to trigger Job using build Parameters create a URL as:

http://user-name/:<api_token>@jenkins-web-url.com/job/<JobName>/buildWithParameters?<name=value&gt;

2.Create AWS EventBridgeRule 

We’ll create an eventbridge rule to trigger the AWS Lambda which will help us to trigger Jenkins Job.

Go to AWS Console and search for service Amazon EventBridge

 AWS EventBridge >> Rules >>  Create Rule >> Rule Name >> Rule Type (rule with an event pattern) >> Click next

  • In Build Even Pattern configuration scroll down to Event Pattern leaving above configuration as default.

Event Pattern >> Event Source >> AWS Service (Lambda) >> Event Type (all events) >> Click Next

Select Target >> Target type (AWS) >> Select target >> Function >> next 

Configure Tags optional 

Finally Review and Create 

3.Create AWS Lambda Function 

To trigger jenkins job via AWS Lambda Function, you have to create  a Lambda Function.

Open your AWS Console and and search for service as Lambda

Go to Lambda >> Function >> create Function >> select Author From Scratch >> give function name >> select Python3.7 Runtime (any language you prefer) >> Create Function

leave all the other settings as default for now and Add the below code to the lambda function that was created in the above steps:

import requests
import os
from time import sleep
USERNAME = os.environ[‘USERNAME’]
JENKINS_URL = os.environ[‘JENKINS_URL’]
API_TOKEN = os.environ[‘API_TOKEN’]
AUTH_TOKEN = os.environ[‘AUTH_TOKEN’]
def lambda_handler(event, context):
try:
url = f”https://{USERNAME}:{API_TOKEN}@{JENKINS_URL}/job/aws-lambda-test/buildWithParameters?name=Deeksha&address=DayanandVihar”
print(url)
response = requests.post(url)
if response.status_code != 201:
print(response.status_code)
except Exception as err:
print(err.args[0])

as you can see in the above function we need some Environment Variables to trigger the job. So let’s add these variables to our Lambda Function.

Go to configuration >> Environment variables >> Add Environment Variable >> Save

Add trigger into lambda function as 

Go to Lambda >> Functions >> Your-Function

Select Function >> select Add trigger >>Trigger configuration >>  EventBridge(CloudWatch Events) >> Existing rules (as you have already created AWS EVentBridege Rule in step-2 above) >> Add

Note: If you do not have Existing rule click on Create a new rule & create rule for your lambda. I have already shows it on above step .

Once you have added trigger go to the Function >> Deploy >> add new Event >> Event name >> Create

Test the Lambda Function by clicking on Test as:

As you will Click on Test  your Jenkins Job will be triggered by the Lambda Function you have created.

This is the whole process by which we can trigger Jenkins Job using a lambda function.

Conclusion

As you can In this blog we have seen How we can Trigger Lambda for triggering Jenkins job. I hope this post will be helpful for you. Do ping me if you have any query regarding this.

Thank You !!!

Happy Learning 🙂 !!!

Reference

https://docs.aws.amazon.com/lambda/latest/dg/getting-started.html

https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-get-started.html

Written by 

Deeksha Tripathi is a Software Consultant at Knoldus Inc Software. She has a keen interest toward learning new technologies. Her practice area is DevOps. When not working, she will be busy in listening music , and spending time with her family .