AWS CloudFormation Git Sync
Tags: aws, cloudformation, git, tutorial
Published: 2024-01-18 18:01:25

**Infrastructure as Code (IaC)** has become the cornerstone of modern cloud management, empowering developers and DevOps teams to declaratively define and provision their cloud resources. CloudFormation is a leading IaC tool from Amazon Web Services (AWS), enabling you to define your infrastructure in templates and then automate its deployment and management. However, maintaining and updating these templates can be a time-consuming and error-prone task.

CloudFormation allows you to sync with your Git repo now, so there is no need for manual deployments, your infrastructure always reflects the latest version of your code.

**Let’s speak about the benefits of Git Sync:**

_Continuous Deployment for Infrastructure: Automate the deployment of your infrastructure changes with every commit or pull request to your Git repository._

_Improved Collaboration and Version Control: Leverage the power of Git for collaboration and version control._

_Simplified Infrastructure Management: Automate infrastructure updates and reduce the burden of manual deployments. You don’t need GitHub Action to deploy to AWS._

**Let’s start with Git Sync:**

_Create a Git Repository: Initialize a new Git repository to store your CloudFormation templates._

_Configure CloudFormation Git Sync._

_Create a CloudFormation Stack._

_Deploy Infrastructure Changes: Commit your changes to your Git repository, and CloudFormation Git Sync will automatically deploy the updated infrastructure._

**Let’s discuss the points 2 and 3.**

**Configuring Git SYNC in CloudFormation**

- Firstly, you need to create a connection. As you can see, there are several repository providers.


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

_Once the connection is ready, you need 2 roles:_


- The role is to update the stack from Git. Use this link to create a role: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/git-sync-prereq.html


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

- CF role to interact with AWS resources. You can add an admin policy just for testing purposes.


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

Once roles are ready, let’s create a stack:


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

As you can see, the Git sync status is enabled. My deployment-file.yaml looks this way:


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

So Git will constantly monitor changes in vpc.yaml file. Let’s add some SG to our template:


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

Now, CF after a few seconds after commit will create a changeset:


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

Finally, we will check a template in CF console, our template is updated and SG was successfully created:


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


**Conclusion**

**CloudFormation Git Sync** is a valuable tool for streamlining your infrastructure automation and deployment processes. By integrating your CloudFormation templates with your Git repository, you can automate infrastructure updates, improve collaboration, and simplify infrastructure management. Embrace the power of continuous deployment for your infrastructure and experience the benefits of enhanced agility and reduced risk.

Thank you for your time.

Cheers!