How does EC2 user data work?
IntermediateConceptEC2, Auto Scaling and Load Balancing
Answer
User data (up to 16 KB) is passed to the instance and executed by cloud-init as root on first boot only by default; it can be a shell script (starting with #!) or #cloud-config YAML. Output goes to /var/log/cloud-init-output.log. Changing user data requires the instance to be stopped, and to rerun it you must clear cloud-init state or use a #cloud-boothook/scripts-user: always setting.
User data is visible via the metadata service, so never put secrets in it.