Automating Serverless Lambda Deployments with GitLab CI/CD: A Step-by-Step Guide


Introduction: Bridging the Gap Between Code and Infrastructure

Serverless computing has revolutionized application deployment, enabling developers to focus on code without worrying about infrastructure management. AWS Lambda provides a powerful platform for running serverless applications, while GitLab CI/CD simplifies automated deployments. This guide bridges the gap between code and infrastructure, demonstrating how to deploy AWS Lambda functions seamlessly with GitLab CI/CD.


Prerequisites: Essential Tools for Seamless Deployment

Before diving into deployment, ensure you have the following tools and setups:

  • AWS Account: Active account with necessary permissions.

  • GitLab Account: Repository set up for your project.

  • AWS CLI: Installed and configured with access credentials.

  • Node.js and NPM: Installed for building and packaging Lambda functions.

  • Git: Installed for version control.

  • YAML Basics: Familiarity with YAML for GitLab CI/CD pipelines.


AWS Setup: Creating Users and Setting IAM Permissions

  1. Create an IAM User:

    • Go to the AWS Management Console.

    • Navigate to IAM > Users > Add User.

    • Provide a username and select Programmatic Access.

  2. Attach Policies:

    • Attach the following policies for Lambda deployments:

      • AWSLambdaFullAccess

      • AmazonS3FullAccess (if using S3 for code storage)

      • IAMFullAccess (for role creation if needed).

  3. Save Access Keys:

    • Save the Access Key ID and Secret Access Key for later use in GitLab.


GitLab Configuration: Storing Sensitive Credentials

  1. Securely Store AWS Credentials:

    • Go to Settings > CI/CD > Variables in your GitLab repository.

    • Add the following environment variables:

      • AWS_ACCESS_KEY_ID

      • AWS_SECRET_ACCESS_KEY

      • Optional: AWS_REGION (e.g., us-east-1).

  2. Set Permissions:

    • Ensure the variables are masked and protected to prevent exposure.


Automated Deployment Pipeline: Building, Testing, and Deploying with GitLab CI/CD

  1. Define .gitlab-ci.yml:

    • Create a .gitlab-ci.yml file in your repository root with the following content:

stages:

  - build

  - deploy


build:

  stage: build

  image: node:latest

  script:

    - npm install

    - zip -r function.zip .

  artifacts:

    paths:

      - function.zip


deploy:

  stage: deploy

  image: amazon/aws-cli:latest

  script:

    - aws lambda update-function-code --function-name myLambdaFunction --zip-file fileb://function.zip

  only:

    - main

  1. Customize the Pipeline:

    • Replace myLambdaFunction with your actual Lambda function name.

    • Add unit tests in the build stage for better reliability.


Deployment Verification: Locating Your Lambda Function in AWS

  1. Check AWS Lambda Console:

    • Log in to the AWS Management Console.

    • Navigate to Lambda > Functions.

    • Verify the updated deployment timestamp and function code.

  2. Test Your Function:

Use the Test tab in the Lambda console or invoke it via the AWS CLI:
aws lambda invoke --function-name myLambdaFunction out.json


Beyond the Basics: Exploring Further Deployment Possibilities

  • Environment Variables: Use Lambda environment variables for configuration.

  • Infrastructure as Code: Automate Lambda creation with AWS CloudFormation or Terraform.

  • Event Triggers: Explore Lambda event sources like S3, DynamoDB, or API Gateway.

  • Monitoring and Alerts: Use AWS CloudWatch to monitor Lambda performance.

References

Building a CI/CD pipeline for cross-account deployment of an AWS Lambda API with the Serverless Framework

Using GitLab CI/CD to deploy with AWS SAM


Comments

YouTube Channel

Follow us on X