Terraform for AWS: Infrastructure as Code Basics
Learn how Terraform automates AWS infrastructure, understand Infrastructure as Code basics, and explore why DevOps teams use it for repeatable deployments.
Series: My 90-Day AWS & DevOps Learning Journey (27 articles)
- …
- 23Terraform for AWS: Infrastructure as Code Basics This article
Terraform for AWS: Infrastructure as Code Basics
Imagine creating an EC2 instance manually through the AWS Console.
It works.
Now imagine creating ten similar environments, repeating the same configuration next week, or rebuilding everything after an accidental deletion.
Would you want to repeat every step manually?
This is one reason I'm exploring Infrastructure as Code (IaC) as part of my AWS and DevOps learning journey.
What Is Infrastructure as Code?
Infrastructure as Code means defining and managing infrastructure through configuration files instead of relying entirely on manual setup.
Instead of clicking through the AWS Console every time, we can describe the resources we need and use tools to create or update them.
For example, infrastructure might include:
- Amazon EC2 instances
- Amazon S3 buckets
- VPCs and subnets
- Security groups
- IAM resources
One popular tool for this is Terraform.
What Is Terraform?
Terraform is an Infrastructure as Code tool that lets you define infrastructure using configuration files.
It uses a declarative approach: you describe the desired infrastructure, and Terraform determines the changes needed to move toward that configuration.
A simplified workflow looks like this:
1
2
3
4
5
6
7
8
9
10
11
Write Configuration
↓
terraform init
↓
terraform plan
↓
Review Changes
↓
terraform apply
↓
Infrastructure CreatedThe useful part is that the configuration can be stored in Git, reviewed, reused, and updated as your infrastructure evolves.
A Simple AWS Example
Let's look at a basic Terraform configuration that defines an S3 bucket.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 6.0"
}
}
}
provider "aws" {
region = "ap-south-1"
}
resource "aws_s3_bucket" "example" {
bucket = "replace-with-a-globally-unique-name"
}This example specifies the AWS provider and an S3 bucket resource. The bucket name must be replaced with a globally unique name that meets S3 naming requirements.
Before running it, configure AWS credentials securely and check the Terraform and provider versions available in your environment.
What Do the Terraform Commands Do?
Three commands are particularly important when getting started.
1. Initialize
1
terraform initDownloads the required providers and prepares the working directory.
2. Preview changes
1
terraform planShows the changes Terraform intends to make.
3. Apply changes
1
terraform applyApplies the proposed changes after the required confirmation.
For beginners, the most important habit is to review the plan before applying it. Infrastructure changes can create billable resources or affect existing systems.
When you're finished experimenting, use
terraform destroy only after reviewing which resources it will remove. Never run destructive commands against infrastructure you need to keep.Why Is Terraform Useful in DevOps?
Terraform becomes particularly useful when infrastructure needs to be consistent across development, testing, and production environments.
For example:
1
2
3
4
5
6
7
8
9
Terraform Configuration
↓
Git Repository
↓
Review Changes
↓
CI/CD Pipeline
↓
AWS InfrastructureA team can review infrastructure changes before applying them, track configuration changes in version control, and reduce repetitive manual work.
Terraform also supports many infrastructure providers, which can be useful when working across different environments.
However, IaC doesn't automatically make infrastructure secure or error-free. Permissions, state management, secrets, and change reviews still matter.
Terraform vs AWS CloudFormation
Another question worth understanding is how Terraform differs from AWS CloudFormation.
Terraform supports multiple infrastructure providers and uses its own configuration language.
AWS CloudFormation is an AWS-native infrastructure provisioning service.
Both can define AWS resources as code. The right choice depends on your project, existing workflows, and the tools your team uses.
For someone starting with AWS DevOps, understanding the IaC concept is more important than trying to master every tool at once.
My Learning Journey
My recent learning has covered Docker, ECR, ECS, Jenkins, and GitHub Actions.
Now I'm exploring how infrastructure itself can be automated.
The bigger workflow is becoming clearer:
Code → CI/CD → Infrastructure as Code → Deployment → Monitoring
I want to move beyond simply launching AWS resources manually and learn how to create repeatable, manageable infrastructure.
💬 Let's Discuss
For your first Infrastructure as Code project, would you choose Terraform or AWS CloudFormation? Why?
If you've already used Terraform, what was the biggest challenge when you started: writing HCL, understanding state, configuring IAM permissions, or managing infrastructure changes?
I'd love to hear practical experiences from people learning or working in cloud and DevOps.
Series: My 90-Day AWS & DevOps Learning Journey (27 articles)
- …
- 23Terraform for AWS: Infrastructure as Code Basics This article
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article