Infrastructure as Code (IaC) tools like Terraform have revolutionized the way we manage and provision cloud infrastructure. In today's blog post, we'll guide you through the process of hosting a website on an AWS EC2 instance, complete with security group setup, all using Terraform. Let’s dive in!
Prerequisites:
Basic knowledge of AWS services (EC2, Security Groups).
Terraform is installed on your machine.
AWS CLI configured with appropriate secret credentials.
Step1 Setting Up Your Terraform Configuration
First, initialize a new Terraform directory and create two main files: variables.tf
and main.tf
.
variables.tf: This file will contain our variable definitions.
#Define AWS Region
variable "aws_region" {
description = "AWS region to deploy resources in"
default = "us-east-1"
}
#Define Instance Type - Free Tier
variable "instance_type" {
description = "Type of EC2 instance"
default = "t2.micro"
}
#Define Amazon Machine Image - Linux Ubuntu
variable "ami_id" {
description = "ID of the Amazon Machine Image (AMI) to use"
default = "ami-053b0d53c279acc90" # This is an Linux Ubuntu AMI. Ensure it's available in your region or adjust accordingly.
}
main.tf: Contains all the resources
Step2 : Create the EC2 Security Group
Security Groups act as a virtual firewall to control inbound and outbound traffic. For our website, we need to allow HTTP (port 80) and HTTPS (port 443) traffic.
#provider
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
region = "us-west-1" # You can change this to your preferred region
}
#Create New Security Group
resource "aws_security_group" "web_sg" {
name = "web_sg"
description = "Allow web traffic"
#HTTP Traffic Allowed
ingress {
description = "Access Website"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "SSH"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
description = "egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
Step3 : Launch the EC2 Instance
Now that we have our security group, we can launch the EC2 instance.
#main.tf continued....
resource "aws_instance" "web_server" {
ami = var.ami_id
instance_type = var.instance_type
security_groups = [aws_security_group.web_sg.name]
key_name = "My_User"
tags = {
Name = "WebServer-Terraform"
}
#Add UserData to install apache server to host your website
user_data = <<-EOF
#!/bin/bash
sudo apt-get update -y
sudo apt-get install -y apache2
sudo systemctl start apache2
sudo systemctl enable apache2
echo "<header> <h1> Hello World! Welcome to <a href="https://kshitijaa.hashnode.dev/" target="_blank"> Kshitija-Bartakke-Malwade-Blogs! </h1> </header>" > var/www/html/index.html
EOF
}
We are using user_data
to automatically set up and start an Apache server and add a sample HTML page.
Step4 : Initialize and Apply Terraform Configuration
teterraform init && terraform apply
After running terraform apply
, review the plan and confirm the changes. Once the EC2 instance is up and running, you can get its public IP address from the AWS Management Console or the Terraform output. Accessing this IP in a browser will display our sample webpage.
This is how instance got created with all the configuration used in terraform script.
Let's browse the URL with public Ip address using port 80/http
This is the outcome of many hours of setting up the configuration!
I won't give up! I will always shine through all the hurdles!
Step5 : CleanUp
To avoid unnecessary costs, make sure to destroy resources you've created after you're done:
terraform destroy
This is a basic example. In a real-world scenario, you might want to use an Elastic Load Balancer, Auto Scaling groups, and other resources to make your website resilient and highly available.
Wrapping Up
And there you have it! With just a few Terraform configurations, you’ve set up a secure AWS EC2 instance and hosted a simple website on it. Remember, this is a basic setup. In a real-world scenario, you might want to incorporate more advanced features like SSL, domain names, load balancers, and more. Nevertheless, this should give you a solid foundation to build upon using Terraform with AWS. Happy coding!