Day 3: Terraform - Automating AWS Infrastructure with Infrastructure as Code
pipelines work with the same Terraform infrastructure
Day 3: Terraform — Automating AWS Infrastructure with Infrastructure as Code
As part of my DevOps learning journey, Day 3 was focused on Terraform and the concept of Infrastructure as Code (IaC).
After exploring AWS DevOps fundamentals, Amazon EKS, and GitLab CI/CD, the next logical step was understanding how cloud infrastructure itself can be automated.
Instead of manually creating AWS resources through the console, Terraform allows us to define infrastructure using code, review those changes, version them, and deploy them consistently.
What is Infrastructure as Code?
Infrastructure as Code means managing infrastructure through configuration files instead of manually creating resources.
For example, instead of manually creating a VPC, subnets, IAM roles, ECR repositories, or an EKS cluster from the AWS Console, we can define them using Terraform.
A simplified workflow looks like this:
Terraform Code
↓
Terraform Plan
↓
Review Changes
↓
Terraform Apply
↓
AWS InfrastructureThis approach makes infrastructure more:
- Repeatable
- Consistent
- Version controlled
- Reviewable
- Scalable
- Automated
Why Terraform?
Terraform provides a declarative approach to infrastructure management.
We describe the desired state, and Terraform determines what changes are required to reach that state.
For example:
Desired State
↓
VPC
├── Public Subnets
├── Private Subnets
├── Route Tables
├── Security Groups
└── IAMTerraform compares this configuration with the existing infrastructure and creates, updates, or removes resources as required.
This becomes especially powerful when Terraform is integrated with CI/CD.
Terraform Providers and Resources
Terraform uses providers to interact with cloud platforms and other services.
For AWS, we commonly use the AWS provider.
A simplified configuration looks like:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
}
}
provider "aws" {
region = "ap-south-1"
}Then we can define AWS resources.
For example:
resource "aws_s3_bucket" "example" {
bucket = "my-example-bucket"
}The provider tells Terraform how to communicate with AWS, while the resource describes what we want Terraform to create.
Variables and Outputs
Hardcoding values everywhere makes infrastructure difficult to maintain.
Terraform variables allow us to make configurations reusable.
Example:
variable "aws_region" {
type = string
default = "ap-south-1"
}We can then reference the variable:
provider "aws" {
region = var.aws_region
}Outputs allow Terraform to expose useful information after deployment.
For example:
output "vpc_id" {
value = aws_vpc.main.id
}This becomes especially useful when different Terraform modules or deployment stages need to share information.
Terraform Modules
As infrastructure grows, putting everything into one Terraform file becomes difficult to maintain.
Modules help organize infrastructure into reusable components.
For example:
terraform/
│
├── modules/
│ ├── vpc/
│ ├── iam/
│ ├── ecr/
│ └── eks/
│
└── environments/
├── dev/
├── qa/
└── prod/A module can encapsulate a complete infrastructure component.
For example:
VPC Module
↓
VPC
├── Public Subnets
├── Private Subnets
├── Route Tables
└── NAT / Internet ConnectivityThis makes infrastructure easier to reuse across environments.
The Terraform Workflow
One of the most important things I focused on was understanding the Terraform workflow.
1. terraform init
terraform initThis initializes the Terraform working directory and downloads the required providers and modules.
2. terraform validate
terraform validateThis checks whether the Terraform configuration is syntactically valid and internally consistent.
3. terraform plan
terraform planThis shows what Terraform intends to change.
For example:
Plan:
+ Create VPC
+ Create Subnet
+ Create ECR Repository
~ Update IAM Role
- Destroy ResourceThis is one of the most important safety steps because we can review the proposed changes before applying them.
4. terraform apply
terraform applyThis applies the planned changes and creates or modifies the AWS infrastructure.
The overall process is:
terraform init
↓
terraform validate
↓
terraform plan
↓
Review
↓
terraform apply
↓
AWS Infrastructure
Understanding Terraform State
Terraform needs to know what infrastructure it manages.
This information is stored in the Terraform state.
The state allows Terraform to understand the relationship between:
Terraform Configuration
↓
Terraform State
↓
Real AWS InfrastructureFor example:
Terraform Code
↓
aws_vpc.main
↓
Terraform State
↓
Actual AWS VPCWithout properly managing state, collaboration and infrastructure management can become difficult.
Remote State with Amazon S3
For team environments, storing Terraform state only on a developer's local machine is not a good approach.
A common AWS architecture is to store Terraform state remotely in Amazon S3.
Developer
↓
GitLab CI/CD
↓
Terraform
↓
Amazon S3
↓
Terraform StateRemote state allows multiple engineers and CI/CD pipelines to work with the same infrastructure state.
It also provides a central location for state management.
State Locking
When multiple people or pipelines work with the same Terraform infrastructure, two operations should not modify the same state simultaneously.
State locking helps prevent conflicting Terraform operations.
Conceptually:
Pipeline A
↓
Lock State
↓
Apply Infrastructure
↓
Unlock State
Pipeline B
↓
WaitThis helps reduce the risk of concurrent Terraform operations causing state conflicts.
Terraform + GitLab CI/CD
Terraform becomes even more powerful when integrated with GitLab CI/CD.
Instead of running Terraform manually from a laptop, the pipeline can execute Terraform automatically.
A simplified architecture looks like:
Developer
↓
GitLab
↓
GitLab CI/CD
↓
GitLab Runner
↓
Terraform
↓
AWSA pipeline could contain stages such as:
Validate
↓
Plan
↓
Review / Approval
↓
ApplyFor production environments, an approval step can be introduced before
terraform apply .This provides an additional layer of control.
Terraform and AWS Architecture
Terraform can be used to provision many of the AWS components required for a modern application.
For example:
Terraform
|
+---------------+---------------+
| | |
↓ ↓ ↓
VPC ECR EKS
| | |
Subnets Docker Images Kubernetes
| |
+---------------+---------------+
|
ApplicationA larger architecture could include:
AWS
│
├── VPC
│ ├── Public Subnets
│ ├── Private Subnets
│ └── Route Tables
│
├── IAM
│ ├── Roles
│ └── Policies
│
├── ECR
│ └── Container Images
│
└── EKS
├── Control Plane
├── Nodes
└── ApplicationsAll of these resources can be managed through Terraform.
The Biggest Lesson
The biggest takeaway from Day 3 was simple:
Don't manually create infrastructure when it can be defined, reviewed, versioned, and deployed as code.
Terraform changes the way we think about infrastructure.
Instead of:
Open AWS Console
↓
Create Resource
↓
Configure Resource
↓
Repeat Manuallywe can move toward:
Write Terraform
↓
Git Commit
↓
GitLab CI/CD
↓
Terraform Plan
↓
Review
↓
Terraform Apply
↓
AWS InfrastructureThis makes infrastructure deployment more predictable and repeatable.
What I Learned on Day 3
During Day 3, I focused on:
- Infrastructure as Code
- Terraform providers
- Terraform resources
- Variables
- Outputs
- Modules
- Terraform initialization
- Validation
- Planning
- Applying infrastructure
- Terraform state
- Remote state
- State locking
- Terraform with GitLab CI/CD
- AWS infrastructure automation
The most important thing wasn't memorizing Terraform commands.
It was understanding how Terraform fits into the larger DevOps workflow.
Complete DevOps Flow
My learning journey is now starting to connect together:
Developer
↓
GitLab
↓
GitLab CI/CD
↓
GitLab Runner
↓
Terraform
↓
AWS Infrastructure
↓
Docker
↓
Amazon ECR
↓
Amazon EKS
↓
ApplicationEach technology has a specific responsibility.
GitLab manages source code and CI/CD.
GitLab Runner executes pipeline jobs.
Terraform manages infrastructure.
Docker packages applications.
Amazon ECR stores container images.
Amazon EKS runs containerized applications.
What's Next?
With Terraform infrastructure automation covered, the next step is to focus on AWS IAM & OIDC for secure CI/CD authentication.
The goal is to understand how GitLab pipelines can access AWS securely without relying on long-lived AWS access keys.
The journey continues:
Day -1
AWS DevOps Foundation
↓
Day 1
Amazon EKS Fundamentals
↓
Day 2
GitLab CI/CD → AWS EKS
↓
Day 3
Terraform → AWS Infrastructure
↓
Day 4
AWS IAM & OIDCLearn → Build → Automate → Deploy → Improve
#DevOps #Terraform #AWS #InfrastructureAsCode #GitLab #CICD #AmazonEKS #Docker #CloudComputing #DevOpsJourney #LearningInPublic
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article