2-stage CICD pipeline using Terraform

gbenga700

Gbenga Ojo-Samuel

Posted on January 10, 2024

2-stage CICD pipeline using Terraform

Intoduction
In my previous article, we explored the capabilities of automated code delivery, elevating our development workflow with precision and ease through AWS CI/CD developer tools. In that tutorial, we successfully implemented a two-stage CI/CD pipeline using AWS CodePipeline, CodeCommit, and CodeDeploy, all managed seamlessly through the AWS Management Console.
https://dev.to/gbenga700/implementing-a-two-stage-codepipeline-with-codecommit-and-codedeploy-5fak
Now, let's take it a step further by demonstrating how we can achieve the same application deployment using Terraform, an Infrastructure as Code (IaC) tool. This approach not only provides us with reusable code for deployment but also introduces additional flexibility and control over our infrastructure. For brevity, we will zoom in on the implementation of Terraform modules.
To access the complete Terraform code, you can find the repository in the link below. Dive into the modules, where the core infrastructure logic resides, providing a clear and concise representation of our deployment strategy.
https://github.com/7hundredtech/Terraform_AWS_Codepipleine/tree/main/terraform

Overview of my project folder

Image description

Modules folder: This folder consists of all the terraform codes for all the services required from this project. Please see the repo link provided above for details.

Image description
Ec2.sh file : This is a user data file for our instance. It automatically downloads CodeDeploy agent on our instance at launch.

Image description
Main.tf file: This file consists of the detailed infrastructure to be deployed as extracted in each AWS service folder from the Modules folder.

Image description
Output.tf file: This file consists of the outputs that will be displayed on our terminal after running terraform Apply command.

Image description
terraform.tf file: This file specifies the terraform provider and version in use.

Image description

terraform.tfvars file: This file allows us to pass our variable values with putting them directly in the code.

Image description
variable.tf file: This file shows the list of the variables used in the project.

Image description

Initializing Terraform
Change directory into the project folder, open integrated terminal and run terraform init command.

Image description
Run terraform validate to confirm that our configurations are valid.

Image description
Run terraform plan to display the plan of the infrastructures that we be deployed.

Image description

Image description
Run terraform apply to deploy the infrastructure. Ensure you approve with a yes.

Image description

Image description

Image description
Let’s confirm our infrastructures from the AWS console.

Ec2 instance

Image description

CodeCommit Repository

Image description
CodeDeploy Application and Deployment Group

Image description

Image description
IAM Roles

Image description

Image description
CodePipeline

Image description

Note that the code pipeline failed because our repository is still empty without any code to deploy.

Image description
To update our CodeCommit Repository with Codes.
Copy the repository url from the output of the terraform apply command as seen below.

Image description

Clone the repository in your project folder using the git clone command.

Image description
CD into the repository folder and copy the application files it.

Image description
Stage, commit and push the files to the remote repository.

Image description

Image description
Let’s confirm from the AWS console that the codes have been pushed successfully.

Image description

The CodePipeline is also triggered simultaneously and successful this time.

Image description

Run the terraform output command to get our server public dns endpoint.

Image description
Copy and past on your web browser to display the web page.

Image description

Lets verify the pipeline is working by effecting a change in our code.
Change the background colour in the index.html file to #FF0000 “red” and “Congratulations” to Pipeline deployed using terraform codes!!!

Image description

Image description
Stage, Commit and Push the changes to the remote CodeCommit repository

Image description

This automatically triggers the pipeline

Image description

Image description
Refresh your browser to see the change.

Image description
Congratulations!!! the pipeline is working as designed.

Clean Up
Run terraform destroy to remove all infrastructures deployed to avoid unexpected bills

Image description

💖 💪 🙅 🚩
gbenga700
Gbenga Ojo-Samuel

Posted on January 10, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related