summary refs log tree commit diff
path: root/nixos/modules/virtualisation/ec2-metadata-fetcher.nix
diff options
context:
space:
mode:
authorGraham Christensen <graham@grahamc.com>2020-11-19 12:59:07 -0500
committerGraham Christensen <graham@grahamc.com>2020-11-19 13:56:44 -0500
commitf2cfecdec357c1c449aef2884c70b6a4d79b30e3 (patch)
treea17f8b2a38107e21d556ef55e6038674970a2545 /nixos/modules/virtualisation/ec2-metadata-fetcher.nix
parent83ea88e03fe2775601636c5f578b63276910a538 (diff)
downloadnixpkgs-f2cfecdec357c1c449aef2884c70b6a4d79b30e3.tar
nixpkgs-f2cfecdec357c1c449aef2884c70b6a4d79b30e3.tar.gz
nixpkgs-f2cfecdec357c1c449aef2884c70b6a4d79b30e3.tar.bz2
nixpkgs-f2cfecdec357c1c449aef2884c70b6a4d79b30e3.tar.lz
nixpkgs-f2cfecdec357c1c449aef2884c70b6a4d79b30e3.tar.xz
nixpkgs-f2cfecdec357c1c449aef2884c70b6a4d79b30e3.tar.zst
nixpkgs-f2cfecdec357c1c449aef2884c70b6a4d79b30e3.zip
nixos ami: preflight the imds token
According to Freenode's ##AWS, the metadata server can sometimes
take a few moments to get its shoes on, and the very first boot
of a machine can see failed requests for a few moments.
Diffstat (limited to 'nixos/modules/virtualisation/ec2-metadata-fetcher.nix')
-rw-r--r--nixos/modules/virtualisation/ec2-metadata-fetcher.nix21
1 files changed, 21 insertions, 0 deletions
diff --git a/nixos/modules/virtualisation/ec2-metadata-fetcher.nix b/nixos/modules/virtualisation/ec2-metadata-fetcher.nix
index 247bcf513c5..812e93ec4aa 100644
--- a/nixos/modules/virtualisation/ec2-metadata-fetcher.nix
+++ b/nixos/modules/virtualisation/ec2-metadata-fetcher.nix
@@ -26,6 +26,19 @@
       http://169.254.169.254/latest/api/token
   }
 
+  preflight_imds_token() {
+    # retry-delay of 1 selected to give the system a second to get going,
+    # but not add a lot to the bootup time
+    ${curl}/bin/curl \
+      -v \
+      --retry 3 \
+      --retry-delay 1 \
+      --fail \
+      --connect-timeout 1 \
+      -H "X-aws-ec2-metadata-token: $IMDS_TOKEN" \
+      http://169.254.169.254/1.0/meta-data/instance-id
+  }
+
   try=1
   while [ $try -le 3 ]; do
     echo "(attempt $try/3) getting an EC2 instance metadata service v2 token..."
@@ -38,6 +51,14 @@
     echo "failed to fetch an IMDS2v token."
   fi
 
+  try=1
+  while [ $try -le 10 ]; do
+    echo "(attempt $try/10) validating the EC2 instance metadata service v2 token..."
+    preflight_imds_token && break
+    try=$((try + 1))
+    sleep 1
+  done
+
   echo "getting EC2 instance metadata..."
 
   if ! [ -e "$metaDir/ami-manifest-path" ]; then