Skip to main content

Command Palette

Search for a command to run...

Deploy Nginx Automatically Using Terraform user_data

Automatically configure AWS EC2 instances using Terraform and deploy a live Nginx web server during server creation.

Updated
5 min read
Deploy Nginx Automatically Using Terraform user_data
S

As a seasoned system administrator with a passion for technology and problem-solving, I bring 6 Years of hands-on experience in managing and maintaining IT infrastructure. My journey in the field began with a deep curiosity for computers and systems, which has evolved into a fulfilling career dedicated to ensuring the smooth operation of critical business systems.

Introduction

In the previous Terraform lab, we created AWS infrastructure using Terraform.

Now we’ll move one step further into real-world DevOps automation by automatically installing and configuring Nginx during EC2 deployment using Terraform user_data.

This is one of the most important concepts in Infrastructure as Code (IaC) because modern cloud servers are usually configured automatically during provisioning.


Goal

Build a Terraform lab that:

  • Creates AWS infrastructure

  • Automatically installs Nginx

  • Automatically starts the web server

  • Serves a custom web page

  • Uses AWS Free Tier safely

  • Demonstrates a real-world provisioning workflow


What You Will Learn

  • What Terraform user_data is

  • How EC2 bootstrapping works

  • Automatic package installation

  • Automated server configuration

  • How to deploy Nginx automatically

  • How Infrastructure as Code works in real environments


Architecture

Terraform will create the following:

Internet
└─ Internet Gateway
└─ Public Subnet
└─ EC2 Instance
└─ Automatic Nginx Installation
└─ Website Accessible from Browser

Add architecture diagram screenshot here


Why This Lab Is Important

Until now, Terraform only created infrastructure.

After this lab:

  • Server configures itself automatically

  • Nginx installs automatically

  • Website becomes live automatically

  • No manual package installation needed

This is how modern cloud automation works.


Understanding user_data

AWS EC2 instances support something called user_data.

user_data allows us to run shell scripts automatically when the server starts for the first time. This can be used for:

  • Installing software

  • Creating users

  • Updating packages

  • Deploying applications

  • Configuring services

This process is called bootstrapping.


⚠️ AWS Free Tier Safety

Use ONLY:

  • t2.micro or t3.micro

  • One EC2 instance

  • Small EBS volume

Avoid:

  • NAT Gateway

  • Load Balancers

  • RDS

Always destroy resources after testing.


Step 1 — Update Security Group

Previously we allowed only SSH access. Now also allow HTTP traffic on port 80.

Replace your ingress section with this:

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

ingress {
  description = "HTTP Access"
  from_port   = 80
  to_port     = 80
  protocol    = "tcp"
  cidr_blocks = ["0.0.0.0/0"]
}

Step 2 — Update EC2 Resource

Replace your EC2 resource block with the following:

# -----------------------------
# EC2 Instance
# -----------------------------
resource "aws_instance" "ubuntu_server" {
  ami                         = "ami-03bb6d83c60fc5f7c"
  instance_type               = "t3.micro"
  subnet_id                   = aws_subnet.public_subnet.id
  vpc_security_group_ids      = [aws_security_group.lab_sg.id]
  associate_public_ip_address = true

  user_data = <<-EOF
              #!/bin/bash
              apt update -y
              apt install nginx -y

              systemctl enable nginx
              systemctl start nginx

              echo "<h1>Terraform Nginx Lab Working</h1>" > /var/www/html/index.html
              EOF

  tags = {
    Name = "terraform-nginx-server"
  }
}

Download the full main.tf file from here:
https://github.com/sanchitpandit/Devops/blob/main/terraform/tf-deploy-nginx/main.tf


What This Script Does

When EC2 starts:

  • Updates Ubuntu packages

  • Installs Nginx

  • Enables Nginx service

  • Starts Nginx

  • Creates a custom HTML page

Everything happens automatically.


Step 3 — Format Terraform Files

Run:

terraform fmt

Step 4 — View Terraform Plan

Run:

terraform plan

Terraform will detect infrastructure changes.

Step 5 — Apply Changes

Run:

terraform apply

Type:

yes

Terraform will update and create resources.

Inserting image...

Step 6 — Verify Nginx Installation

After deployment completes, open your browser:

http://PUBLIC-IP

Replace PUBLIC-IP with your EC2 public IP.

Expected Output

You should see the page with:

Terraform Nginx Lab Working

Real-World DevOps Concepts Learned

This lab teaches:

  • Bootstrapping

  • Automated provisioning

  • Immutable infrastructure

  • Infrastructure automation

  • Server initialization

  • Cloud provisioning workflow

These are real production concepts used in DevOps environments.


⚠️ Common Mistakes

  • Forgetting to open port 80 — Browser will not load the website. Make sure Security Group allows port 80.

  • Wrong AMI — Always use an Ubuntu AMI compatible with your region.

  • Not waiting for user_data — Sometimes installation takes 1–2 minutes after EC2 starts. Wait briefly before testing the website.


Real Business Usage

In production, user_data is used for:

  • Web server deployment

  • Monitoring agent installation

  • Docker installation

  • Kubernetes node preparation

  • Security hardening

  • Application deployment

This allows fully automated server provisioning.


Cleanup

After testing, destroy all resources to avoid AWS charges.

Run:

terraform destroy

Type:

yes

🎉 Conclusion

You have now moved beyond basic infrastructure deployment and entered real Infrastructure Automation using Terraform.

With user_data, your servers can:

  • Configure themselves

  • Install software automatically

  • Become production-ready immediately after deployment

This is a core foundation of modern DevOps and Cloud Engineering.