Save the cost of a load balancer with route53-update
Tags: aws, route53, cost
Published: 2023-04-24 03:01:20

AWS is really great when it comes to scale. You can spin up hundreds of EC2 instances and very easily scale up to handle traffic spikes, and you can do this almost anywhere in the world.

An essential component in this setup is a load balancer, acting as the entry point in a region which seamlessly balances the traffic to the compute instances that runs your app. When instances are replaced then the load balancer automatically stops sending traffic to the old instance and when the replacement comes online it starts sending traffic to that one.

But what about when you only need a single instance? You might not need or want auto-scaling to occur. The service might be private or you might prefer it to fail rather than to incur more costs. If the instance fails and a new one comes up, how can we ensure that traffic is routed to the new instance? A load balancer costs [about $17 per month](https://aws.amazon.com/elasticloadbalancing/pricing/), which in many cases is more than the compute required for the app itself.

A common solution to this problem is to allocate an Elastic IP and then associate the EIP to the new instance in the userdata script.

But in some cases an EIP is not appropriate (e.g. when using private IP addresses inside of the VPC), and it is just simpler to rely on DNS. It is easy to write a script that does this for you (you can find several options on GitHub).

I recently had this problem with Amazon ECS and I couldn't find a solution that perfectly solved my use cases. Sometimes I need to update the DNS to a private IP address, and sometimes to a public IP address. Sometimes I run the ECS task on EC2 and sometimes I run it on Fargate. To add to this, I want the program to be as small as possible to reduce the time and resources it takes to run ([the official aws-cli docker image](https://hub.docker.com/r/amazon/aws-cli) is more than 100 MB).

So I decided to write my own tool: [route53-update](https://github.com/stefansundin/route53-update)

The program is still under development but so far there is a beta docker image published that can update DNS based on an ECS task's public or private IP address. You can also specify an argument to update the DNS to a value from a URL (e.g. https://checkip.amazonaws.com/). Once the program has matured a bit more then I will start publishing binaries to make it easier to install. A bonus for writing my own tool is that it gives me another opportunity to use Rust in a new project. The AWS SDK for Rust is slowly maturing so I am using Rust more and more in new AWS projects (the SDK is not stable yet but in my opinion it is completely fine to use in new hobby projects).

The GitHub repository [contains an example](https://github.com/stefansundin/route53-update/blob/9f4959a564612fbbed7095aed144290430c46954/examples/ecs/rssbox.json) showing how to plug it into your Amazon ECS Task Definition. I will add more examples in the future, like how to download and run it in a userdata script.

One incentive for writing route53-update is [RSS Box](https://github.com/stefansundin/rssbox). I am working to make it simpler to run on AWS. I want people to be able to run it without requiring a costly load balancer, and route53-update is one small piece of the solution.

That's all for today. The program is very early in its development, but my hope is that it will be very versatile and eventually support many different deployment configurations.