Start a script at instance start

Prasad
Feb 7, 2024

When launching an EC2 instance, many are familiar with using User Data to execute a script upon the instance’s initial boot. This capability stems from Cloud-Init, maintained by Canonical, which is the standard for multi-distribution cloud instance initialization.

Originally, User Data was designed for passing information to applications (e.g., database passwords). Cloud-Init can execute scripts:

  • On every boot
  • - On the next boot only

To run a script every time the instance starts, place it in `/var/lib/cloud/scripts/per-boot/`. This approach is useful for tasks like triggering a batch process on startup, where the script can retrieve data from a database, SQS queue, or even the instance’s User Data. Note that User Data can only be updated when the instance is in the Stopped state.

--

--

Prasad

I am a OpenSource Enthusiast|Python Lover who attempts to find simple explanations for questions and share them with others