An engineer adds a shell script as user data to an already-running Linux EC2 instance's configuration, expecting it to reinstall a monitoring agent every time the instance reboots. After the script runs successfully at initial launch, the engineer reboots the instance and finds the script did not run again. Why?
- User data scripts can only ever run once per instance for the lifetime of that instance, with no way to change this
- The script failed silently the second time, because reboots automatically clear all instance user data
- By default, user data scripts and cloud-init directives run only during the first boot cycle at launch, not on subsequent reboots or starts, unless explicitly configured to run every time
- Reboots never re-read user data at all; only a full stop and start re-reads it
Why C? And why not the others?
Correct answer: C. By default, user data scripts and cloud-init directives run only during the first boot cycle at launch, not on subsequent reboots or starts, unless explicitly configured to run every time
Amazon EC2 runs user data shell scripts and cloud-init directives only during the initial boot cycle when an instance is first launched; making a script run on every subsequent reboot or start requires explicitly opting into that behavior, for example by adding cloud-init's scripts-user "always" configuration on Linux, which the engineer's script did not include, so it correctly ran once at launch and then stayed dormant on the later reboot. This is a deliberate default rather than a permanent limitation, since once persistence is configured the same script can run on every start. User data is not cleared by a reboot either; it remains stored as an instance attribute and is simply not re-executed unless configured to be. The claim that only a full stop and start, and never a reboot, re-reads user data is also inaccurate, because without the persistence configuration, neither a reboot nor a stop and start re-runs the script — both follow the exact same first-boot-only default.
Source: AWS EC2 documentation: Run commands when you launch an EC2 instance with user data input — user data execution