Run IaC (CloudFormation) using a Custom Stage

Introduction

In this short tutorial, I’ll explain how to create a simple CloudFormation Stack with Harness native features.

What are we going to do?

We’ll perform these simple tasks:

  1. Launch a small EC2 with Docker Engine and Docker Compose
  2. Give this EC2 a good Instance Profile (IAM Role), so it’s able to do IaC work in your AWS Account
  3. Deploy the Docker Delegate in this EC2
  4. Create an AWS Connector that will use the Instance Profile permissions
  5. Deploy a very simple CloudFormation Stack using Harness

Very simple CF Example: IaC-CloudFormation-Harness/S3Bucket.yaml at main · gacerioni/IaC-CloudFormation-Harness · GitHub


Step By Step Guide

Step 0 - Launch the EC2

Instance system recommendation for Delegates: Install a Docker delegate | Harness Developer Hub

For this Lab, I’ll launch a t2.small EC2 instance.
For the Operating System, let’s pick Ubuntu 22.04 LTS, amd64 jammy image

Please make sure to give sufficient permissions in the IAM Role you will use for the EC2’s Instance Profile. For this Lab, I’ll use the OOTB [PowerUserAccess] Permission Policy.

Step 1 - Install Docker Engine (CE) and Docker Compose

To make this straightforward, I’ll summarize the official Docker docs, with some brief comments:

# Install some dependencies
sudo apt-get update
sudo apt-get -y install \
    ca-certificates \
    curl \
    gnupg \
    lsb-release

# Add Docker’s official GPG key
sudo mkdir -m 0755 -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

# Set up the pertinent repo
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# Install Docker Engine (CE)
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y

# Enable Docker Engine
sudo systemctl enable docker

# Install Docker Compose
sudo apt install docker-compose -y

# Test if the engine is working fine
sudo docker run hello-world

Step 2 - Deploy the Docker Delegate

You can follow this good documentation: Install a Docker delegate | Harness Developer Hub

In my case, this was pretty much it:

# Start the Delegate with Compose
sudo docker-compose -f docker-compose.yml up -d

# Check if the Delegate Container started properly
sudo docker ps
sudo docker logs <delegate_container_id>

image

Step 3 - Create an AWS Cloud Provider Connector

Instead of providing credentials directly, let’s tell Harness to use the EC2 Instance Profile:
I’ll create my Connector at the Account Resources level:

image

Connect via a Delegate:

Filter the Docker Delegate that holds the permissions to the target AWS Account:

Voilà:

Step 4 - Create a Pipeline with a Custom Stage (no CD licensing is required)

Ref: Provision with the CloudFormation Create Stack Step | Harness Developer Hub

You can create a Pipeline:
image

And then add a Custom Stage:

Then, you’ll have access to all 1st-class commands for CloudFormation, directly from Harness Step Library!
image

Then, I’ll configure the Create Stack command to:

  • Fetch the CloudFormation Stack Manifest from my GH Project (I have a GH Connector already)
  • Load the Parameters. For me, the BucketName

Then, just some extra steps to approve and then delete the stack.

The only trick I’ll do is to ask you to provide me the bucket name at runtime. Just to show you some Variables capabilities. No worries, I’ll share the Pipeline YAML.

So, the Pipeline looks like this:

Step 5 - Let’s test it!

Looks good!

image

Let me clean the lab:
image

And we good!
image

The Pipeline YAML EXAMPLE

https://raw.githubusercontent.com/gacerioni/IaC-CloudFormation-Harness/main/harness/cf-pipeline-example.yaml