How to Create an AWS EC2 Instance Using Terraform
Hi everyone! 👋 Today, I’ll show you how I created an AWS EC2 instance using Terraform. If you are new to this, don’t worry—I'll keep it simple and easy to follow. Let’s get started! 🚀
What is Terraform?
Terraform is a tool that helps you create and manage cloud resources like servers (EC2), storage, and more. Instead of clicking around in the AWS console, you write a file that tells Terraform what you want. Then, it creates everything for you.
Step 1: Things You Need
Before starting, make sure you have these tools ready:
Terraform installed on your computer.
AWS CLI installed and set up with your AWS account credentials.
If you don’t have them installed, you can find the installation guides here:
Step 2: Write the Configuration File
Create a file named main.tf
and copy this code:
provider "aws" {
region = "ap-south-1"
}
resource "aws_instance" "example" {
ami = "ami-0dee22c13ea7a9a67"
instance_type = "t2.micro"
tags = {
Name = "Example-demo"
}
}
Here’s what the code does:
The provider block tells Terraform to use AWS and sets the region (I used ap-south-1).
The resource block creates an EC2 instance with the specified AMI (Amazon Machine Image) and instance type (t2.micro).
The tags give a name to the instance so it’s easy to identify.
Step 3: Run Terraform Commands
After writing the file, open your terminal and run these commands:
Initialize Terraform
terraform init
This command sets up Terraform and downloads the necessary plugins.
Check the Plan
terraform plan
This shows what Terraform will create. It’s like a preview before making changes.
Create the Instance
terraform apply
When you run this, Terraform will ask for confirmation. Type yes
to continue.
Step 4: EC2 Instance Created!
That’s it! After running the terraform apply command, my EC2 instance was created successfully. 🎉
Here’s what it looked like:
Conclusion
Using Terraform makes creating cloud resources like EC2 instances super easy. You just write a file, run a few commands, and everything is ready.
Try it yourself, and let me know how it goes! If you found this helpful, share it with others. 😊
Subscribe to my newsletter
Read articles from Harsh Soni directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Harsh Soni
Harsh Soni
👋 Hello! I'm Harsh, a DevOps enthusiast driven by a passion for continuous learning and innovation. With hands-on experience across cloud platforms, infrastructure tools, and programming languages, I'm committed to creating scalable, automated, and efficient solutions. Let’s connect, share knowledge, and build amazing things together! SKILLS: 🔹Languages & Runtimes: Python, C#, Shell Scripting, YAML 🔹Cloud Expertise: AWS (proficient in EC2, ELB, EBS, S3, VPC, Route53, SES, SNS, CloudFormation, CloudWatch) 🔹Infrastructure & DevOps Tools: Docker, Terraform, AWS CloudFormation 🔹Development & CI/CD: Linux, Git, GitHub Actions, Jenkins, Jira, GitLab (beginner), AWS DevOps 🔹Web Development: HTML, CSS, Bootstrap, .NET, Python, SQL Why Connect with Me? I'm a detail-oriented professional with strong communication skills, always ready to tackle complex problems with a positive mindset. Whether you’re here to network or explore potential collaborations, feel free to reach out. Let’s grow together in the DevOps journey! 📧 Contact Me: harshsoni6011@gmail.com