Building a CI/CD Pipeline with GitHub Actions for AWS CloudFormation

Photo by EJ Strat on Unsplash

Building a CI/CD Pipeline with GitHub Actions for AWS CloudFormation

Introduction

In today’s fast-paced development world, Continuous Integration and Continuous Deployment (CI/CD) pipelines are essential for automating code validation, testing, and deployment. In this blog, I’ll walk you through building a CI/CD pipeline using GitHub Actions to validate and deploy AWS CloudFormation templates for an S3 bucket. This project demonstrates how to integrate cloud infrastructure validation directly into your development workflow.


The Workflow

1. Setting Up the Repository

The repository contains:

  • CloudFormation templates in a cloudformation/ directory.

  • A GitHub Actions workflow defined in .github/workflows/validate-cfn.yml.

2. Workflow Overview

Step 1: Validate CloudFormation Templates

When a developer submits a PR, the pipeline:

  • Checks out the code.

  • Validates the CloudFormation template using the AWS CLI:

      aws cloudformation validate-template --template-body file://cloudformation/s3-bucket.yml
    

Step 2: Deploy a Test Stack

If validation passes, the pipeline deploys a temporary CloudFormation stack using the create-stack command:

aws cloudformation create-stack \
  --stack-name pr-test-stack-${{ github.event.pull_request.number }} \
  --template-body file://cloudformation/s3-bucket.yml \
  --parameters ParameterKey=Environment,ParameterValue=test

This creates a stack named after the pull request number for easy tracking.

Step 3: PR Commenting

After deployment, the pipeline posts a comment on the PR with the stack name and status, enabling reviewers to know the test environment is ready.

Step 4: Cleanup on Merge

Once the PR is merged, another workflow job (cleanup-on-merge) triggers, deleting the temporary stack to free up resources:

aws cloudformation delete-stack --stack-name pr-test-stack-${{ github.event.pull_request.number }}

Challenges Faced

  1. Template Validation Failures:

    • Misconfigured Tags in the CloudFormation template caused initial validation errors.

    • Solution: Adjusted the template to use BucketTagging for S3 bucket tags.

  2. Branch Deletion After PR Merge:

    • Accidentally deleted the feature branch remotely before fixes could be pushed.

    • Solution: Recreated the branch locally or deleted it if no further updates were needed.

  3. Debugging CI Failures:

    • Investigating CI errors required careful log reviews and iterations to fine-tune the pipeline.

Key Takeaways

  1. Automated Validation Saves Time: Automating CloudFormation validation ensures that errors are caught early in the development process.

  2. Dynamic Environments Facilitate Testing: Temporary stacks allow for isolated and repeatable testing of infrastructure changes.

  3. Resource Cleanup Reduces Costs: Automating resource cleanup prevents accidental charges for unused infrastructure.


Code Repository

You can find the full code and configuration in my GitHub repository: GitHub Link.


Conclusion

This project taught me the power of integrating CI/CD workflows into infrastructure development. By leveraging GitHub Actions and AWS, I created a pipeline that streamlines the development process, ensures infrastructure reliability, and maintains cost efficiency.

Whether you're building your first pipeline or looking to refine an existing process, automating infrastructure validation is a game-changer. Give it a try!