What is a User Data Script?

A User Data Script automates tasks when an EC2 instance first launches, setting up software, applying updates, or configuring the system. It runs once on first boot and can be written in shell, PowerShell, or cloud-init.

Common Use Cases

Example: Setting Up Apache on Amazon Linux 2

To automatically set up a basic web server on launch:

#!/bin/bash
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
echo "<html><h1>Welcome!</h1></html>" > /var/www/html/index.html

How It Works

  1. Installs Apache and updates the instance.
  2. Starts Apache immediately and enables it to auto-start on reboot.
  3. Creates a Simple Web Page for instant access.

Benefits