Table of contents
- Introduction
- Prerequisites
- Step 1: Create a Working Directory
- Step 2: Write the Terraform Configuration File
- Step 3: Initialize Terraform
- Step 4: Preview the Changes
- Step 5: Deploy the EC2 Instance
- Step 6: Connect to the Instance via SSH
- Step 7: Verify the Instance
- Step 8: Destroy the Instance (Optional)
- Conclusion
Introduction
Terraform is an Infrastructure as Code (IaC) tool that helps you automate AWS resource deployment. In this tutorial, you'll learn how to use Terraform to launch an EC2 instance on AWS.
Prerequisites
Before starting, ensure you have:
An AWS account
AWS CLI installed and configured (
aws configure
)Terraform installed (Download Terraform)
Step 1: Create a Working Directory
Create a new folder for your Terraform project and navigate into it:
mkdir terraform-ec2 && cd terraform-ec2
Step 2: Write the Terraform Configuration File
Create a new file named main.tf
and add the following code:
provider "aws" {
region = "us-east-1" # Change to your preferred region
}
resource "aws_security_group" "terraform_sg" {
name_prefix = "terraform-sg"
description = "Allow SSH and HTTP access"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"] # Restrict this in a real-world scenario
}
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0" # Replace with a valid AMI ID
instance_type = "t2.micro"
security_groups = [aws_security_group.terraform_sg.name]
tags = {
Name = "Terraform-EC2"
}
}
Step 3: Initialize Terraform
Run the following command to initialize the Terraform project:
terraform init
This will download the necessary provider plugins.
Step 4: Preview the Changes
Check what resources Terraform will create:
terraform plan
Review the output to ensure everything looks correct.
Step 5: Deploy the EC2 Instance
Run the following command to create the EC2 instance:
terraform apply -auto-approve
Terraform will provision the EC2 instance, and you will see the instance ID in the output.
Step 6: Connect to the Instance via SSH
If you have a key pair associated with your AWS account, you can SSH into the instance using:
ssh -i /path/to/your-key.pem ec2-user@your-instance-public-ip
You can find the public IP using:
aws ec2 describe-instances --query "Reservations[*].Instances[*].PublicIpAddress" --output text
Step 7: Verify the Instance
You can check if the EC2 instance is running using AWS CLI:
aws ec2 describe-instances --query "Reservations[*].Instances[*].InstanceId" --output text
Alternatively, go to the AWS Management Console and verify the instance in the EC2 Dashboard.
Step 8: Destroy the Instance (Optional)
If you want to delete the EC2 instance and free up resources, run:
terraform destroy -auto-approve
Conclusion
Congratulations! You have successfully launched an EC2 instance using Terraform. This tutorial covered:
Writing a Terraform configuration file
Creating a security group to allow SSH and HTTP access
Initializing Terraform
Applying Terraform to deploy an EC2 instance
Connecting to the instance via SSH
Verifying and destroying the instance
For more Terraform tutorials, stay tuned to our blog!