0
0
AWScloud~10 mins

Instance metadata and user data in AWS - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Instance metadata and user data
Instance Launch
Access Metadata URL
Retrieve Metadata Info
Access User Data URL
Retrieve User Data Script
Execute User Data Script
Instance Configured
When an instance starts, it can get info about itself from metadata and run setup commands from user data.
Execution Sample
AWS
curl http://169.254.169.254/latest/meta-data/instance-id
curl http://169.254.169.254/latest/user-data
These commands get the instance ID and the user data script from the instance metadata service.
Process Table
StepActionURL AccessedResponseEffect
1Instance starts--Instance boots up
2Request instance IDhttp://169.254.169.254/latest/meta-data/instance-idi-1234567890abcdef0Instance learns its ID
3Request user datahttp://169.254.169.254/latest/user-data#!/bin/bash echo Hello > /var/tmp/greeting.txtInstance gets setup script
4Execute user data script-Script runsFile /var/tmp/greeting.txt created with 'Hello'
5Instance ready--Configured and running
6Exit--No more metadata or user data requests
💡 User data script executed and instance fully configured
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
instance_idundefinedi-1234567890abcdef0i-1234567890abcdef0i-1234567890abcdef0i-1234567890abcdef0
user_data_scriptundefinedundefined#!/bin/bash echo Hello > /var/tmp/greeting.txt#!/bin/bash echo Hello > /var/tmp/greeting.txt#!/bin/bash echo Hello > /var/tmp/greeting.txt
greeting_filedoes not existdoes not existdoes not existexists with content 'Hello'exists with content 'Hello'
Key Moments - 3 Insights
Why do we use the special IP 169.254.169.254 to get metadata?
This IP is a fixed link-local address that all instances can use to get their own metadata without needing internet access, as shown in step 2 of the execution table.
What happens if the user data script is empty or missing?
The instance will not run any setup commands from user data, so no changes happen after step 3; the instance just boots normally.
Can the instance metadata change after launch?
No, metadata like instance ID is fixed at launch and does not change during the instance lifetime, as tracked in the variable 'instance_id'.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the instance ID returned at step 2?
Ainstance-001
Bi-abcdef1234567890
Ci-1234567890abcdef0
DNo ID returned
💡 Hint
Check the 'Response' column in row for step 2 in the execution table.
At which step does the instance execute the user data script?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look for the 'Execute user data script' action in the execution table.
If the user data script was missing, what would happen to the 'greeting_file' variable?
AIt would be created with 'Hello'
BIt would not exist
CIt would contain an error message
DIt would be empty but exist
💡 Hint
Refer to the variable_tracker row for 'greeting_file' and the key moment about missing user data.
Concept Snapshot
Instance metadata is info the instance can get about itself via a special IP.
User data is a script the instance runs at launch to configure itself.
Access metadata at http://169.254.169.254/latest/meta-data/
Access user data at http://169.254.169.254/latest/user-data
User data runs only once at boot.
Metadata is read-only and fixed per instance.
Full Transcript
When an AWS instance starts, it can get information about itself called metadata by accessing a special IP address 169.254.169.254. This metadata includes things like the instance ID. The instance can also get user data, which is a script provided at launch time. The instance runs this script once to set itself up. For example, the instance can get its ID by requesting the metadata URL and get a setup script by requesting the user data URL. Then it runs the script to create files or install software. Metadata is fixed and does not change after launch. User data is optional and runs only once. This process helps automate instance configuration without manual steps.