2-stage CICD pipeline using Terraform
Gbenga Ojo-Samuel
Posted on January 10, 2024
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
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.
Ec2.sh file : This is a user data file for our instance. It automatically downloads CodeDeploy agent on our instance at launch.
Main.tf file: This file consists of the detailed infrastructure to be deployed as extracted in each AWS service folder from the Modules folder.
Output.tf file: This file consists of the outputs that will be displayed on our terminal after running terraform Apply command.
terraform.tf file: This file specifies the terraform provider and version in use.
terraform.tfvars file: This file allows us to pass our variable values with putting them directly in the code.
variable.tf file: This file shows the list of the variables used in the project.
Initializing Terraform
Change directory into the project folder, open integrated terminal and run terraform init command.
Run terraform validate to confirm that our configurations are valid.
Run terraform plan to display the plan of the infrastructures that we be deployed.
Run terraform apply to deploy the infrastructure. Ensure you approve with a yes.
Let’s confirm our infrastructures from the AWS console.
Ec2 instance
CodeCommit Repository
CodeDeploy Application and Deployment Group
Note that the code pipeline failed because our repository is still empty without any code to deploy.
To update our CodeCommit Repository with Codes.
Copy the repository url from the output of the terraform apply command as seen below.
Clone the repository in your project folder using the git clone command.
CD into the repository folder and copy the application files it.
Stage, commit and push the files to the remote repository.
Let’s confirm from the AWS console that the codes have been pushed successfully.
The CodePipeline is also triggered simultaneously and successful this time.
Run the terraform output command to get our server public dns endpoint.
Copy and past on your web browser to display the web page.
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!!!
Stage, Commit and Push the changes to the remote CodeCommit repository
This automatically triggers the pipeline
Refresh your browser to see the change.
Congratulations!!! the pipeline is working as designed.
Clean Up
Run terraform destroy to remove all infrastructures deployed to avoid unexpected bills
Posted on January 10, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.