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.