How can I set ordinal based DNS names for ASG instances

547 views Asked by At

Problem

In my dev environment I want to create easy to remember ordinal dns names for ECS cluster hosts. When we deploy we scale out from 1 to 2 hosts and then drain/scale back in.

At present we use userdata like so so set the dns name

INSTANCE_ID=$(curl http://169.254.169.254/latest/meta-data/instance-id)
INSTANCE_IP=$(curl http://169.254.169.254/latest/meta-data/local-ipv4)
RECORD_CONFIG="/tmp/ecs-a-record.json"

cat >>$RECORD_CONFIG << ROUTE53
{
  "Comment": "Create a friendly DNS name for the DOD ECS host",
  "Changes": [{
  "Action": "UPSERT",
    "ResourceRecordSet": {
      "Name": "dev-ecs.ourenv.dev",
      "Type": "A",
      "TTL": 300,
      "ResourceRecords": [{ "Value": "$INSTANCE_IP"}]
    }
  }]
}
ROUTE53

aws route53 change-resource-record-sets --hosted-zone-id ... --change-batch file://$RECORD_CONFIG

I think my options are

  • probe dns name and if its in use, increment a counter, try again with N+1
  • use aws cli to gather data about the ASG instances and use launch time to determine ordinal name

Does anyone know of a more elegant solution?

1

There are 1 answers

0
Peter Kahn On BEST ANSWER

So, here's how we manage convenience fqdns records for devTest environments. This becomes important when we scale the cluster to ensure the 2nd/3rd hosts don't attempt to take the 1st host's name

checkHostExists() {
  host=$1
  nc -z $host 22 >> /dev/null 2>&1 ; echo $?
}

findDevTestDNSName() {
  base_name=$1
  domain=$2
  count=1
  name=$(printf "%s%02d.%s" $base_name $count $domain)

  while [[ "$(checkHostExists $name)" -eq "0"  ]]
  do
    count=$((count+1))
    name=$(printf "%s%02d.%s" $base_name $count $domain)
  done
  echo $name
}


##
# Main Userdata context
#
# Please Note: This is a Terraform template
#  ${foo} refers to a foo variable passed to the template
#  $${bar} refers to an actual env variable
# when terraform resolves the template then tf vars are replaced with
# concrete values in the usedata
##

# lookup the instance ID
INSTANCE_ID=$(curl http://169.254.169.254/latest/meta-data/instance-id)
INSTANCE_IP=$(curl http://169.254.169.254/latest/meta-data/local-ipv4)

if [[ "${cluster_name}" =~ "devtest" ]]; then
  # create a friendly hostname for the ECS host if this is a development test env

  # install nmap/nc for host probing
  yum install -y nmap

  ECS_HOST_FQDNS=$(findDevTestDNSName ${cluster_name}-ecs ${dns_domain})
  HOST_NAME=$(echo $ECS_HOST_FQDNS|sed 's/\..*//')
  echo Setting DevTest ECS Hostname: $${ECS_HOST_FQDNS}

    RECORD_CONFIG="/tmp/ecs-a-record.json"
    cat >>$RECORD_CONFIG << ROUTE53
    {
      "Comment": "Create a friendly DNS name for the ${cluster_name} ECS host",
      "Changes": [{
      "Action": "UPSERT",
        "ResourceRecordSet": {
          "Name": "$${ECS_HOST_FQDNS}",
          "Type": "A",
          "TTL": 300,
          "ResourceRecords": [{ "Value": "$INSTANCE_IP"}]
        }
      }]
    }
    ROUTE53

    aws route53 change-resource-record-sets --hosted-zone-id ${dns_zone_id} --change-batch file://$RECORD_CONFIG

fi