Hosting a website on AWS EC2 is a popular approach for individuals and businesses leveraging the cloud’s scalability and reliability. When combined with Terraform, an Infrastructure as Code (IaC) tool, provisioning and managing AWS resources becomes seamless and automated. This guide will walk you through using Terraform to provision infrastructure and host a website on an EC2 instance.

Introduction to Terraform Provisioning

Terraform is an open-source tool for defining and managing infrastructure using simple, human-readable configuration files. It can also automate the provisioning of AWS resources, ensuring consistency and repeatability across environments. This guide will focus on using Terraform to create an EC2 instance and host a website on it.

Creating a Key Pair for Secure Access

Before provisioning an EC2 instance, you must create a key pair to access the instance via SSH securely. A key pair consists of a private key, which you keep secure, and a public key stored on the instance.

To create a key pair:

  1. Navigate to the EC2 dashboard in the AWS Management Console.
  2. Select “Key Pairs” from the left-hand menu.
  3. Click “Create Key Pair,” give it a name, and download the .pem file.

Ensure you store this file securely, as it will be required for SSH access to your EC2 instance.

Defining Terraform Variables for Customization

Terraform allows you to define variables to customize your infrastructure. Variables make your Terraform configuration flexible and reusable. You can define variables for parameters such as the EC2 instance type, region, AMI ID, and key pair name.

Example variables.tf file:

variable “region” {

  description = “The AWS region to deploy resources.”

  default     = “us-east-1”

}

variable “instance_type” {

  description = “The EC2 instance type.”

  default     = “t2.micro”

}

variable “ami_id” {

  description = “The AMI ID for the EC2 instance.”

  default     = “ami-12345678”  # Replace with a valid AMI ID

}

variable “key_pair_name” {

  description = “The name of the key pair.”

}

Writing the Terraform Provider Configuration

The provider configuration in Terraform specifies the infrastructure provider (AWS in this case) and the region where resources will be provisioned.

Example provider.tf file:

provider “aws” {

  region = var.region

}

Scripting the Website Setup Process

To host a website on your EC2 instance, you must install a web server and deploy your website files. This can be automated using a user data script that runs when the instance is launched.

Example user data script (userdata.sh):

#!/bin/bash

sudo yum update -y

sudo yum install -y httpd

sudo systemctl start httpd

sudo systemctl enable httpd

# Replace with your website files

echo “<h1>Hello, World!</h1>” > /var/www/html/index.html

Creating an EC2 Instance with Terraform

With the variables and provider configuration in place, you can now define the EC2 instance resource in Terraform.

Example main.tf file:

resource “aws_instance” “web” {

  ami           = var.ami_id

  instance_type = var.instance_type

  key_name      = var.key_pair_name

  user_data = file(“userdata.sh”)

  tags = {

    Name = “WebServer”

  }

}

Executing Terraform to Provision Resources

To execute the Terraform configuration and provision the resources on AWS, follow these steps:

  1. Initialize Terraform:

    terraform init
  2. Preview the changes Terraform will make:

    terraform plan
  1. Apply the configuration to create the resources:

    terraform apply

Verifying Resource Creation on AWS

Once the resources are provisioned, you can verify their creation in the AWS Management Console:

  1. Navigate to the EC2 dashboard and check for the newly created instance.
  2. Verify the instance state and IP address.

Accessing the Hosted Website

After verifying that the EC2 instance is running, you can access the hosted website using its public IP address. Open a web browser and enter the IP address in the address bar. You should see the “Hello, World!” message or your website content displayed.

Cleaning Up Resources with Terraform

Once you’re done with the website, cleaning up the resources is essential to avoid unnecessary costs. Terraform makes this process easy:

  1. Run the following command to destroy the resources created by Terraform:

    terraform destroy
  2. Confirm the action when prompted. Terraform will delete the resources from AWS.

Conclusion

By following this guide, you’ve successfully used Terraform to provision infrastructure on AWS and host a website on an EC2 instance. This approach automates the provisioning process and ensures your infrastructure is consistent, scalable, and easy to manage.

References

Provision AWS infrastructure using Terraform (By HashiCorp): an example of a web application logging customer data

What’s the Difference Between Terraform and Kubernetes?