Time to Write Your First Terraform Code, Infra Coders!
Hey, Infra coders! By now, you’ve got Terraform installed and set up with a cloud provider like AWS from our last article. Awesome work! Today, we’re diving into the fun part—writing your first Terraform configuration file to build something real in the cloud. Think of this as your first hands-on project, like sketching out a blueprint and watching it come to life. We’ll create an AWS S3 bucket and a simple EC2 instance (a virtual server) step by step. Don’t worry, I’ll keep it super simple and walk you through everything. Let’s get coding!
Step 1: Set Up Your Project Folder
First, let’s get organized. Create a new folder on your computer for this project—call it something like first-terraform
. Open your terminal or command prompt, navigate to this folder, and create a file named main.tf
. This is where we’ll write our Terraform code.
You can use any text editor you like—VS Code, Notepad, or even Vim. Just make sure your file ends with .tf
so Terraform knows it’s a configuration file.
Why main.tf?
Terraform reads all .tf
files in a folder, but main.tf
is a common name for your main configuration. It’s like the entry point for your project.
Step 2: Configure the AWS Provider
Since we’re using AWS, we need to tell Terraform to connect to it. Open main.tf
and add this code:
provider "aws" {
region = "us-east-1"
}
This tells Terraform:
- Use AWS as the cloud provider.
- Work in the
us-east-1
region (you can change this to another region if you want).
Make sure your AWS credentials are set up in ~/.aws/credentials
(like we did in the last article). Terraform will use those to talk to AWS.
Step 3: Create an S3 Bucket
Let’s start with something simple—an AWS S3 bucket to store files. Add this to your main.tf
file:
resource "aws_s3_bucket" "my_bucket" {
bucket = "my-unique-bucket-name-12345"
}
Here’s what this code does:
resource
: Tells Terraform we’re creating something (in this case, an S3 bucket).aws_s3_bucket
: The type of resource we want (AWS’s S3 bucket).my_bucket
: A name we give this resource to refer to it in our code.bucket
: The actual name of the bucket in AWS (must be globally unique, so replacemy-unique-bucket-name-12345
with something unique).
Pro Tip
S3 bucket names must be unique across all AWS users, so add random numbers or your name to avoid conflicts.
Step 4: Add an EC2 Instance
Now, let’s make things a bit more exciting by adding an EC2 instance—a virtual server in AWS. Add this to main.tf
:
resource "aws_instance" "my_server" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
}
What’s happening here?
aws_instance
: Creates an EC2 instance.my_server
: Our name for this resource.ami
: The Amazon Machine Image (AMI) ID, which defines the operating system (this one is for Amazon Linux 2 inus-east-1
—check AWS for the latest AMI ID in your region).instance_type
: The size of the server (t2.micro
is free-tier eligible).
Note on AMIs
AMI IDs change by region and over time. To find the latest Amazon Linux 2 AMI ID:
- Go to the AWS EC2 Console.
- Click Launch Instance.
- Search for Amazon Linux 2 and note the AMI ID.

Step 5: Initialize Terraform
Before we can run our configuration, we need to set up Terraform in our project folder. In your terminal, navigate to the first-terraform
folder and run:
terraform init
This command:
- Downloads the AWS provider plugin.
- Sets up a
.terraform
folder and a lock file to track dependencies.
You’ll see a message like Terraform has been successfully initialized!
if it works.
Step 6: Preview Your Plan
Let’s see what Terraform is about to do. Run:
terraform plan
Terraform will show you a plan that says it will create:
- One S3 bucket.
- One EC2 instance.
Check the output to make sure it looks right. This is like a dry run to avoid surprises.
Step 7: Apply Your Configuration
Time to make it real! Run:
terraform apply
Terraform will show the plan again and ask you to type yes
to confirm. Once you do, it’ll create the S3 bucket and EC2 instance in AWS. This might take a minute or two. When it’s done, you’ll see Apply complete!
.
Head to the AWS Console:
- Check S3 to see your bucket.
- Check EC2 to see your running instance.
Step 8: Clean Up to Save Costs
Since AWS charges for EC2 instances (even free-tier ones have limits), let’s clean up to avoid surprises. Run:
terraform destroy
Type yes
to confirm, and Terraform will delete the bucket and instance. Check the AWS Console to confirm they’re gone.
Your Complete main.tf File
For reference, here’s what your main.tf
should look like:
provider "aws" {
region = "us-east-1"
}
resource "aws_s3_bucket" "my_bucket" {
bucket = "my-unique-bucket-name-12345"
}
resource "aws_instance" "my_server" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
}
Tips for Writing Great Configurations
You’re off to a great start! Here are some tips to keep your Terraform code clean and effective:
- Use Descriptive Names: Name resources like
my_bucket
orweb_server
so you know what they do. - Keep Files Organized: For small projects, one
main.tf
is fine. For bigger ones, split code into multiple.tf
files. - Check AWS Free Tier: Stick to free-tier resources like
t2.micro
to avoid costs. - Save Your Code: Use Git to version your Terraform files and share with your team.
What’s Next?
You just wrote and ran your first Terraform configuration—how cool is that? You created an S3 bucket and an EC2 instance with a few lines of code. In the next article, we’ll explore how to manage multiple providers (like AWS and Azure together) and add more complex resources. Keep your first-terraform
folder handy—we’ll build on it! See you in the next lesson, Infra coders!
The post Writing Your First Terraform Configuration: A Step-by-Step Guide appeared first on TecAdmin.
Source: Read More