Enhance your career prospects with our Data Science TrainingKNOW MORE
Bash script is a simple text file that contains commands which are used in the command line.
When an Amazon EC2 instance is launched, user data can be passed to the instance in different ways. This user data can be used to further perform common automated configuration tasks as well as run scripts once the instance has been started.
User data type which can be passed to Amazon EC2 includes: Shell scripts, and cloud-init directives. Shell scripting is considered to be a simple way of sending user data to an EC2 instance.
Shell scripts should begin with #! characters and it should be followed by the path to the interpreter which is to be used to read the script (the commonly used path is /bin/bash). When scripts are entered as user data, they are executed as a ‘root’ user, which means the ‘sudo’ command shouldn’t be used inside the script. Any file which is created by user is owned by the root, and if a non-root user somehow gains permission to access these files, the permissions have to be immediately modified. Since the script doesn’t run interactively, users who write the script should make sure they don’t use commands that require user feedback (an example is yum update without providing the -y flag).
When the processing of user provided script begins, it is first copied to a location and then executed. The path usually looks like this- /var/lib/cloud/instance/instance-id/. The script doesn’t get deleted after it completes its run. But the user has to make sure that they delete the script from the above-mentioned path before creating an AMI from the same instance. Otherwise, when any instance from AMI gets launched, the script will still reside in the same directory.
The user has the control over specifying the instance user data when the instance is launched. If the root volume of the instance to be launched is an EBS volume, it can also be stopped, and the user data can be updated, and then specified again to launch it.
Some amount of time has to be given for the instance to launch and execute the commands which are provided in the script. The user can then check to see if the tasks mentioned in the script have been completed successfully or not.
We understood how user data can be passed to an Amazon EC2 instance with the help of a bash script.