Automate CloudFormation deployments from GitHub
Tags: aws, cloudformation, github, devops
Published: 2024-01-21 05:53:28


- **AWS CloudFormation**,(IaC) lets you provision, and manage AWS and third-party resources, now offers Git sync support. 
- This feature automatically triggers deployments upon updates to a tracked Git repository. The integration, compatible with **GitHub, GitHub Enterprise, GitLab, and Bitbucket**.
- CloudFormation development cycles by seamlessly integrating with **Git workflows**, reducing time lost to context switching.

## Key Highlights:

1. **Git Sync Integration:** AWS CloudFormation now supports Git sync, triggering automatic deployments on Git repository updates.

2. **Compatibility:** GitHub, GitHub Enterprise, GitLab, and Bitbucket are directly integrated with AWS CloudFormation.

3. **Development Experience:** Explore a modern development experience utilizing GitHub's native tools and CloudFormation's Git sync integration.

4. **Cloud Development Environment:** Create a cloud development environment using GitHub CodeSpaces.

5. **Integration with GitHub Actions:** Integrate direct feedback into Pull Requests using GitHub Actions and the CloudFormation Linter.


## Requirements:

1. **[AWS Account:](https://aws.amazon.com/)** Ensure you have an AWS account for deploying S3 Bucket.

2. **[GitHub Account:](https://github.com/)** Access to GitHub Codespaces and Actions is necessary.

## GIT Setup

Step 1: Creating an empty Git Repository


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bubf36quft0dtd4semme.png)


Step 2: Clone repository in your workspace and you can install the linter.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b03nmn0px1xbr673r9w0.png)

Next, you’ll create your template in the base directory called s3.yaml

Below template will create s3 bucket.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9ge2kkdphoabtam0ix44.png)




You can verify the template gives no errors by running cfn-lint in the terminal and verifying it returns no errors.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j8rjft5yco0mkbczqv08.png)



Please install package if cf-lint is not installed
```
pip3 install cfn-lint

```

Step 3: Need to add the deployment file

1. Git sync supports a [deployment file](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/git-sync-concepts-terms.html?icmpid=docs_console_unmapped#git-sync-terms-depoyment-file) to provide flexibility for managing CloudFormation stacks from within a Git repository.

2. This config file manages the location of the template file, and any parameters or tags you may be interested in using.

3. You’ll be creating a new file called deployment-file.yaml in your repository. Since this stack doesn’t have parameters or tags, it’ll be relatively simple:

```
template-file-path: ./s3.yaml

```
Your code working directory look look this

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m0vc93aa3hs5dad8twvv.png)




## AWS Setup

First, [create the role](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create_for-service.html) that will deploy our CloudFormation template. Be sure to note the name you select for this as you’ll be using it to manage your stack later. This example uses s3-deployment-iam-role.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/px0g0096vermx3tu0deu.png)


## Creat stack.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4aby0ihv7y4up4z1mtay.png)

Here, you can see the new option to select Sync from Git template source, which you can configure on the next screen. Since you already created your stack deployment file, you can select I am providing my own file in my repository.

Select GitHub, your connection, the repository, and branch, the deployment file location.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mzw9fp78rrukinalzmc7.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/imje6sfvsel9tbbhgvmq.png)



Finally, you will select New IAM Role to create a service managed role

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q2lbb2zk61fm9j6ng0fb.png)



Finally, you can see the status of your sync in the new “Git sync” tab, including the configuration you provided earlier as well as the status of your sync, your previous deployments, and the option to retry or disconnect the sync if needed.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xpfpp3y3xc7k1ugh6lzv.png)


## FINAL RESULT:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yq8viu1rw5lahumxfl1v.png)

I updated the GitHub repository by changing the bucket name from 'cf-s3-git-bucket-v1' to 'cf-s3-git-bucket-v2'."


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/go0s080lq5ryjhoqgbut.png)

"After updating the GitHub repository, the modified bucket name 'cf-s3-git-bucket-v2' will automatically trigger the deployment of your stack. This enhancement reflects a resilient and **scalable CI/CD** system designed for efficient management of your infrastructure as code."

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ec1nkkgvvm4jfj5a1p3w.png)


Thanks for reading. Happy learning