Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
EC2 — Virtual Servers  ›  Lesson

User Data Scripts

User Data Scripts 12 minUser data is a script passed to an EC2 instance at launch that runs once, as root, on its first boot, so a fresh server can set itself up without anyone logging in.

User Data Scripts

Every time Inkwell launches a new web server, the developer logs in and types the same commands: install nginx, start it, copy the site. On book-launch days, when servers need to appear quickly, that manual routine is too slow and too easy to get wrong. EC2 can run the routine for you.

The rule

User data runs once, as root, at first boot.

User data is text you hand to an instance when you launch it. If it is a script, a tool on the image called cloud-init runs it during boot:

#!/bin/bash
dnf install -y nginx
systemctl enable --now nginx
echo "Inkwell Books is open" > /usr/share/nginx/html/index.html

Each part of the rule is a rule to design around:

  • Once: by default the script runs only on the first boot. Stopping and starting the instance does not run it again.
  • As root: the script already has full rights, so no sudo is needed. It also means a mistake can do real damage.
  • At first boot: it runs before anyone could log in, so it cannot ask questions. Every command must work unattended, which is why dnf install gets the -y flag.

Practical limits: user data is capped at 16 KB, so large setups should download their files from somewhere like S3 rather than embed them. The first line decides how it is read; #!/bin/bash marks a shell script. And user data is not secret: anyone who can view the instance's settings can read it, so passwords belong in a service such as AWS Secrets Manager instead.

If the site does not come up, read the script's output on the instance at /var/log/cloud-init-output.log.

The shortcut

Test each script with the three unattended checks: Could it run with nobody watching? Is it safe running as root? Is there nothing secret in it?

Checking Inkwell's scriptStep 1 of 3
  1. Nobody watching?

    Without -y, dnf install nginx would stop to ask "Is this ok?". The flag answers yes by itself.

The common trap: editing user data on a stopped server and expecting it to rerun. By default it will not. Launch a new instance instead, which is the healthier habit anyway, since servers built the same way every time are easier to replace.

Drills

Answer fast — trust the rule.

quick drill

When does a user data script run by default?

quick drill

Which user runs a user data script?

quick drill

Inkwell's script works when typed by hand but hangs as user data. What is the likely cause?

quick drill

Where should the shop's database password go?

Remember

  • User data runs once, as root, at first boot.
  • Three unattended checks: nobody watching, safe as root, nothing secret.
  • Trap: editing user data later does not rerun it; logs are in /var/log/cloud-init-output.log.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainBootstrap a web server

Write (in words or as a short script) the user data Inkwell would give a new Amazon Linux 2023 server so it installs and starts the nginx web server. Then explain when the script runs, as which user, where to look if it fails, and why the database password should not be placed in it.