AWS EC2 User Data Script Sample Tutorial |Video upload date:  · Duration: PT1M0S  · Language: EN

Learn how to use EC2 user data scripts to install packages start services restart instances and capture logs during launch

Quick warning before you get cute

User data is the little magic wand AWS hands to you when an EC2 instance first wakes up. Use it to install packages run a startup script boot strap your app and write logs so you do not end up guessing why the server is sulking. This guide covers AWS EC2 user data for Linux with CloudInit friendly tips and real world logging advice.

Why user data matters on first boot

When an instance boots the cloud platform hands your script to either cloud init or the distro init system. The script runs as root so it can install software enable services and touch files that normal users cannot. That power is useful and also terrifying if you do not log output or enable debugging.

Core steps for a reliable startup script

  • Create a plain text shell script with a shebang so the runtime knows what to do
  • Use the package manager that matches your Linux distro such as apt for Debian and Ubuntu or yum dnf for RHEL based systems
  • Redirect stdout and stderr to a persistent log file so troubleshooting is possible
  • Enable and start services with systemctl or the distro init commands you trust
  • Verify logs from the console or over SSH and reboot only when necessary

Sample user data script you can copy and test

#!/bin/bash
# simple EC2 user data to install nginx log output and reboot if needed
exec > /var/log/user-data.log 2>&1
set -x

# update and install for Debian and Ubuntu
if command -v apt-get >/dev/null 2>&1; then
  apt-get update -y
  apt-get install -y nginx
elif command -v yum >/dev/null 2>&1; then
  yum update -y
  yum install -y nginx
fi

systemctl enable nginx
systemctl start nginx

echo "Hello from root user" > /var/www/html/index.html
# reboot only if you actually need to complete a package driven kernel update
# reboot

This is minimal but practical. It detects whether apt or yum exists and uses the right package manager. It sends all output to /var/log/user-data.log and turns on bash tracing with set -x so you can see what failed if things go sideways.

Notes about CloudInit and init scripts

CloudInit reads user data and decides how to run it. On many images a plain shell script works out of the box. Some AMIs expect cloud init directives. If your distro includes CloudInit you can also use cloud init syntax to write files or run commands with nicer lifecycle control. Do not confuse CloudInit with the package manager though. They play together but are not the same thing.

Debugging tips that save time

  • Check system console output from the EC2 console when you cannot SSH in
  • Always redirect stdout and stderr to a log file so you do not play the guessing game
  • Use set -x early in the script for verbose command tracing
  • Test locally in a disposable VM before launching lots of instances
  • If a reboot interrupts your work comment out reboot until you confirm the flow

Logging and auditing best practices

Put logs somewhere persistent so they survive reboots and so automation can collect them. /var/log/user-data.log is conventional but you can forward logs to CloudWatch Logs or a centralized log server for real production setups. Logging makes debugging less fun but far faster.

Final checklist before you launch

  • Script begins with a shebang and is plain text
  • All output is redirected to a log file
  • Package manager commands match the Linux distro
  • Services are enabled and started with systemctl or the distro equivalent
  • You tested the script on an instance or VM first

There you go. Use this to bootstrap EC2 instances with CloudInit friendly startup scripts that install packages enable services and log what happened. Your future self will thank you and your ops team will suspect witchcraft if you do it right.

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.