Skip to content

Commit

Permalink
fix: retry aws metadata token download (#3292)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdarwin authored Jul 20, 2023
1 parent 999d139 commit 5537474
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
21 changes: 17 additions & 4 deletions modules/runners/templates/start-runner.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,38 @@

Write-Host "Retrieving TOKEN from AWS API"
$token=Invoke-RestMethod -Method PUT -Uri "http://169.254.169.254/latest/api/token" -Headers @{"X-aws-ec2-metadata-token-ttl-seconds" = "180"}
if ( ! $token ) {
$retrycount=0
do {
echo "Failed to retrieve token. Retrying in 5 seconds."
Start-Sleep 5
$token=Invoke-RestMethod -Method PUT -Uri "http://169.254.169.254/latest/api/token" -Headers @{"X-aws-ec2-metadata-token-ttl-seconds" = "180"}
$retrycount=$retrycount + 1
if ( $retrycount -gt 40 )
{
break
}
} until ($token)
}

$ami_id=Invoke-RestMethod -Uri "http://169.254.169.254/latest/meta-data/ami-id" -Headers @{"X-aws-ec2-metadata-token" = $token}

$metadata=Invoke-RestMethod -Uri "http://169.254.169.254/latest/dynamic/instance-identity/document" -Headers @{"X-aws-ec2-metadata-token" = $token}

$Region = $metadata.region
Write-Host "Reteieved REGION from AWS API ($Region)"
Write-Host "Retrieved REGION from AWS API ($Region)"

$InstanceId = $metadata.instanceId
Write-Host "Reteieved InstanceId from AWS API ($InstanceId)"
Write-Host "Retrieved InstanceId from AWS API ($InstanceId)"

$tags=aws ec2 describe-tags --region "$Region" --filters "Name=resource-id,Values=$InstanceId" | ConvertFrom-Json
Write-Host "Retrieved tags from AWS API"

$environment=$tags.Tags.where( {$_.Key -eq 'ghr:environment'}).value
Write-Host "Reteieved ghr:environment tag - ($environment)"
Write-Host "Retrieved ghr:environment tag - ($environment)"

$runner_name_prefix=$tags.Tags.where( {$_.Key -eq 'ghr:runner_name_prefix'}).value
Write-Host "Reteieved ghr:runner_name_prefix tag - ($runner_name_prefix)"
Write-Host "Retrieved ghr:runner_name_prefix tag - ($runner_name_prefix)"

$ssm_config_path=$tags.Tags.where( {$_.Key -eq 'ghr:ssm_config_path'}).value
Write-Host "Retrieved ghr:ssm_config_path tag - ($ssm_config_path)"
Expand Down
2 changes: 1 addition & 1 deletion modules/runners/templates/start-runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Retrieve instance metadata

echo "Retrieving TOKEN from AWS API"
token=$(curl -f -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 180")
token=$(curl --retry 20 -f -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 180")

ami_id=$(curl -f -H "X-aws-ec2-metadata-token: $token" -v http://169.254.169.254/latest/meta-data/ami-id)

Expand Down

0 comments on commit 5537474

Please sign in to comment.