Automatic DNS for EC2
instances on Route53
.
This project is in alpha stage with no stable release. It is currently a really good example from which you can take inspiration or base further developments, do not try to use in production as it is.
If you have ever worked in a project/team with more than one server, you have felt the pain of:
- Keeping track of all your servers
- Distribute and maintain a servers list, maybe with
ssh
aliases - Easily access each server as it's up and running
Pilot53 is a AWS lambda
function listening to EC2
events and automatically registering new servers to Route53
based on their Name
tag.
You create a new EC2
instance with tag Name: web-1
. Pilot53
will receive an event from EC2
as your instance will become running
and will create a new DNS record. Your team will be able to access web-1
from web-1.yourdomain.com
after adding yourdomain.com
to their local DNS search lists.
When creating a new instance, Pilot53
will look for the tag Name
and will use it to create the DNS entry. For example, creating an instance with Name
tag called web-1
will generate a DNS record web-1.yourdomain.xpz
As your instance gets stopped
or terminated
, Pilot53
will remove the related DNS entry
If you wish to swap an instance, simply create a new instance with the same Name
tag. As the new instance is created, Pilot53
will first DELETE
the current DNS entry and create a new one with the updated instance IP. Pay attention: if you do that and the new machine hasn't yet been provisioned, the DNS will be anyway swapped. This will cause downtime of your services until the new machine is ready.
-
Create a new
Hostedzone
inRoute53
. This is where your DNS domain will live. Note down theHosted Zone ID
. -
Create a new
IAM
role that will run your lambda function. It will needReadAccess
onEC2
andFullAccess
onLambda
. -
Create your
lambda
function. From the AWS Console selectAWS Lambda
, click oncreate a Lambda function
and skip the blueprint selection. Here you can name your function, just make sure to selectNode.js >= 4.3
. -
Copy the content of
index.js
in theLambda function code
text area. Make sure to add yourBASE_DOMAIN_NAME
(ex.opengrid.xyz
) and theHOSTED_ZONE_ID
from your Route53. Select asRole
the newly createdIAM role
-
You need now to bind your lambda function to specific
EC2
events. You can do that in theCloudWatch
section of yourAWS Console
. Click onEvents
and create a newrule
. SelectEC2 instance state change notification
, fromspecific state
selectRunning, Shutting Down and Stopping
. Then add a newTarget
where you will need to select your newly createdlambda function
. Choose a name for your Rule definition and simply create it.
If everything went well, you should be good to go.
Note: You can add your base_domain
to your Search Domains
to be able to simply (for example) do ssh <newly-created-instance-name>
This project was developed after a hint/idea from pracucci
Thanks to gabrielelana for co-authoring/contributing.
The project was developed during the Open Source Saturday. If you live in Milan, you should check it out.