CloudFormation Git Sync & GitLab Pipeline

This is a continuation of the previous projects for Adding Numbers with API Gateway, Lambda and DynamoDB. That mini project was more about the AWS resources. This project is to set up CloudFormation to use GitLab sync and deploy the same CloudFormation Template, but using GitLab as the repo in a more managed and structured method than simply uploading a yaml file to the CloudFormation console interface.

This will walkthrough a step by step of how I got this to work with my own GitLab account and the AddingNumbers repo.

Create Connection to Git (Personal not AWS GitLab)

In the AWS console navigate to CodeCommit >>> Settings >>> Connections

Create the new connection, name it and select your supported Git provider

This then takes you to login, there is an app to authorise

Approve the app to connect GitLab to AWS

New Connection is present

Link a Git Repo to CloudFormation

CloudFormation >>> Create Stack >>> Sync from Git
Link a Git Repo

Choosing the Repo

Select the Git connection created in the previous step
Select the repo
Select the Branch

Deployment File Path

This is not the actual cfn template. It is a file to store the parameters. CloudFormation will actually create this file inside the Git repo. There will be a merge request for it

IAM Role

This IAM role is full of permissions for CloudFormation and to codeconnections

AWS CodeConnections integrates with third party Git-based source providers such as GitHub, GitLab, and Bitbucket, and enables AWS services such as AWS CodePipeline, Amazon CodeWhisperer, and AWS CloudFormation to get notified on repository events, and download the source code to build, test and deploy code.
https://aws.amazon.com/about-aws/whats-new/2024/03/aws-codeconnections-formerly-codestar-connections/

Deployment File Parameters

This is the cfn file that will create the template

Configure Stack Options

Permissions Role (Optional), but doesn’t seem to work without in my experience.
I created a new role that gives CloudFormation full access to some basic resources that are enough for this example.
Cfn will assume this role when creating the resources

Stack Creation

It’s just like any other really if it works.
Connects to GitLab, creates a new merge request, pending manual interaction. GitLab pipeline changes to automate it all.

Troubleshooting

Delete Repo

This happens when there is a linked repo and the Git connection is deleted. There are stale repo links. So if readding, you can’t.
Cannot be done from the GUI. CLI only and seems to only be on CloudShell
https://docs.aws.amazon.com/dtconsole/latest/userguide/repositorylinks-list.html

How to Fix

List repos and find the one that needs to be deleted if there are multiple

Use the repo link ID

Nothing is returned for the delete action

Empty repo now

Delete Sync Configuration

I had a situation where I could no longer use CloudFormation due to a sync configuration needing to be deleted. I was also unable to delete the repo link as in the previous troubleshooting step.
https://docs.aws.amazon.com/dtconsole/latest/userguide/syncconfigurations-delete.html

How to Fix

GitLab Auto Merge CloudFormation Branch

Create and Access Token

Create a variable

Create a .gitlab-ci.yml file

Leave a Comment

Your email address will not be published. Required fields are marked *