Published on

How to Deploy an Nginx Web Server on an EC2 Instance with Terraform (Fully Automated)

Authors

In this guide, I will walk you through the steps to deploy a basic HTML page on an EC2 instance using an Nginx web server and fully automate the process with Terraform.

Prerequisites

  • An active AWS account (you can create one for free).
  • Terraform installed on your computer (follow the installation guide if needed).
  • A text editor of your choice (I recommend VSCode).

Terraform is an Infrastructure as Code (IaC) tool that automates cloud tasks, reducing the need to manually configure settings via a GUI. This automation increases productivity and eases migration between cloud providers.

Setting Up the Terraform Project

Create the following files in your project directory:

  • terraform.tfvars
  • main.tf
  • outputs.tf
  • commands.tpl

The terraform.tfvars file holds variable settings, allowing you to easily adjust infrastructure parameters without modifying longer files. The main.tf file contains the primary infrastructure definitions, and any post-deployment commands should be placed in the commands.tpl file.

Defining the Provider

First, define the AWS provider in your main.tf file:

provider "aws" {
  profile = "default"
  region  = "ap-south-1"
}

The profile value corresponds to your AWS CLI configuration. If you followed the AWS CLI setup guide, you can leave it as "default". Choose the region closest to your location; for example, ap-south-1 if you are in India.

Configuring Resources

Define the EC2 instance resource:

resource "aws_instance" "aws_ubuntu" {
  instance_type = "t2.micro"
  ami           = "ami-09298640a92b2d12c"
  user_data     = file("commands.tpl")
}

Here, aws_instance represents the type of resource. You can consult the Terraform documentation for additional EC2 configuration options. The ami is the Amazon Machine Image ID and may vary by region.

Now, define additional resources:

resource "aws_default_vpc" "default" {
  // ...existing code...
}

resource "aws_security_group" "demo_sg" {
  name        = "demo_sg"
  description = "Allow SSH on port 22 and HTTP on port 80"
  vpc_id      = aws_default_vpc.default.id

  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  ingress {
    from_port   = 80
    to_port     = 80
    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"]
  }
}

These resources ensure that your instance has access to the default VPC and the necessary security group rules for SSH and HTTP.

Outputs

The following output configuration prints the public DNS of your EC2 instance after deployment:

output "aws_instance_public_dns" {
  value = aws_instance.aws_ubuntu.public_dns
}

Initializing and Deploying with Terraform

  1. Initialize your Terraform project:

    terraform init
    

    This command downloads the provider plugins and configures your project.

  2. Validate the configuration:

    terraform validate
    

    This step checks for syntax errors or typos.

  3. Deploy the infrastructure:

    terraform apply
    

    Review the changes, confirm by entering “yes”, and watch as Terraform spins up your instance.

Post-Deployment and Destruction

After deployment, the commands in commands.tpl will be executed on the EC2 instance:

#!/bin/bash -ex
amazon-linux-extras install nginx1 -y
echo "<h1>Hello World</h1>" > /usr/share/nginx/html/index.html
systemctl enable nginx
systemctl start nginx

To tear down all deployed resources, simply run:

terraform destroy -auto-approve

This single command removes all resources, including the EC2 instance.


Hope this guide helps you quickly understand the Terraform process for deploying infrastructure on AWS.