1 metaDir
=/etc
/ec2-metadata
2 mkdir
-m 0755 -p "$metaDir"
6 # retry-delay of 1 selected to give the system a second to get going,
7 # but not add a lot to the bootup time
16 -H "X-aws-ec2-metadata-token-ttl-seconds: 600" \
17 http
://169.254.169.254/latest
/api
/token
20 preflight_imds_token
() {
21 # retry-delay of 1 selected to give the system a second to get going,
22 # but not add a lot to the bootup time
30 -H "X-aws-ec2-metadata-token: $IMDS_TOKEN" \
32 http
://169.254.169.254/1.0/meta-data
/instance-id
36 while [ $try -le 3 ]; do
37 echo "(attempt $try/3) getting an EC2 instance metadata service v2 token..."
38 IMDS_TOKEN
=$
(get_imds_token
) && break
43 if [ "x$IMDS_TOKEN" == "x" ]; then
44 echo "failed to fetch an IMDS2v token."
48 while [ $try -le 10 ]; do
49 echo "(attempt $try/10) validating the EC2 instance metadata service v2 token..."
50 preflight_imds_token
&& break
55 echo "getting EC2 instance metadata..."
58 # --fail to avoid populating missing files with 404 HTML response body
59 # || true to allow the script to continue even when encountering a 404
60 curl
--silent --show-error --fail --header "X-aws-ec2-metadata-token: $IMDS_TOKEN" "$@" || true
63 get_imds
-o "$metaDir/ami-manifest-path" http
://169.254.169.254/1.0/meta-data
/ami-manifest-path
64 (umask 077 && get_imds
-o "$metaDir/user-data" http
://169.254.169.254/1.0/user-data
)
65 get_imds
-o "$metaDir/hostname" http
://169.254.169.254/1.0/meta-data
/hostname
66 get_imds
-o "$metaDir/public-keys-0-openssh-key" http
://169.254.169.254/1.0/meta-data
/public-keys
/0/openssh-key