Get into DevOps is a blog about methodologies, practices and tools to improve software delivery.

How to use a custom domain name with AWS EC2 instances

How to use a custom domain name with AWS EC2 instances

In this post I'll show you how to easily point your own custom domain name to Amazon Web Services EC2 instances running Linux.

To accomplish this we'll use Dyname, a free service which I recently built. Dyname will take care of creating DNS records for your domain according to the hostnames you specify.

We'll be going through four simple steps:

  1. Register a domain with a domain registrar
  2. Set domain's nameservers to Dyname
  3. Associate domain with Dyname
  4. Run the Dyname setup on your instance(s)
  5. Optionally: Set up unattended hostname registration on boot

Note: You can use these instructions even if you don't have your own domain: jump right to step 4, set your hostname to end in '.dyname.net' or '.dnm.li', and specify an empty Domain Secret when prompted. There's no need to register separately - Dyname allows you to use any available subdomain.

Step 1: Register a domain with a domain registrar

If you already have a domain, you can skip this step.

First you will need to get a domain. You can register one with your favourite registrar; I tend to use Amazon Registrar (accessible via Route53) or Joker.com.

You can expect to pay around 10 pounds/euros/dollars per year for the most common TLDs (.com, .net, .org), with prices varying wildly between more exotic TLDs.

Step 2: Set domain's nameservers to Dyname

When registering a domain, you usually need to specify the nameservers your domain uses. If you already have a domain, you can change the nameservers by logging into your domain registrar's control panel. See instructions for Amazon Registrar and Joker.com.

Change the nameservers to the following:

A.DYNAME.NET

B.DYNAME.NET

Step 3: Associate domain with Dyname

This is an easy one. Go to https://dyname.net, and click use your own domain right in the middle of the page.

You will see a window asking you for your domain name, and e-mail address. Fill in both and click Associate.

You will be presented with a Domain Secret. Important: Take note of the Domain Secret. This is the last time it will be visible, and you will need it to create hostnames under your domain!

Step 4: Run the Dyname setup on your instance(s)

Log into your instances via SSH, and run Dyname setup with the following command:

bash <(wget -qO- https://get.dyname.net)

The Dyname setup will guide you through setting up a hostname for this instance.

For Hostname, enter the hostname you want this instance to have, for example www.mydomain.com or server1.mydomain.com.

For E-mail address, enter your e-mail address.

For Domain Secret, enter the Domain Secret you got on step 3 of this guide.

For Operating mode, select 1) Traditional Dynamic DNS.

For the last question about setting up a crontab entry, select y to make sure the hostname is updated whenever your instance changes IP addresses (ie. when you restart it).

You're all set! Repeat step 4 for all instances you want to have a hostname for, and remember to open up the instance's Security Group for the services you want to expose publicly.

Pro tip: Setup hostnames unattended when your instance boots

Dyname's AWS support allows you to setup hostnames automatically when your instances are provisioned. Assuming you've completed steps 1-3 above, all that's left for automatic hostnames is setting up an Instance Role and defining User Data and tag for your instances.

Dyname's AWS support is currently only compatible with instances running Ubuntu Linux.

Setting up an IAM Role

Dyname's AWS support works by running a script when your instance starts, looking at the tags of your instance, and registering a hostname accordingly. For Dyname to work with your AWS instances, it needs read access to EC2 tags. The easiest way to do this is with an IAM Role.

Create an IAM role using the IAM console

  1. Sign in to the Identity and Access Management (IAM) console at https://console.aws.amazon.com/iam/.
  2. In the navigation pane, choose Roles, Create New Role.
  3. On the Set Role Name page, enter a name for the role, for example AutomaticHostname and choose Next Step.
  4. On the Select Role Type page, choose Select next to Amazon EC2.
  5. On the Attach Policy page, select the AWS managed policy AmazonEC2ReadOnlyAccess.
  6. Review the role information, and then choose Create Role.

You will need to select this role when starting your EC2 instances.

Defining User Data and tag for your instances

To run the Dyname script automatically on boot, we'll enter it in the User Data field of the instances.

Launch your instance with the correct settings

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.
  2. On the dashboard, choose Launch Instance.
  3. Select an Ubuntu Linux AMI and instance type and then choose Next: Configure Instance Details.
  4. On the Configure Instance Details page, for IAM role, select the IAM role that you created.
  5. Still on the Configure Instance Details page, scroll to the bottom of the page and select Advanced Details.
  6. For User Data, select As text and paste the following:
#!/bin/bash

DOMAINSECRET="YOUR-DOMAINSECRET-HERE" EMAIL="YOUR-EMAIL-HERE" bash <(wget -qO- https://aws.dyname.net)

Click Next: Add Storage and configure any volumes to your instance. Afterwards, click Next: Add Tags. As the Name tag, configure the hostname you want to use for the instance, for example server1.mydomain.com.

Follow through the rest of the wizard. Review your settings, then choose Launch to choose a key pair and launch your instance.

When your instance launches, it will automatically register the hostname you specified in the Name tag, and point it to the public IP address of your instance.

Hope this is useful for you. Leave a comment below this post with any feedback or additional tips you may have.

How to expose your local Node/Django/Rails/Flask development server on the Internet

How to expose your local Node/Django/Rails/Flask development server on the Internet

Announcing: Dyname.net - The free dynamic DNS service for pros