Skip to content

EC2 User Data for Beginners

Published: at 06:10 AM
Written by Rajath Kumar K S

Amazon EC2 (Elastic Compute Cloud) allows you to launch virtual servers in the cloud, and one of its most powerful features is the ability to run custom setup scripts at instance startup using User Data. This guide walks you through creating an EC2 instance and configuring it to serve a basic HTML page using NGINX installed automatically through User Data.

AWS Logo


What is EC2 User Data?

User Data is a feature in AWS EC2 that lets you pass a script to an instance at the time of its launch. This script runs as the root user the first time the instance boots. It’s commonly used to:


Prerequisites


Step-by-Step: Launching an EC2 Instance to Serve HTML with NGINX

1. Go to the EC2 Dashboard

EC2 Dashboard Launch Instance

2. Configure Basic Settings

Configure Basic Settings Configure Basic Settings Configure Basic Settings

3. Set Network and Security

Set Network and Security Set Network and Security

4. Add the User Data Script

Scroll to the Advanced Detail section and paste the following script:

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

This script will:

Add User Data Script Add User Data Script

5. Launch the Instance


Verifying the Setup

Method 1: Browser

Verify Setup

Method 2: SSH

ssh -i <your-key-pair>.pem ec2-user@<your-ec2-ip>
sudo cat /var/log/cloud-init-output.log

Verify Setup


Advanced Use Cases for User Data

1. Automated Deployment

2. Configuration Management

3. Security Best Practices

User Data Scripts can be extended to:

If you are having full static website (including, html, css, js and so on), you can use the following script to install and configure NGINX:

#!/bin/bash
#!/bin/bash
yum update -y
amazon-linux-extras install nginx1 -y
yum install -y nginx git
cd /usr/share/nginx/html
rm -rf *
git clone https://github.com/your-repo/static-website.git .
systemctl enable nginx
systemctl start nginx

Summary

Using EC2 User Data and NGINX, you can automate the deployment of a static HTML site the moment your instance boots. It’s a clean and fast way to get up and running with a web server in minutes—perfect for prototyping, demos, or even hosting a simple landing page.

Whether you’re automating infrastructure or deploying a portfolio site, this approach gives you flexibility and speed.

Happy deploying!

Got questions or want to explore more ways to use NGINX on EC2? Reach out to me on LinkedIn.

Follow Me on LinkedIn


Next Post
Creating a Middleware in FastAPI for Logging Requests and Responses