Introduction****
Infrastructure as Code (IaC) has transformed how we build and manage cloud infrastructure. Tools like Terraform allow us to define infrastructure in code, but in real-world DevOps environments, writing code is only the beginning.
The real power comes when we automate infrastructure deployment using CI/CD pipelines.
In this blog, we will take Terraform beyond local execution and build a fully automated AWS infrastructure deployment pipeline.
Why CI/CD for Terraform?
Running Terraform manually is fine for learning, but in production environments it creates challenges:
Manual execution increases human errors
No proper review or approval process
No consistency across environments
Difficult to track changes
CI/CD pipelines solve these problems by introducing:
Automated validation and testing
Controlled deployment workflow
Approval mechanisms
Version-controlled infrastructure deployment
Project Goal
We will build a simple DevOps workflow where:
Infrastructure code is stored in GitHub
Every code push triggers a CI/CD pipeline
Terraform automatically validates and plans infrastructure
AWS resources are deployed after approval
Architecture Overview
Developer
|
v
GitHub Repository
|
v
CI/CD Pipeline (Jenkins / GitHub Actions)
|
+--> terraform fmt
+--> terraform validate
+--> terraform plan
+--> manual approval (optional)
+--> terraform apply
|
v
AWS Cloud Infrastructure (EC2 / VPC / S3)
Tools Used
Terraform (Infrastructure as Code)
AWS (Cloud Platform)
Jenkins / GitHub Actions (CI/CD Pipeline)
Git & GitHub (Version Control)
Linux (Execution Environment)
Step 1: Terraform Project Structure
A clean project structure improves readability and scalability:
terraform-project/
│
├── provider.tf
├── main.tf
├── variables.tf
├── outputs.tf
└── backend.tf
Step 2: Writing Terraform Configuration
Let’s create a simple EC2 instance using Terraform.
</> hcl
provider "aws" {
region = "ap-south-1"
}
resource "aws_instance" "dev_server" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "DevOps-Server"
}
}
Step 3: Remote State Management (Best Practice)
Instead of storing state locally, we use remote backend storage.
terraform {
backend "s3" {
bucket = "my-terraform-state-bucket"
key = "dev/terraform.tfstate"
region = "ap-south-1"
}
}
Why remote state?
Prevents state loss
Enables team collaboration
Provides locking mechanism
Ensures consistency across environments
Step 4: CI/CD Pipeline (Jenkins Example)
We now automate Terraform using a Jenkins pipeline.
</> groovy
pipeline {
agent any
stages {
stage('Checkout') {
steps {
git branch: 'main', url: 'https://github.com/your-repo.git'
}
}
stage('Terraform Init') {
steps {
sh 'terraform init'
}
}
stage('Terraform Format') {
steps {
sh 'terraform fmt'
}
}
stage('Terraform Validate') {
steps {
sh 'terraform validate'
}
}
stage('Terraform Plan') {
steps {
sh 'terraform plan'
}
}
stage('Approval') {
steps {
input message: 'Approve Terraform Apply?'
}
}
stage('Terraform Apply') {
steps {
sh 'terraform apply -auto-approve'
}
}
}
}
Step 5: AWS Authentication in Pipeline
To allow Jenkins to deploy to AWS:
Configure AWS credentials securely in Jenkins
Or use IAM roles (recommended for production environments)
⚠️ Never hardcode AWS credentials in code.
Step 6: End-to-End Workflow
Here’s what happens in a real DevOps pipeline:
Developer pushes Terraform code to GitHub
Jenkins detects the change
CI/CD pipeline starts execution
Terraform initializes infrastructure
Code is validated
Execution plan is generated
Manual approval is requested
Infrastructure is deployed to AWS
Key DevOps Concepts Covered
This project demonstrates:
Infrastructure as Code (Terraform)
Continuous Integration / Continuous Deployment
Automated cloud provisioning
Version-controlled infrastructure
Approval-based deployment workflow
Common Issues and Fixes
Terraform State Conflicts
✔ Fix: Use S3 backend with state locking (DynamoDB)AWS Permission Issues
✔ Fix: Proper IAM roles and policiesPipeline Failures
✔ Fix: Always test Terraform locally before CI/CD
Why This Matters in Real DevOps
In modern DevOps environments:
Infrastructure is never deployed manually
Every change goes through pipelines
Everything is traceable and auditable
Automation reduces human error
This workflow is exactly how production systems are managed.
Conclusion
This project demonstrates how Terraform evolves from a simple infrastructure tool into a production-grade automation system when combined with CI/CD pipelines.
Top comments (0)