Day 75 of 100 Days : Terraform Practical Demo – Spin Up Your First EC2 Instance in AWS! 🚀
Welcome to Day 75 of my 100 Days of DevOps journey! 🎉 Today, we’re diving into a hands-on, practical demo of Terraform. If you've ever wanted to quickly spin up an AWS EC2 instance without manually clicking through the AWS Console, this is your chance!
We’re going to keep it simple and easy, so don’t worry if you’re new to Terraform. By the end of this blog, you’ll know how to use Terraform to create your very own EC2 instance in AWS.
Step 1: Check Terraform Version
Before we start, let's first check if Terraform is installed on your system. Open your terminal or bash and type the following command:
terraform -v
This command will show you the installed Terraform version. If you see something like this:
Congratulations! Terraform is already installed. If not, you’ll need to install it first. Once you're good to go, we can move to the next step!
Step 2: Set Up Your AWS Secret Keys
To use Terraform with AWS, you need to pair it with your AWS credentials. This ensures Terraform has permission to interact with your AWS account.
In your bash terminal, use the following commands to set up your AWS credentials:
export AWS_ACCESS_KEY_ID="your-access-key"
export AWS_SECRET_ACCESS_KEY="your-secret-key"
export AWS_DEFAULT_REGION="ap-south-1" # Mumbai region
Make sure to replace your-access-key
and your-secret-key
with your actual AWS credentials. You can get these from your AWS IAM (Identity and Access Management) console. These keys are like a password that Terraform uses to talk to AWS.
Step 3: Create the Terraform Configuration File
Now, let’s create a Terraform configuration file. We’ll use Nano to create the main.tf
file, which will tell Terraform what to do.
Open the terminal and type:
nano main.tf
Inside main.tf
, paste the following code:
provider "aws" {
region = "ap-south-1" # AWS Region set to Asia Pacific (Mumbai)
}
resource "aws_instance" "demo_ec2" {
ami = "ami-053b12d3152c0cc71" # Your AMI ID for the EC2 instance
instance_type = "t2.micro" # t2.micro is eligible for the Free Tier
key_name = "aws_logins" # EC2 key pair for SSH access (make sure this key pair exists in the region)
tags = {
Name = "Demo EC2 Instance" # Tagging the instance with a name
}
}
Breaking Down the Code – Let's Explain It Like a Kid! 🎨
Let’s break down this code and explain it like we’re explaining to a 5-year-old! 🧸
provider "aws"
:
This part tells Terraform, "Hey! I want to use AWS for creating resources." Theregion
is where your resources will live – in this case, Mumbai (ap-south-1
).resource "aws_instance" "demo_ec2"
:
This defines the resource we’re creating – in this case, an EC2 instance nameddemo_ec2
.ami = "ami-053b12d3152c0cc71"
:
This is the ID of the Amazon Machine Image (AMI). An AMI is like a blueprint for your instance. It tells AWS what kind of machine you’re creating. This AMI ID is for a basic, free-tier-friendly Linux machine.instance_type = "t2.micro"
:
This is the type of EC2 instance. A t2.micro is free-tier eligible, which means it’s free for personal use up to certain limits!key_name = "aws_logins"
:
This is the SSH key pair that you’ll use to securely access your EC2 instance. You should already have this key pair created in the AWS console.tags = { Name = "Demo EC2 Instance" }
:
Tags help us name and organize our resources. We’re naming our EC2 instance “Demo EC2 Instance.”
Step 4: Initialize Terraform
Before running any commands, we need to initialize Terraform. This sets up the backend and downloads any necessary plugins.
Run:
terraform init
This command will:
Initialize your working directory.
Download AWS provider plugins.
Prepare everything for the magic to happen.
Step 5: Preview the Changes
Now that everything is initialized, let’s see what Terraform is planning to do. Run:
terraform plan
This command shows you a preview of what Terraform is going to create, modify, or delete. Think of it as a sneak peek before the actual action begins!
Step 6: Apply the Terraform Configuration
Ready for action? Let’s apply the changes. Run:
terraform apply
Terraform will ask for confirmation to proceed. Type yes
to confirm.
This command will:
Create the EC2 instance based on the configuration we wrote in
main.tf
.Spin up the EC2 instance, ready for you to SSH into and start using!
Step 7: Check Your AWS EC2 Instance
After the Terraform apply finishes, head over to the AWS Console to see your EC2 instance running. You should see something like this:
Additional Terraform Commands
Here are a few other useful Terraform commands you can use in your day-to-day workflow:
terraform deploy
: Not an official command, but often used to describe the act of applying your configuration and deploying resources in AWS.terraform destroy
: When you’re done, run this command to delete the resources you’ve created and clean up your AWS account.
Conclusion: Terraform Magic at Your Fingertips!
Today, we’ve gone through the entire process of using Terraform to create an EC2 instance in AWS, from checking your version to running your very own infrastructure as code. 🚀
With Terraform, the sky’s the limit! You can scale up your infrastructure in no time, automate the deployment of entire cloud environments, and work faster and smarter.
Make sure to check your AWS console for the EC2 instance you’ve just created and have fun exploring what Terraform can do next!
What’s Next?
Tomorrow, we’ll continue with more advanced Terraform tricks, including managing multiple environments and handling state files. Stay tuned! 🌟
And don’t forget to share your experience with Terraform or any cool tricks you learned today in the comments below. Let’s keep the learning rolling! 💬