Sub topic 10
Bootstrapping with user data
This chapter assumes you can launch an instance with the AWS CLI, that you have a security group allowing HTTP, and that you can read a basic shell script. User data lets an instance configure itself the first time it boots, so you don't have to log in over SSH to install software.
What user data is
User data is a block of text you pass to an instance at launch. On Amazon Linux 2023, a service called cloud-init reads it during boot. If the text starts with a shebang line such as #!/bin/bash, cloud-init runs it as a script.
By default, user data runs only once, at the first boot of the instance. It runs as the root user, so you don't need sudo.
A first script
This script installs and starts nginx on Amazon Linux 2023:
#!/bin/bash
dnf install -y nginx
systemctl enable --now nginxSave it as user-data.sh. Pass it to run-instances with the --user-data option. The CLI reads a local file when you prefix the path with file://:
aws ec2 run-instances \
--image-id "$AMI_ID" \
--instance-type t3.micro \
--key-name my-key \
--security-group-ids sg-0123456789abcdef0 \
--subnet-id subnet-0123456789abcdef0 \
--metadata-options HttpTokens=required \
--user-data file://user-data.sh$AMI_ID is the Amazon Linux 2023 image ID you looked up in the launch chapter. The instance must have a route to the internet, and its security group must allow inbound port 80, or you can't reach nginx from your browser.
How the boot sequence works
The script runs after the instance reaches the running state. The instance can therefore show as running and pass its status checks before your script finishes. Wait for the script to complete before you test the service.
Common errors
- The script does nothing. The first line must be a shebang such as
#!/bin/bash. Without it, cloud-init does not treat the text as a script. - A package fails to install. The instance has no route to the internet or to a package mirror. Check the subnet's route table and its public IP or NAT setup.
- Wrong package manager. Amazon Linux 2023 uses
dnf. Ubuntu usesapt. A script written for one fails on the other. - You edited user data and nothing changed. The script already ran on first boot. Stopping and starting the instance does not run it again by default.
- The script is too large. User data is limited to 16 KB before base64 encoding. For longer setup, have the script download a file from Amazon S3.
Debugging a script
Connect over SSH and read the cloud-init log:
sudo cat /var/log/cloud-init-output.logThis file holds the output and errors from your script. Look here first when something is missing.
Changing user data later
You can change user data only while the instance is stopped, using modify-instance-attribute. The new script still won't run unless you configure cloud-init to run per-boot scripts. For repeatable setups, put the script in a launch template and launch new instances from it.
Exercise
You launch an instance with a user data script that installs nginx. The instance shows running and passes its status checks, but your browser can't load the page. Give two possible causes.
Show answer
The script may still be running, because the instance reaches running before user data finishes. The script may also have failed, which you can check in /var/log/cloud-init-output.log. Other valid causes are a security group that doesn't allow inbound port 80, or a missing route to the internet that stopped the package install.