Voiced by Amazon Polly |
Introduction
In this blog, we will walk through the steps to set up a CI/CD pipeline using GitLab to deploy an AWS Lambda function. We will use a YAML file to configure the pipeline and ensure that your AWS Lambda function is deployed automatically whenever changes are pushed to your repository.
Pioneers in Cloud Consulting & Migration Services
- Reduced infrastructural costs
- Accelerated application deployment
Prerequisites
Before diving into the deployment process, make sure you have the following prerequisites in place:
- An AWS Account: You must access AWS Lambda and AWS IAM (Identity and Access Management).
- A GitLab Repository: This will host your AWS Lambda function code and the CI/CD pipeline configuration.
- AWS CLI: Ensure the AWS Command Line Interface (CLI) is installed and configured with the necessary permissions.
Step-by-Step Guide
Step 1: Generate AWS Access Key and Secret Key
To allow GitLab CI/CD to interact with AWS, you need to generate an Access Key and Secret Key. Here’s how:
- Log in to your AWS Management Console.
- Navigate to AWS IAM (Identity and Access Management).
- In the left panel, click Users.
- Select an existing user or create a new one with programmatic access.
- Click Security credentials and then Create access key.
- Add the necessary permissions to the user (e.g., AWSLambdaFullAccess).
- Copy and store the Access Key ID and Secret Access Key
These keys will be used to authenticate GitLab CI/CD with AWS. We will store them as environment variables in GitLab in a later step.
Step 2: Create an AWS Lambda Function
Let’s start by creating a basic Python-based AWS Lambda function. Here’s how:
- Create a directory for your Lambda function.
- Add a file named py with the following code:
1 2 3 4 5 6 7 |
import json def lambda_handler(event, context): return { 'statusCode': 200, 'body': json.dumps('Hello from Lambda!') } |
This simple AWS Lambda function returns a “Hello from Lambda!” message when invoked.
Step 3: Configure GitLab CI/CD
Next, we will configure the GitLab CI/CD pipeline using a .gitlab-ci.yml file. This file defines the stages and scripts for building and deploying your Lambda function.
Create a .gitlab-ci.yml file in the root of your repository with the following content:
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 28 29 |
stages: - build - deploy variables: AWS_ACCESS_KEY_ID: "<your-access-key>" AWS_SECRET_ACCESS_KEY: "<your-secret-key>" AWS_REGION: "us-east-1" before_script: - pip install awscli --upgrade --user build_lambda: stage: build script: - zip -r function.zip lambda_function.py artifacts: paths: - function.zip deploy_lambda: stage: deploy script: - aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID - aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY - aws configure set region $AWS_REGION - aws lambda update-function-code --function-name myLambdaFunction --zip-file fileb://function.zip only: - main |
Explanation of the .gitlab-ci.yml File
- Stages: The pipeline has two stages: build and deploy.
- Variables: AWS credentials and region are defined as variables.
- before_script: Upgrades the AWS CLI to the latest version.
- build_lambda: Packages the Lambda function into a ZIP file (zip).
- deploy_lambda: Deploys the ZIP file to AWS Lambda using the AWS CLI.
Step 4: Set Up GitLab Variables
For security reasons, storing sensitive information like AWS credentials as environment variables in GitLab is best. Here’s how to do it:
- Navigate to your GitLab repository.
- Go to Settings > CI/CD > Variables.
- Add the following variables:
- AWS_ACCESS_KEY_ID
- AWS_SECRET_ACCESS_KEY
- AWS_REGION
These variables will be used in the .gitlab-ci.yml file to authenticate with AWS.
Step 5: Commit and Push Code
Once everything is set up, commit your code and push it to your GitLab repository:
1 2 3 |
git add . git commit -m "Initial commit for Lambda deployment" git push origin main |
This will trigger the GitLab CI/CD pipeline. The pipeline will:
- Package the AWS Lambda function into a ZIP file.
- Deploy the ZIP file to AWS Lambda.
You can monitor the pipeline’s progress under the CI/CD > Pipelines section in GitLab.
Conclusion
By integrating AWS Lambda deployment with GitLab CI/CD, you can streamline your development workflow and ensure automated, error-free deployments. This setup not only saves time but also reduces the risk of human error during the deployment process.
You can further enhance your pipeline by adding testing stages, implementing rollback mechanisms, or using Infrastructure as Code (IaC) tools for better management. With this guide, you’re well on your way to mastering serverless deployments with AWS Lambda and GitLab CI/CD.
Drop a query if you have any questions regarding AWS Lambda 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 award-winning company and the first in India to offer cloud training and consulting services worldwide. As a Microsoft Solutions Partner, AWS Advanced Tier Training Partner, and Google Cloud Platform Partner, CloudThat has empowered over 850,000 professionals through 600+ cloud certifications winning global recognition for its training excellence including 20 MCT Trainers in Microsoft’s Global Top 100 and an impressive 12 awards in the last 8 years. CloudThat specializes in Cloud Migration, Data Platforms, DevOps, IoT, and cutting-edge technologies like Gen AI & AI/ML. It has delivered over 500 consulting projects for 250+ organizations in 30+ countries as it continues to empower professionals and enterprises to thrive in the digital-first world.
FAQs
1. How do I troubleshoot GitLab CI/CD pipeline failures?
ANS: – Check the pipeline logs in GitLab under CI/CD > Pipelines. Look for error messages and ensure your AWS credentials and region are correctly configured.
2. Can I deploy multiple AWS Lambda functions with the same pipeline?
ANS: – Yes, you can modify the .gitlab-ci.yml file to include multiple functions by adjusting the script commands to handle multiple deployments.

WRITTEN BY Sanket Gaikwad
Sanket is a Cloud-Native Backend Developer at CloudThat, specializing in serverless development, backend systems, and modern frontend frameworks such as React. His expertise spans cloud-native architectures, Python, Dynamics 365, and AI/ML solution design, enabling him to play a key role in building scalable, intelligent applications. Combining strong backend proficiency with a passion for cloud technologies and automation, Sanket delivers robust, enterprise-grade solutions. Outside of work, he enjoys playing cricket and exploring new places through travel.
Comments