To create a simple basic EC2 Instance using terraform. Below are the step needed.
Directory Structure
Create a new project directory and files:
web-server-terraform/: this is a folder
├ main.tf
├ variables.tf
├ outputs.tf
main.tf
This file contains the main infrastructure definition. Copy ad paste this code into the main.tf file
Provider Configuration
provider "aws" {
region = "us-east-1" # Replace with your preferred region
}
VPC
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = "web-server-vpc"
}
}
Subnet
resource "aws_subnet" "public_subnet" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
map_public_ip_on_launch = true
availability_zone = "us-east-1a" # Replace as needed
tags = {
Name = "web-server-subnet"
}
}
Internet Gateway
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.main.id
tags = {
Name = "web-server-igw"
}
}
Route Table
resource "aws_route_table" "public_route_table" {
vpc_id = aws_vpc.main.id
tags = {
Name = "public-route-table"
}
}
Associate Route Table with Subnet
resource "aws_route_table_association" "public_association" {
subnet_id = aws_subnet.public_subnet.id
route_table_id = aws_route_table.public_route_table.id
}
Route for Internet Access
resource "aws_route" "public_route" {
route_table_id = aws_route_table.public_route_table.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
Security Group
resource "aws_security_group" "web_sg" {
vpc_id = aws_vpc.main.id
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "web-server-sg"
}
}
EC2 Instance
resource "aws_instance" "web_server" {
ami = "ami-08c40ec9ead489470" # Replace with a valid AMI ID for your region
instance_type = "t2.micro"
subnet_id = aws_subnet.public_subnet.id
security_groups = [aws_security_group.web_sg.name]
associate_public_ip_address = true
user_data = <<-EOF
#!/bin/bash
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
echo "Hello, World!" > /var/www/html/index.html
EOF
tags = {
Name = "web-server"
}
}
variables.tf
Define variables for your configuration (optional for better reusability).
variable "region" {
description = "AWS region to deploy resources"
default = "us-east-1"
}
outputs.tf
Outputs to display useful information after deployment.
output "web_server_public_ip" {
description = "Public IP of the web server"
value = aws_instance.web_server.public_ip
}
- Deploy Using Terraform Initialize Terraform Run this command in the project directory to initialize Terraform and download the AWS provider:
bash
Copy code
terraform init
Validate Configuration
Check for syntax or configuration errors:
bash
Copy code
terraform validate
Preview Changes
View the resources Terraform will create:
bash
Copy code
terraform plan
Apply the Configuration
Deploy the infrastructure:
bash
Copy code
terraform apply
Type yes when prompted.
**4. Access the Web Server
**After Terraform completes, it will output the public IP of the EC2 instance. Open a browser and visit: http://
You should see the "Hello, World!" page.
- Cleanup To destroy the resources created by Terraform type:
terraform destroy
Top comments (0)