Terraform Lab 3 : Deploying a WebPage Using Terraform 2024

In this detailed guid we will take you through the process of deploying a basic “Hello World” web page on an Amazon EC2 instance using Terraform. Terraform’s infrastructure as code (IaC) approach allows for easy provisioning and management of cloud resources.

Prerequisites

  • An AWS account.
  • Terraform installed on your local machine.
  • Basic knowledge of HTML and web server.

Step 1: Setup an AWS credentials

Ensure you have your AWS credentials configured either by using AWS CLI or setting environment variables on your local machine.

Step 2: Create Project Structure

Begin by creating a new directory for your project. Inside the directory, create a file named ‘webpage.tf’ to define the Terraform configuration.

Step 3: Write Terraform Configuration

Open ‘webpage.tf’ and add the following code,

provider "aws" {
region = "your-preferred-region"
}

resource "aws_instance" "web_server" {
ami = "ami-xxxxxxxxxxxxxxxxx" # Choose an appropriate AMI for your region
instance_type = "t2.micro"

tags = {
Name = "web-server-instance"
}

user_data = <<-EOF
#!/bin/bash
echo "<html><body><h1>Hello, World </h1></body></html>" > /var/www/html/index.html
service apache2 restart
EOF
}

Replace “your-preferred-region” and “ami-id” with your desired AWS region and suitable AMI ID respectively.

Certainly! Let’s breakdown the contents of webpage.tf file, explaining each section,

provider "aws" {
region = "your-preferred-region"
}

The above section defines the AWS provider configuration for Terraform. It specifies that Terraform should use AWS as the cloud provider and sets the region where the resources will be created. Replace “your-preferred-region” with the AWS region you want to use.

resource "aws_instance" "web_server" {
ami = "ami-xxxxxxxxxxxxxxxxx"
instance_type = "t2.micro"

tags = {
Name = "web-server-instance"
}

user_data = <<-EOF
#!/bin/bash
echo "<html><body><h1>Hello, World!</h1></body></html>" > /var/www/html/index.html
service apache2 restart
EOF
}

In this above section, a specific AWS resource is defined using the “aws_instance” resource type. This resource type represents an EC2 instance.

  • ami : Stands for Amazon Machine Image. It specifies the image ID to use for EC2 instance. Replace “ami-xxxxxxxxxxxxxxxxx” with a valid AMI ID based on your desired operating system and region.
  • instance_type : Specifies the type of EC2 instance to launch, In this example, it’s set to “t2.micro” for a micro sized instance.
  • tags : Allows you to attach metadata to your resource. In this case a tag with the key “Name” and value “web-server-instance” is assigned.
  • user_data : This field is used for providing user data scripts that run when the instance is launched. The script in this example is a simple bash script that creates an HTML file with a “Hello, World” message and restarts the apache web server.

This “webpage.tf” essentially describes the desired state of your infrastructure. When you run terraform apply, Terraform reads this file, understands the configuration, and take the necessary action to create or modify resources to match this configuration.

Step 4: Initialize, Plan and Apply Terraform Configuration

terraform init
terraform plan
terraform apply

terraform init : This command initializes your Terraform configuration and downloads the necessary provider plugins.

terraform plan : This command shows you what Terraform will do before actually doing it.

terraform apply : Execute this command to apply your terraform configuration.

After applying the Terraform configuration Terraform promt you to confirm the changes type ‘yes’ to proceed with the deployment.

Step 5: Access the Hello World Web Page

Once Terraform has completed the deployment, check the output for the public IP address of your EC2 instance. Open your web browser, enter the IP address in the address bar and you should see your “Hello, World” message.

Step 6: Cleaning Up

Once you are done experimenting, it’s essential to clean up your resources to avoid unnecessary charges. Run the following command in the respective directory,

terraform destroy

Above command will destory/delete the resource which we have created with the above configuration file.

Conclusion

You have successfully deployed a basic web page on an AWS EC2 instance using Terraform. The provided Terraform configuration file (webpage.tf) serves as a starting point for more advanced infrastructure deployments. Explorer additional Terraform features and AWS resources to enhance and customize your cloud infrastructure. This hands-on experience lays the founfoundation for managing infrastructure as code efficiently.

Leave a Comment