|
3 | 3 |
|
4 | 4 | Write-Host "Retrieving TOKEN from AWS API"
|
5 | 5 | $token=Invoke-RestMethod -Method PUT -Uri "http://169.254.169.254/latest/api/token" -Headers @{"X-aws-ec2-metadata-token-ttl-seconds" = "180"}
|
| 6 | +if ( ! $token ) { |
| 7 | + $retrycount=0 |
| 8 | + do { |
| 9 | + echo "Failed to retrieve token. Retrying in 5 seconds." |
| 10 | + Start-Sleep 5 |
| 11 | + $token=Invoke-RestMethod -Method PUT -Uri "http://169.254.169.254/latest/api/token" -Headers @{"X-aws-ec2-metadata-token-ttl-seconds" = "180"} |
| 12 | + $retrycount=$retrycount + 1 |
| 13 | + if ( $retrycount -gt 40 ) |
| 14 | + { |
| 15 | + break |
| 16 | + } |
| 17 | + } until ($token) |
| 18 | +} |
6 | 19 |
|
7 | 20 | $ami_id=Invoke-RestMethod -Uri "http://169.254.169.254/latest/meta-data/ami-id" -Headers @{"X-aws-ec2-metadata-token" = $token}
|
8 | 21 |
|
9 | 22 | $metadata=Invoke-RestMethod -Uri "http://169.254.169.254/latest/dynamic/instance-identity/document" -Headers @{"X-aws-ec2-metadata-token" = $token}
|
10 | 23 |
|
11 | 24 | $Region = $metadata.region
|
12 |
| -Write-Host "Reteieved REGION from AWS API ($Region)" |
| 25 | +Write-Host "Retrieved REGION from AWS API ($Region)" |
13 | 26 |
|
14 | 27 | $InstanceId = $metadata.instanceId
|
15 |
| -Write-Host "Reteieved InstanceId from AWS API ($InstanceId)" |
| 28 | +Write-Host "Retrieved InstanceId from AWS API ($InstanceId)" |
16 | 29 |
|
17 | 30 | $tags=aws ec2 describe-tags --region "$Region" --filters "Name=resource-id,Values=$InstanceId" | ConvertFrom-Json
|
18 | 31 | Write-Host "Retrieved tags from AWS API"
|
19 | 32 |
|
20 | 33 | $environment=$tags.Tags.where( {$_.Key -eq 'ghr:environment'}).value
|
21 |
| -Write-Host "Reteieved ghr:environment tag - ($environment)" |
| 34 | +Write-Host "Retrieved ghr:environment tag - ($environment)" |
22 | 35 |
|
23 | 36 | $runner_name_prefix=$tags.Tags.where( {$_.Key -eq 'ghr:runner_name_prefix'}).value
|
24 |
| -Write-Host "Reteieved ghr:runner_name_prefix tag - ($runner_name_prefix)" |
| 37 | +Write-Host "Retrieved ghr:runner_name_prefix tag - ($runner_name_prefix)" |
25 | 38 |
|
26 | 39 | $ssm_config_path=$tags.Tags.where( {$_.Key -eq 'ghr:ssm_config_path'}).value
|
27 | 40 | Write-Host "Retrieved ghr:ssm_config_path tag - ($ssm_config_path)"
|
|
0 commit comments