What is User Data In AWS

User data in AWS some command or script which can be run during launching of an instance. You can provide the user-data while launching the instance from AWS console. For example, you want to have certain packages installed or some configuration files to be present on the instance after the launch, user data is the thing you need. Whatever commands you specify in the user data gets executed and you get the stuff when instance is launched.

Let's see an example, Below is the user data which is creating a file and we are putting some content in it. "set -x" option will start the debugging mode and all the standard output will get logged in /var/log/user-data.log file, so after the instance is launched, you can see what all commands gets executed and which commands got failed.
#!/bin/bash
# set debug mode
set -x

# output log of userdata to /var/log/user-data.log
exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1
# create and put some content in the file
touch /home/ec2-user/created_by_userdata.txt
(
cat << 'EOP'
Hey there!!!
EOP
) > /home/ec2-user/created_by_userdata.txt
You will get the option to put user data in “Configure Instance Details page” as shown below:
 
A good use-case for user data could be, bootstrapping autoscaled instance with chef. Here is the complete post explaining this use case.

Leave a Reply

Your email address will not be published. Required fields are marked *