Node Js Function Trigger S3 File Upload Lamda
This article depicts a existent use-case where a Jenkins task is triggered using AWS Lambda on a successful upload to AWS S3
WORKFLOW
- Object is uploaded to S3
- S3 event triggers a Lambda part
- Lambda triggers Jenkins chore using Jenkins API
Create Jenkins User
Outset of all, create a Jenkins user with sufficient permissions. The user must have Overall read, Job build, read, workspace accesses.
To create Jenkins user, become to Manage Jenkins >> Manage Users >> Create User
Grant Required Permissions to User
To requite sufficient permissions to the new user, go to
Manage Jenkins >> Configure Global Security >> bank check Project-based Matrix Authorization Strategy under Dominance
add the lambda-user to user/groups and grant required permissions
Configure API Token and Authentication Token
Login using the newly created Lambda-Jenkins-User. Nosotros need to create an API Token for the user which we will employ for authenticating the API call for triggering the task
go to People >> lambda-user >> Configure >> Add New Token nether API Token >> Generate >> Save
Logout and login as Root User. Now we need to add an Authentication Token to the job which we want to trigger remotely.
become to job >> Configure >> bank check 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://lambda-user:<api_token>@jenkins-web-url.com/job/<JobName>/build?token=<authentication_token>
<JobName> should be replaced with Jenkins project proper noun. <api_token> should be replaced with API Token we created for user . <authentication_token> should be replaced with Authentication Token nosotros created for chore. If nosotros want to trigger a parameterized job, supplant build with buildWithParameters in the url
Create S3 Bucket
I take created a bucket named lambda-trigger-sample. This is the bucket which triggers the Lambda
Create Lambda Function
go to Lambda >> create Function >> select Author From Scratch >>give function name >> select Python3.8 Runtime >> Create Function
Add Trigger to Lambda Function
click Add Trigger under Configuration >> select S3 from drib-down >> select lambda-trigger-sample bucket >> under Event Blazon, select All Object Create Event >> Add together
Add Requests Parcel to Lambda Function
We demand to add Requests package to Lambda Function externally to transport mail requests to Jenkins server. First step is to export the Lambda Function we created just now
go to Actions >> Consign Function >> Download Deployment Packet
unzip the downloaded zip file to get lambda_function.py. execute the below commands in the verbal same society.
nothing function.cypher lambda_function.py aws lambda update-function-code --function-name <function_name> --goose egg-file fileb://function.zip pip install --target ./package requests cd package naught -r9 ${OLDPWD}/function.null . cd $OLDPWD zip -g office.cypher lambda_function.py aws lambda update-function-lawmaking --part-name <function_name> --zip-file fileb://role.zip
Please go through the AWS Documentation to find explanations of the to a higher place steps. <function_name> should be replaced with the name of Lambda Function. If aws cli is non installed, please install and configure it by referring the official documentation.
Add together Python Code to Lambda Function
In Configure Function, paste the following lawmaking snippet into alphabetize.js file
Here, we are storing Jenkins username, Server url, Api token and Hallmark token as environment variables in the lambda function. For that, get to Manage Environment Variables >> Add Environment Variable >> Add all values as Central-Value Pairs >> Save
Please go through this to run across how we can access Environment Variable within Lambda function. This commodity explains how to make a post request using Requests
Note that the url in the role is for triggering a parameterized job. We take taken the value of fundamental of the object which created the event (object which was uploaded to S3 bucket) from the Amazon S3 notification event and stored it into a variable named primal. As a parameter to Jenkins job, we are passing the fundamental.
Conclusion
By these configurations, we have integrated AWS S3, AWS Lambda and Jenkins. I hope this tutorial will help other people to hands gear up information technology up.
Source: https://www.linkedin.com/pulse/aws-s3-lambda-jenkins-integration-krishnanunni-menon
0 Response to "Node Js Function Trigger S3 File Upload Lamda"
Post a Comment