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

KUNALKUNAL
3 min read

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 with us-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:

FilePurpose
.terraform/A hidden folder where Terraform stores provider binaries and plugin data.
.terraform.lock.hclA lock file that records the exact versions of all Terraform providers used, ensuring reproducible builds.
terraform.tfstateA critical file where Terraform records the real-world state of your infrastructure. It keeps track of all created resources.
main.tfYour 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! 🌱

0
Subscribe to my newsletter

Read articles from KUNAL directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

KUNAL
KUNAL