Deploy AWS SAM Applications with GitLab CI/CD Pipeline

In this article we are going to cover how to Deploy AWS SAM Applications with GitLab CI/CD Pipeline

AWS Serverless Application Model (SAM) simplifies the deployment of serverless applications on AWS. Automating this process with GitLab CI/CD ensures consistent, repeatable, and efficient deployments. This guide walks you through setting up a GitLab CI/CD pipeline to deploy an AWS SAM application, covering prerequisites, setup, and troubleshooting steps.

Prerequisites

Before setting up the GitLab CI/CD pipeline, ensure you have the following:

  • An AWS Account with necessary permissions.
  • AWS SAM CLI installed.
  • GitLab Repository with your AWS SAM application.

Below is the Workflow Diagram:

Deploy AWS SAM Applications with GitLab CI/CD Pipeline 1

Install Dependencies packages on Ubuntu 24.04 LTS

Install Unzip on Ubuntu 24.04 LTS

sudo apt install unzip

Install AWS SAM CLI on Ubuntu 24.04 LTS:

curl -Lo aws-sam-cli-linux-x86_64.zip https://github.com/aws/aws-sam-cli/releases/latest/download/aws-sam-cli-linux-x86_64.zip
unzip aws-sam-cli-linux-x86_64.zip -d sam-installation
sudo ./sam-installation/install

Setting Up AWS SAM Application with Gitlab

Step #1:Initialize a New AWS SAM Project

Create a Repository in Gitlab:

Deploy AWS SAM Applications with GitLab CI/CD Pipeline 2

Set up a fresh AWS SAM project in the repository directory:

sam init

Select the following options:

Deploy AWS SAM Applications with GitLab CI/CD Pipeline 3

You can see new files in the repository:

Deploy AWS SAM Applications with GitLab CI/CD Pipeline 4

Step #2:GitLab CI/CD Pipeline AWS SAM Application

Create a .gitlab-ci.yml file in your repository root:

image: public.ecr.aws/sam/build-python3.13

variables:
  AWS_REGION: "ap-south-1"
  SAM_CLI_TELEMETRY: 0

stages:
  - build
  - deploy

before_script:
  - echo "Verifying AWS SAM installation..."
  - sam --version

build:
  stage: build
  script:
    - echo "Building the AWS SAM application..."
    - sam build
  artifacts:
    paths:
      - .aws-sam

deploy:
  stage: deploy
  script:
    - echo "Configuring AWS CLI..."
    - 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

    - echo "Deploying the AWS SAM application..."
    - sam deploy --stack-name my-sam-app --resolve-s3 --capabilities CAPABILITY_IAM --region $AWS_REGION --no-confirm-changeset
Deploy AWS SAM Applications with GitLab CI/CD Pipeline 5

Step #3:Configuring GitLab CI/CD Variables

  1. Go to GitLab Repository → Settings → CI/CD → Variables.
  2. Add the following masked and protected variables:
    • AWS_ACCESS_KEY_ID (Your AWS access key)
    • AWS_SECRET_ACCESS_KEY (Your AWS secret key)
Deploy AWS SAM Applications with GitLab CI/CD Pipeline 6

Step #4:Pushing the Code to GitLab

Turn ON Force Push

Settings > Repository > Protected branches > Allowed to force push

Deploy AWS SAM Applications with GitLab CI/CD Pipeline 7

If your AWS SAM application directory is not already a Git repository, initialize it:

git init

Link your local repository to your GitLab repository:

git remote add origin https://gitlab.com/your-username/your-repo.git

Add and Commit Files

Add all the necessary files, including the AWS SAM application and .gitlab-ci.yml:

git add .
git commit -m "Initial commit - Add GitLab CI/CD for AWS SAM"

Push your changes to the main branch:

git branch -M main
git push -uf origin main

Step #5:Running the Pipeline in Gitlab

  1. Navigate to Build → Pipelines.
  2. You should see your pipeline running through build and deploy stages.
  3. If successful, your AWS SAM application will be deployed.
Deploy AWS SAM Applications with GitLab CI/CD Pipeline 8
Deploy AWS SAM Applications with GitLab CI/CD Pipeline 9

Step #6:Accessing the Deployed Application

Access the Application by using the link mentioned in the deploy stage as shown below:

Deploy AWS SAM Applications with GitLab CI/CD Pipeline 10

Below is the Output:

Deploy AWS SAM Applications with GitLab CI/CD Pipeline 11

You can also review the deployment details, including the resources created as part of the my-sam-app and aws-sam-cli-managed-default stacks in CloudFormation:

Deploy AWS SAM Applications with GitLab CI/CD Pipeline 12

Remove the resources (two stacks in CloudFormation) if they are no longer needed.

Conclusion:

By following this guide, you have successfully set up a GitLab CI/CD pipeline to automate AWS SAM application deployment.

You can now easily deploy, test, and update your serverless applications with GitLab CI/CD, improving efficiency and reliability in your development workflow.

Related Articles:

Deploy Serverless App with GitHub Actions and AWS SAM

Reference:

Using GitLab CI/CD Pipeline to Deploy AWS SAM Applications AWS page

Harish Reddy

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap