Interview Prep/Shell Scripting/Scenarios: deployments, user management and fleet operations

An EC2 instance's user-data Bash script sometimes fails half-way, and you cannot see why. How do you debug and harden it?

BeginnerScenarioScenarios: deployments, user management and fleet operations
Answer

Situation read → User data runs once at first boot via cloud-init, as root, with no terminal.

What I'd do →

  1. Read the logs: /var/log/cloud-init-output.log (script output) and /var/log/cloud-init.log; the script itself is at /var/lib/cloud/instance/scripts/part-001.
  2. Add #!/bin/bash, set -Eeuxo pipefail, and an ERR trap that logs the line.
  3. Wait for network and package locks: retry apt-get update with backoff; cloud-init status --wait in dependent steps.
  4. Make steps idempotent so cloud-init clean && reboot (or rerunning the script) is safe.
  5. Signal completion or failure (for example cfn-signal, an ASG lifecycle hook, or a tag), so failures are visible.

Why → First-boot scripts fail on timing (network not ready, apt locks), and without logs and signals nobody notices.

Interview tip → Mention baking images with Packer to move most of user data out of boot time.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Scenarios: deployments, user management and fleet operations