Terraform Learning Journey: Day 1 β Launching EC2 Instance on AWS with Terraform


Welcome to Day 1 of our Terraform Daily Learning Series! π
Today, we will walk through creating and launching an EC2 instance on AWS using Terraform β the industry-leading tool for Infrastructure as Code (IaC).
Let's dive right in. π₯
π§ What is Terraform?
Terraform is an open-source Infrastructure as Code (IaC) tool created by HashiCorp.
It allows you to define your infrastructure in simple, human-readable configuration files, and then deploy and manage it automatically.
Today, our goal is simple:
β‘οΈ Use Terraform to spin up a virtual server (EC2 instance) on AWS.
π οΈ Prerequisites
Before starting, make sure you have the following:
β An AWS account
β AWS CLI installed and configured (
aws configure
)β Terraform installed (v1.0+ recommended)
β Basic knowledge of terminal/command prompt usage
π Project Setup
Letβs set up our project in a clean structure:
mkdir terraform-ec2-day1
cd terraform-ec2-day1
touch main.tf
We will be writing our Terraform configuration inside main.tf
.
π Writing the Terraform Code
Here is the full Terraform configuration to launch a basic EC2 instance:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.96.0"
}
}
}
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "demo" {
ami = "ami-0e449927258d45bc4" # Amazon Linux 2 AMI
instance_type = "t3.micro"
tags = {
Name = "demoServer"
}
}
β Explanation:
terraform
block: Specifies that we are using the AWS provider.provider
block: Configures AWS provider withus-east-1
region.resource
block: Defines an EC2 instance with a specific Amazon Machine Image (AMI) and instance type.
π§Ή Initialize the Terraform Project
In your project directory, run:
terraform init
This command initializes Terraform, downloads the AWS provider plugin, and sets up the project.
You should see output like:
Terraform has been successfully initialized!
π Plan the Deployment
Before actually creating resources, you can preview what Terraform intends to do:
terraform plan
This will show you an execution plan where Terraform details the resources it will create.
π Apply and Launch the Instance
Now, deploy the infrastructure:
terraform apply
Terraform will ask for a confirmation:
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
Type yes
and hit Enter.
π Your EC2 instance is now being created!
π Verify the Instance
Go to your AWS Management Console
Navigate to EC2 > Instances
You should see an instance running with the tag
Name: demoServer
.
π What Happens After terraform apply
?
After you successfully apply the configuration, Terraform creates and updates several files in your project directory:
File | Purpose |
.terraform/ | A hidden folder where Terraform stores provider binaries and plugin data. |
.terraform.lock.hcl | A lock file that records the exact versions of all Terraform providers used, ensuring reproducible builds. |
terraform.tfstate | A critical file where Terraform records the real-world state of your infrastructure. It keeps track of all created resources. |
main.tf | Your configuration file, where you wrote the code to define resources. (this one already existed). |
π§Ή Cleaning Up (Optional)
Once you're done experimenting, you can destroy the resources to avoid unnecessary charges:
terraform destroy
Type yes
when prompted.
π Thank You for Reading!
Thank you for taking the time to read today's blog!
I hope you found this guide helpful and are excited to continue your Terraform journey. π
Stay tuned for the next part where we will dive even deeper into building real-world infrastructure β one day at a time!
Feel free to leave your thoughts, questions, or feedback in the comments.
Letβs learn and grow together! π±
Subscribe to my newsletter
Read articles from KUNAL directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
