A Step-by-Step Guide to CI/CD Pipelines with AWS CloudFormation and CodePipeline

Govind Kumar
axcess.io
Published in
4 min readNov 8, 2023

Deploying infrastructure manually can be a tricky and time-consuming process. Each step may require manual adjustments, leading to potential errors and inconsistencies. Moreover, once your infrastructure is deployed through AWS CloudFormation, there can be concerns about making changes due to worries about ‘drift.’ This is when your infrastructure changes over time, and you lose track of its state. However, our solution offers a remedy. By implementing a CI/CD pipeline with AWS CloudFormation, these challenges disappear. The pipeline automates the deployment process, ensuring a smoother and more efficient infrastructure setup. Plus, it helps you keep track of any changes, ensuring you can confidently make updates without fearing drift.

Setting Up Your CI/CD Pipeline:

Step 1: Editing and Uploading Artifacts to S3:

Before creating your CI/CD pipeline, proper preparation is essential. CodePipeline requires source files in a structured format, and using Amazon S3 as your source repository is a common practice.

Here’s how to do it right:

  • Customize Your Artifacts: Start by ensuring your source artifacts are structured correctly and contain all necessary components. Typically, these artifacts include an AWS CloudFormation template and relevant configuration files specific to your infrastructure.
  • Prepare Configuration Files: Modify the configuration files within your artifacts to match your specific requirements. These files typically contain key-value pairs that map to parameters in your AWS CloudFormation template. Customize these values as needed.
  • Package Artifacts: Once your source artifacts are customized, package them for use in your CI/CD pipeline. If you’re using Amazon S3 as your source repository, ensure that your source files are properly organized and zipped for efficient use in your pipeline.

Step 2: Creating the Pipeline Stack:

Now that your source artifacts are ready, it’s time to set up your CI/CD pipeline. To do this, you’ll use a sample AWS CloudFormation template that not only builds the pipeline but also configures essential services like AWS Identity and Access Management (IAM) roles, S3 buckets, and notification services.

Here’s how to go about it:

  • Download the Sample Template: Start by downloading a sample AWS CloudFormation template, which is designed to create the pipeline stack and its associated resources. Ensure that you’ve customized the parameters to match your specific use case.
  • Define the AWS CloudFormation Stack: Access the AWS CloudFormation console, select the appropriate AWS Region, and create a new stack. Specify the location of the downloaded template and provide parameter values that reflect your infrastructure’s requirements.
  • Create the Stack: Review the parameters, ensure the stack name and template URL are correct, and then create the stack. Acknowledge that AWS CloudFormation might create IAM resources as part of the setup.

Understanding the Pipeline:

Your CI/CD pipeline is structured into three stages, each containing specific actions.

Let’s break down what happens in each stage:

Stage 1: Source

The first stage of the pipeline, known as the source stage, is where you specify the location of your source code. Every time you push a revision to this location, CodePipeline reruns your pipeline. In this example, your source code is located in an S3 bucket, identified by its file name.

Stage 2: Test Stage

The Test Stage is where the pipeline creates a test stack, waits for your approval, and then deletes the test stack. During the CreateStack action, the pipeline uses the test configuration file and AWS CloudFormation template to create the test stack. If stack creation fails, the pipeline automatically replaces it, simplifying the process of iterating on test stacks.

The pipeline then pauses at the ApproveTestStack action, sending a notification to your email. You can use CodePipeline to approve or reject this action. Once approved, the pipeline proceeds to the DeleteTestStack action, which deletes the test stack and its resources.

Stage 3: Production Stage

In the Production Stage, the pipeline creates a change set against the existing production stack, waits for approval, and then executes the change set. The change set provides a preview of all modifications AWS CloudFormation will make to your production stack. If this is the initial run, and there’s no existing production stack, the change set will show the actions that AWS CloudFormation performed when creating the test stack.

The pipeline pauses at the ApproveChangeSet action, allowing you to view and approve the proposed modifications to the production stack. After approval, the ExecuteChangeSet action executes the change set, performing the actions described in it. In the initial run, this action creates the production stack. In subsequent runs, it updates the stack.

Conclusion:

In this blog, we’ve explored best practices for implementing CI/CD pipelines using AWS CloudFormation and CodePipeline. CI/CD is a game-changer for software development, and when applied to infrastructure, it becomes a powerful tool for achieving rapid, reliable, and efficient deployments. By following these best practices, you can harness the full potential of AWS services to streamline your infrastructure deployment workflows. Whether you’re working on web applications, microservices, or any other infrastructure, the principles outlined here will help you build a robust and efficient CI/CD pipeline.

--

--

Govind Kumar
axcess.io

Technology Evangelist | AWS Golden Jacket | Practice Lead Cloud Migration @Axcess IO | Cloud Arch. | RHC(SA/E) | AWS (DevOps/Sol. Arch) — Pro. | CCNA