From ef46827e31d3aaabfe9676df4caad756eb1f3dc8 Mon Sep 17 00:00:00 2001 From: Sam Darwin Date: Tue, 8 Aug 2023 10:50:21 -0600 Subject: [PATCH] fix(runners): retry aws metadata token download on Linux (#3408) Adding more thorough logic to fully retry the aws metadata token download, on Linux. Continuing https://github.com/philips-labs/terraform-aws-github-runner/pull/3292. See explanation there. I have tested and it works, however Ubuntu usually succeeds. We will observe further how this goes or if more changes are needed. --- modules/runners/templates/start-runner.sh | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/modules/runners/templates/start-runner.sh b/modules/runners/templates/start-runner.sh index 4fe035a0e0..29788c9216 100644 --- a/modules/runners/templates/start-runner.sh +++ b/modules/runners/templates/start-runner.sh @@ -3,7 +3,19 @@ ## Retrieve instance metadata echo "Retrieving TOKEN from AWS API" -token=$(curl --retry 20 -f -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 180") +token=$(curl -f -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 180" || true) +if [ -z "$token" ]; then + retrycount=0 + until [ -n "$token" ]; do + echo "Failed to retrieve token. Retrying in 5 seconds." + sleep 5 + token=$(curl -f -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 180" || true) + retrycount=$((retrycount + 1)) + if [ $retrycount -gt 40 ]; then + break + fi + done +fi ami_id=$(curl -f -H "X-aws-ec2-metadata-token: $token" -v http://169.254.169.254/latest/meta-data/ami-id)