Voiced by Amazon Polly |
Overview
Serverless computing has revolutionized how we build and deploy applications, making scaling and managing resources easier. AWS Lambda is a leading serverless compute service that allows you to run code in response to various events. Automating deployment can save time and reduce errors when working on a project with multiple AWS Lambda functions. In this blog, we will go through deploying multiple AWS Lambda functions using AWS CodeCommit and AWS CodePipeline, streamlining your serverless application deployment process.
Pioneers in Cloud Consulting & Migration Services
- Reduced infrastructural costs
- Accelerated application deployment
Introduction
AWS Lambda functions are essential in serverless apps because they let developers run code without setting up or maintaining servers.
Problem and Solution
However, organizing and deploying several Lambda functions can be difficult as serverless applications become more complicated. Typical difficulties include:
- Dependency Management: Managing dependencies is just one. Managing libraries and dependencies for several AWS Lambda functions can be challenging, especially if their requirements differ.
- Version Control: It might be difficult and error-prone to keep track of the versions, modifications, and dependencies between different functions.
- Deployment Coordination: It can be difficult to coordinate the deployment of several AWS Lambda functions so that updates and rollback methods are consistent.
- Testing and Monitoring: Maintaining application stability may require carefully testing and monitoring each AWS Lambda function.
Solution:
AWS offers a solution utilizing AWS CodeCommit and AWS CodePipeline to address these issues and ease the deployment of numerous Lambda functions.
- AWS CodeCommit: Teams can store and version their application code using the managed source control service AWS CodeCommit. For AWS Lambda functions, AWS CodeCommit assists in maintaining a central repository, making it simpler to manage and track changes.
- AWS CodePipeline: AWS CodePipeline automates the construction, test, and deployment stages of application releases. AWS CodePipeline is a continuous delivery service. You are able to define a pipeline that streamlines the deployment of AWS Lambda functions using this tool. You may plan deployments, conduct testing, and, if necessary, roll back changes with AWS CodePipeline.
Your serverless application development workflow can be improved by including AWS CodeCommit and AWS CodePipeline.
- AWS CodeCommit can help you centralize code management by storing your AWS Lambda function code in a single repository, making it simpler to manage and version.
- Automate Deployments: To automate the deployment of AWS Lambda functions and ensure consistency and dependability, use AWS CodePipeline.
- Coordination of Updates: As part of a single deployment process, coordinate updates and rollbacks for numerous AWS Lambda functions.
- Integrate testing and monitoring procedures into your pipeline to guarantee the dependability and effectiveness of your serverless application.
Example
- Setting Up the Project Repository:
Create a git repository in AWS CodeCommit and clone it using the clone URL and git credentials to your local.
1 |
git clone https://git-codecommit.us-east-1.amazonaws.com/v1/repos/test_multi_lambda |
Create the files by referring to the below folder structure
Test-multi-lambda/
|– lambda1/
| |– app.py
|– lambda2/
| |– app.py
|– buildspec.yaml
Add hello world messages in app.py and create lambda functions with the same name used in folder names as shown below:
- Creating Build Specifications:
This code helps to update only the AWS Lambda functions that have a change in them using the git diff between two commits so that instead of updating all the functions, we will detect the lambda files that have a change will only be updated.
Buildspec.yaml file code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
version: 0.2 phases: install: commands: - apt-get update - apt-get install -y zip unzip build: commands: - echo Build started on `date` post_build: commands: - echo $CODEBUILD_RESOLVED_SOURCE_VERSION - COMMIT_BELOW=$(git log -n 1 --skip 1 --format="%H") - echo $COMMIT_BELOW - TEST=$(git diff --dirstat=files,0 $CODEBUILD_RESOLVED_SOURCE_VERSION $COMMIT_BELOW) - echo $TEST - CHANGED_DIRECTORIES=$(git diff --dirstat=files,0 $CODEBUILD_RESOLVED_SOURCE_VERSION $COMMIT_BELOW | awk '{print $2}') - echo debug msg - echo $CHANGED_DIRECTORIES - | for dir in $CHANGED_DIRECTORIES; do dir_name=$(echo "$dir" | tr -d '/') echo "$dir_name" zip -qr "${dir_name}.zip" ./"${dir_name}"/*; aws lambda update-function-code --function-name $dir_name --zip-file fileb://"${dir_name}.zip"; done - echo Build Finished on `date` |
- This is a configuration file for AWS CodeBuild.
- It defines three phases: install, build, and post_build.
- In the post_build phase, it captures code changes between the current and previous Git commits and provides the folder names with changes.
- For each changed directory, it creates a zip file and updates an AWS Lambda function with the code from that directory.
Push the code to the remote repository to build the project and pipeline.
- Configuring AWS CodeBuild Projects:
Create a project in AWS CodeBuild, as shown below. Make sure to use the full option in git clone depth.
Configure the project environment as shown below, ensure the role name has full access to AWS CodeCommit and AWS Lambda services, and let the rest of the fields default.
- Setting Up AWS CodePipeline
Let’s create an AWS CodePipeline as shown below. Make sure to use a full clone in the source stage, and you can skip the deploy stage as we are directly updating the AWS Lambda functions from the buildspec file.
- Testing and Monitoring
For testing, update the lambda1>app.py code, commit, and push it to remote, as shown below:
The pipeline is triggered as soon as it detects the buildspec file, and logs are shown below:
Below is the build log, which shows the directories that have changes in them, and using for loop, it updates only those specific AWS Lambda functions as shown below:
Conclusion
AWS Lambda functions are fundamental to serverless applications, but managing and deploying multiple functions can be challenging. AWS CodeCommit and AWS CodePipeline provide a streamlined solution for centralizing code management, automating deployments, and ensuring the reliability of serverless applications.
Drop a query if you have any questions regarding AWS Lambda functions and we will get back to you quickly.
Making IT Networks Enterprise-ready – Cloud Management Services
- Accelerated cloud migration
- End-to-end view of the cloud environment
About CloudThat
CloudThat is an official AWS (Amazon Web Services) Advanced Consulting Partner and Training partner, AWS Migration Partner, AWS Data and Analytics Partner, AWS DevOps Competency Partner, Amazon QuickSight Service Delivery Partner, AWS EKS Service Delivery Partner, and Microsoft Gold Partner, helping people develop knowledge of the cloud and help their businesses aim for higher goals using best-in-industry cloud computing practices and expertise. We are on a mission to build a robust cloud computing ecosystem by disseminating knowledge on technological intricacies within the cloud space. Our blogs, webinars, case studies, and white papers enable all the stakeholders in the cloud computing sphere.
To get started, go through our Consultancy page and Managed Services Package, CloudThat’s offerings.
FAQs
1. How is AWS CodePipeline triggered?
ANS: – When you build an AWS CodePipeline, it will immediately start a run to publish the most recent version of your source code. After that, a fresh run is started each time your source location is modified.
2. How does AWS charge AWS CodePipeline?
ANS: – Each active pipeline on AWS CodePipeline costs $1 per month. Pipelines are free for the first 30 days following creation to promote experimentation. A pipeline is considered active if it has existed for more than 30 days and has had at least one code modification during the month. Pipelines with no new code modifications passing through them for the whole month are free. A pipeline that is in use is not discounted for partial months.
WRITTEN BY Imraan Pattan
Imraan is a Software Developer working with CloudThat Technologies. He has worked on Python Projects using the Flask framework. He is interested in participating in competitive programming challenges and Hackathons. He loves programming and likes to explore different functionalities for creating backend applications.
Venkatesh Uppalapati
Sep 18, 2023
Very useful information.
Click to Comment