Read ec2 Dynamic Data From Instance

10 Aug 2024 - rich

There are several ways to get the dynamic data from an ec2 instance. I have run into some instances (ha, pun intended ;) where I am working on the instance and would like to see the values of some of the dynamic instance data.

View Dynamic Data from ec2 Instance

root@ip-10-111-10-49:/# cd /var/lib/cloud

root@ip-10-111-10-49:/var/lib/cloud# pwd
/var/lib/cloud
root@ip-10-111-10-49:/var/lib/cloud# ls -l
total 24
drwxr-xr-x 2 root root 4096 Jul 30 17:12 data
drwxr-xr-x 2 root root 4096 Jul 30 17:12 handlers
lrwxrwxrwx 1 root root   44 Jul 30 17:12 instance -> /var/lib/cloud/instances/i-030f854703117947e
drwxr-xr-x 3 root root 4096 Jul 30 17:12 instances
drwxr-xr-x 6 root root 4096 Jul 30 17:12 scripts
drwxr-xr-x 2 root root 4096 Jul 30 17:12 seed
drwxr-xr-x 2 root root 4096 Jul 30 17:12 sem
root@ip-10-111-10-49:/var/lib/cloud#

view instance-id

root@ip-10-111-10-49:/var/lib/cloud# cat data/instance-id
i-030f854703117947e
root@ip-10-111-10-49:/var/lib/cloud#

get python version

root@ip-10-111-10-49:/var/lib/cloud# cat data/python-version
3.8
root@ip-10-111-10-49:/var/lib/cloud#

view user-data scripts

root@ip-10-111-10-49:/var/lib/cloud/instance# cd /var/lib/cloud/instance/scripts

# cat scripts/part-001

#!/bin/bash
apt -y remove ec2-instance-connect
systemctl disable snapd && systemctl stop snapd
snap remove amazon-ssm-agent
snap remove lxd
snap remove core18
apt -y purge lxd snapd lxd-agent-loader
apt -y autoremove
.
.
.

this data is also written to user-data.txt:

root@ip-10-111-10-49:/var/lib/cloud/# cd /var/lib/cloud/instance

root@ip-10-111-10-49:/var/lib/cloud/instance# cat user-data.txt
#!/bin/bash
apt -y remove ec2-instance-connect
systemctl disable snapd && systemctl stop snapd
snap remove amazon-ssm-agent
snap remove lxd
snap remove core18
apt -y purge lxd snapd lxd-agent-loader
apt -y autoremove
.
.
.