-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle eventually-consistent PrivateDnsName
- Loading branch information
1 parent
c2cf65d
commit cf837ed
Showing
2 changed files
with
44 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -o errexit | ||
set -o pipefail | ||
set -o nounset | ||
|
||
# Retrieves the PrivateDnsName from EC2 for this instance, waiting until | ||
# it is available if necessary (due to eventual consistency). | ||
|
||
function log { | ||
echo >&2 "$(date '+%Y-%m-%dT%H:%M:%S%z')" "[private-dns-name]" "$@" | ||
} | ||
|
||
INSTANCE_ID=$(imds /latest/meta-data/instance-id) | ||
|
||
# the AWS CLI currently constructs the wrong endpoint URL on localzones (the availability zone group will be used instead of the parent region) | ||
# more info: https://github.com/aws/aws-cli/issues/7043 | ||
REGION=$(imds /latest/meta-data/placement/region) | ||
|
||
MAX_ATTEMPTS=25 | ||
ATTEMPT_INTERVAL=5 | ||
|
||
ATTEMPT=0 | ||
while true; do | ||
PRIVATE_DNS_NAME=$(AWS_RETRY_MODE=standard AWS_MAX_ATTEMPTS=10 aws ec2 describe-instances --region $REGION --instance-ids $INSTANCE_ID --query 'Reservations[].Instances[].PrivateDnsName' --output text) | ||
if [ ! "${PRIVATE_DNS_NAME}" = "" ] || [ ${ATTEMPT} -gteq ${MAX_ATTEMPTS} ]; then | ||
break | ||
fi | ||
ATTEMPT=$((ATTEMPT + 1)) | ||
JITTER=$(seq -1 1 | shuf -n1) | ||
DELAY=$((ATTEMPT_INTERVAL + JITTER)) | ||
log "WARN: PrivateDnsName is not available, waiting for ${DELAY} seconds..." | ||
sleep ${DELAY} | ||
done | ||
|
||
if [ "${PRIVATE_DNS_NAME}" = "" ]; then | ||
log "ERROR: failed to retrieve PrivateDnsName after ${ATTEMPT} attempts!" | ||
exit 1 | ||
else | ||
log "INFO: retrieved PrivateDnsName: ${PRIVATE_DNS_NAME}" | ||
echo "${PRIVATE_DNS_NAME}" | ||
exit 0 | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters