Route 53 is a very useful service from AWS. It allows you to route traffic to a variety of resources like Loadbalancers and static IP addresses. Using Route 53 with a NLB/ALB is pretty straightforward. When it comes to single instance workloads, it is simpler to create route 53 entries directly to the instance's public IP address. However, this approach becomes complex when the instance is a spot instance. With Spot instance, IP address can change for every new spot and dynamically configuring IP address will be challenging.

Standalone workloads with spot instance using Lightwing Managed VM

Usually single instance workloads are not a good candidate for Spot instances because spot instances can get interrupted by AWS with a two minute notice at any point in time.

Lightwing Managed VM is a product which lets you run single instance, persistent workloads on spot instances. This is a very cost effective and efficient way to run single instance workloads on Spot Instances. Spot instances are typically 70-80% cheaper than the on-demand instances.

Lightwing Managed VM will handle the spot workflow required including persisting the storage and network interfaces which gracefully gets attached to the instances. When spot instance gets an interruption notice, Managed VM will replace that instance with a new instance. All these happens without any manual intervention and a fully seamless experience.

Routing to Managed VM using Route 53

We will use a user data script to register the Managed VM to Route 53. Whenever managed vm is facing an interruption, a new spot instance will be launched and Lightwing will execute the provided user data script which will register the instance public IP to the hosted zone in Route 53.

To begin, lets create a new role which can update the Route 53 records. This role can have the following permissions.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": "route53:ChangeResourceRecordSets",
            "Resource": "arn:aws:route53:::hostedzone/<replacethis>"
        }
    ]
}
AWS Role

When creating a Managed VM, we will assign this role as the instance profile.

The following user data will be used to register instance onto Route 53.

#!/bin/bash

# VARIABLES
HOSTED_ZONE_ID="Hosted zone id goes here"
RECORD_NAME="record name goes here (eg:staging.lightwingtest.com)"

# AWS-CLI INSTALLATION
apt install unzip -y
apt-get install python3-distutils -y
curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "awscli-bundle.zip"
unzip awscli-bundle.zip
/usr/bin/python3 awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws
rm -rf awscli-bundle*

# ROUTE53 REGISTRATION
IP=$(curl http://169.254.169.254/latest/meta-data/public-ipv4)

echo '{
            "Comment": "UPSERT a record ",
            "Changes": [{
            "Action": "UPSERT",
                        "ResourceRecordSet": {
                                    "Name": "'$RECORD_NAME'",
                                    "Type": "A",
                                    "TTL": 300,
                                 "ResourceRecords": [{ "Value": "'$IP'"}]
}}]
}
' > /tmp/route_record.json

aws route53 change-resource-record-sets --hosted-zone-id $HOSTED_ZONE_ID  --change-batch file:///tmp/route_record.json

This user data script does the following:

  1. Install AWS CLI on to the instance
  2. Call the instance metadata API to get the public IP address
  3. Uses AWS CLI to update the public IP address to Route 53

Summary

Using Lightwing Managed VM helps you to run long running spot instances and using the above method, Route 53 entry can be used to route traffic to the server. This approach is convenient for smaller applications where load balancer is an overhead.

Lightwing is an intelligent control system that dynamically optimizes cloud resources to reduce cloud compute costs by up to 90 percent on AWS, Azure and Digital Ocean. All this with a simple and fast one-time setup that requires no further manual intervention thereon, and no change to the company's development workflows.

Click here to get started for free!