Neal Magee, Ph.D.
Solution Architect, Research Computing
University of Virginia, Fall 2022
Bootstrapping is the process of bringing a resource onlne pre-loaded with OS updates, packages, and any software required to run without human intervention or further configuration. You can bootstrap a "barebones" instance of a particular OS distribution, or further bootstrap an already-customized AMI. Bootstrapping only occurs upon instance creation.
This lab introduces the basics of updating your EC2 instance upon creation.
BEFORE you start this lab, be sure to watch the How To: Create an EC2 Instance video if you are not familiar with the basics of creating an instance in EC2.
yum package manager as a start, and loading other resources as necessary.
yum to update the OS.
yum to install specific packages.
yum to install tools that install other supporting libraries or resources.
#!/bin/bash yum update -yA few notes:
#!/bin/bash shebang at the start. The instance needs some interpreter or shell to parse the following commands, so this is required.sudo or sudo su command within the script to escalate permissions.-y flag has been included, since bootstrapping scripts are non-interactive. That is, you will not be present to answer "Y" or "N" when asked if you want to install new packages.
#!/bin/bash /usr/bin/yum update -y /bin/amazon-linux-extras install -y epel /usr/bin/yum install -y git python3 python3-dev python3-pip nfs-utils /bin/pip3 install boto3 pandas requestsA few notes:
$PATH of a bootstrap script. Therefore it is often useful to define the full path for every command. (This is the same for cron jobs.)cloud-init tool. This allows bootstrapping to be
much less script-driven and more declarative, resembling management tools like Ansible. AWS, Google and Azure all support cloud-init. Here is an example of a cloud-init script.
#cloud-config
repo_update: true
repo_upgrade: all
packages:
- httpd
- mariadb-server
runcmd:
- [ sh, -c, "amazon-linux-extras install -y lamp-mariadb10.2-php7.2 php7.2" ]
- systemctl start httpd
- systemctl enable httpd
- [ sh, -c, "usermod -a -G apache ec2-user" ]
- [ sh, -c, "chown -R ec2-user:apache /var/www" ]
- chmod 2775 /var/www
- [ find, /var/www, -type, d, -exec, chmod, 2775, {}, \; ]
- [ find, /var/www, -type, f, -exec, chmod, 0664, {}, \; ]
- [ sh, -c, 'echo "" > /var/www/html/phpinfo.php' ]
#!/bin/bash
/bin/curl -O https://gist.githubusercontent.com/nmagee/acb6249ba451c03fd921f0d6d0f442d5/raw/5816fb54963826f953da166bd623a9ba0cd9fa76/bootstrap.sh
/bin/bash bootstrap.sh
A few notes:
curl is already available on the instance and does not need to be installed.-O flag for curl means the script is downloaded as named.chmod to 755 and then executed ./bootstrap.sh or executed against bashsudo yum history also help show you what has completed.
[root@ip-172-31-93-10 ec2-user]# sudo yum history
Loaded plugins: priorities, update-motd, upgrade-helper
ID | Command line | Date and time | Action(s) | Altered
-------------------------------------------------------------------------------
3 | install htop jq -y | 2021-09-27 18:07 | Install | 4
2 | update -y | 2021-09-27 18:06 | Update | 4
1 | -t -y --exclude=kernel - | 2021-09-27 18:06 | Update | 1
aws ec2 run-instances --image-id ami-abcd1234 --count 1 --instance-type m3.medium \ --key-name my-key-pair --subnet-id subnet-abcd1234 --security-group-ids sg-abcd1234 \ --user-data file://my_script.txt
Your assignment in this lab is to bootstrap an EC2 instance yourself. Select 5 of the following packages or tools to install. To evaluate succcess,
simply shell into the instance after the instance has been created (give the instance enough time to complete your setup) and verify. For more advanced
bootstrapping of a service or daemon, open up the relevant port in your security group and test remotely. For instance, the nginx web
server is a fairly simple daemon to bootstrap, and you can then use http://your-instance-ip-address/ to verify.
Refer to Running commands on your Linux instance at Launch for more detail.
python3boto3gitnginxapache2redisaws-clijqmysql-server (advanced)docker (advanced)
Paste your working bootstrap code into the space where provided in Collab.