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 a leading provider of Cloud Training and Consulting services with a global presence in India, the USA, Asia, Europe, and Africa. Specializing in AWS, Microsoft Azure, GCP, VMware, Databricks, and more, the company serves mid-market and enterprise clients, offering comprehensive expertise in Cloud Migration, Data Platforms, DevOps, IoT, AI/ML, and more.
CloudThat is the first Indian Company to win the prestigious Microsoft Partner 2024 Award and is recognized as a top-tier partner with AWS and Microsoft, including the prestigious ‘Think Big’ partner award from AWS and the Microsoft Superstars FY 2023 award in Asia & India. Having trained 650k+ professionals in 500+ cloud certifications and completed 300+ consulting projects globally, CloudThat is an official AWS Advanced Consulting Partner, Microsoft Gold Partner, AWS Training Partner, AWS Migration Partner, AWS Data and Analytics Partner, AWS DevOps Competency Partner, AWS GenAI Competency Partner, Amazon QuickSight Service Delivery Partner, Amazon EKS Service Delivery Partner, AWS Microsoft Workload Partners, Amazon EC2 Service Delivery Partner, Amazon ECS Service Delivery Partner, AWS Glue Service Delivery Partner, Amazon Redshift Service Delivery Partner, AWS Control Tower Service Delivery Partner, AWS WAF Service Delivery Partner, Amazon CloudFront, Amazon OpenSearch, AWS DMS, AWS Systems Manager, Amazon RDS, and many more.
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
Comments