User data is the shell script used to bootstrap an EC2 instance at the time of launch. This is very helpful to make custom configurations or changes in the EC2 instance at the launch time. The user-data is just a normal shell script.
By default, the user-data script will execute without any logs. This will be a problem as we do not have an option to debug if something goes wrong. In order to see the logs of the user-data shell script execution, we need to make some minor adjustments in the user-data script.
#!/bin/bash echo "Hello World" echo "This is a sample user data script" yum update -y
To enable logs, we just need to make the following changes in the script.
#!/bin/bash -xe exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1 echo "Hello World" echo "This is a sample user data script" yum update -y
The following line in the script enables the logging to /var/log/user-data and to the console
exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1
This was tested on EC2 instances provisioned using Amazon Linux AMIs.