In short
Logging in to every new server to run the same install commands doesn't scale. User data lets a fresh instance set itself up on first boot, which is the first step towards fully automated infrastructure.
User data is a script you pass at launch that runs automatically on first boot, typically through cloud-init. It is used to install packages, pull code and start services so instances configure themselves without manual SSH work.

Think of it like this
A note for the new flatmate
You leave a note on the fridge: "Switch on the geyser, water the plants, start the washing machine." The new flatmate follows it on their first morning without calling you. User data is that note for a new server.
Watch it happen
What happens on first boot
run-instances + user data
1/5You pass a bash script with --user-data file://setup.sh.
The big ideas
What you need to know
Runs once by default
Shell scripts in user data run only on the first boot, not on every restart.
Runs as root
No sudo needed inside the script, and files it creates are owned by root unless you change that.
Not a secret store
Anyone who can view instance attributes, or any process on the instance, can read user data. Keep passwords out.
16 KB limit
Keep scripts small. Pull bigger setup files from S3 or Git inside the script.
See it in code
Walk through the code
1#!/bin/bash2# setup.sh - passed as EC2 user data on Amazon Linux 20233set -euxo pipefail4 5dnf update -y6dnf install -y nginx git7 8INSTANCE_AZ=$(ec2-metadata --availability-zone | cut -d " " -f 2)9echo "<h1>Hello from $INSTANCE_AZ</h1>" > /usr/share/nginx/html/index.html10 11systemctl enable --now nginx1/4The shebang is required. set -euxo stops on errors and logs each command.
A user data script that installs Nginx and serves a page showing which AZ answered. Launch with --user-data file://setup.sh.
Save yourself the pain
Common mistakes
Oops
Forgetting the #!/bin/bash line at the top.
Do this instead
Without a shebang, cloud-init doesn't know how to run the script and nothing happens.
Oops
Editing user data on a running instance and expecting it to rerun.
Do this instead
Scripts run on first boot only. Launch a new instance, or bake the changes into an AMI.
Your mission
Self-configuring server
Quick check
Your user data script did not seem to run. Where do you look first on Amazon Linux?
Go deeper
- 1Launch an instance with a user data script that installs and starts a web server.
- 2Read /var/log/cloud-init-output.log to debug a script that failed.
- 3Compare user data bootstrapping with baking a custom AMI and note the trade-offs.
More topics at this stop
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.