In my previous post, I created a new AWS ECS Cluster for my simple “Hello World” Docker image. The “cluster” only has a single container instance, whose details are shows in the AWS Console screenshot below.
AWS assigns a public-facing IP address and DNS entry for the server that hosts the container. The DNS entry for the hostname is pretty ugly. I’d like to assign a nice, user-friendly hostname to be used in a web browser.
Creating an AWS “Elastic IP”
Before I can assign a nice hostname to my container server, I’ll need that server to have an IP address that doesn’t change. AWS provides a service they call “Elastic IP”. This can be found in the EC2 section of the AWS Services.
I have already created an Elastic IP, named it, and associated it with my running Docker container, as seen below:
When I first clicked the “Allocate new address” button, my Elastic IP (188.8.131.52) was not associated with anything. As soon as you create an Elastic IP, it belongs to your AWS account exclusively, until you release the Elastic IP. You can release one (or more) IPs by selecting it, and then choosing the “Release addresses” options under “Actions”.
Amazon will charge you a small fee each hour for any Elastic IPs that you have allocated but have NOT associated with anything. I assume they do this to prevent users from hoarding Elastic IPs and not using them.
In my screenshot above, the “Release addresses” option is disabled because the selected Elastic IP is currently associated with a server instance (my ECS Docker container host). I would have to disassociate the Elastic IP before I’m allowed to release it from my account.
If I click on the “Associate address” option, I’m presented with this:
I only have one choice of “Instance” resource type. It is the EC2 instance that is currently hosting my Docker container that is associated with my ECS cluster.
I’m only given one choice of private IP.
I’ll click “Cancel” now, because I don’t want to mess with my already associated Elastic IP.
Let’s switch to the ECS section in the AWS Console and see where the Elastic IP is being used.
On the details page for my ECS cluster, I selected the “ECS Instances” tab. I can see the EC2 instance id that I associated with my Elastic IP address. I’ll click on the EC2 instance and take a look at its details.
I can see that the IPv4 Public IP is my Elastic IP, as expected. I should be able to open a browser and use the Elastic IP to reach my Docker app on port 80.
That works, but all I’ve done is replace a randomly assigned Public IP address with a static Elastic IP address.
Associating a Domain with the Elastic IP
This next part requires an internet domain name, and administrator access to it. The AWS Elastic IP provides me with a static IP address, but now I need to associate a domain name with that IP.
I own the “jimtough.com” domain. I bought it back in 2000, when a very popular company named Yahoo! was brokering .com domain names. I’m embarrassed to say that I never bothered to move my domain to a modern company. The following screenshots will show the administration page for my domain.
Every domain registrar will have their own interface for managing your domain settings. This just happens to be mine. I’ve added an “A Record” that associates the prefix “aws-hello-world” to my AWS Elastic IP address.
If I use the address “aws-hello-world.jimtough.com” in a browser, then my web request should be directed to IP address 184.108.40.206.
Note that it may take some time for the domain settings change to propagate from your registrar’s DNS servers. After I added this A Record, it took about 15 minutes before I could reach the URL http://aws-hello-world.jimtough.com/.
After allowing some time for the new DNS name to propagate, I try the new name in a browser.
That’s it. As long as I continue to use the same reserved AWS Elastic IP address, this hostname will work. If I release the Elastic IP and change to a new one, then I’ll need to go back into my domain administrator page and update the IP.
You may have noticed that I named my Elastic IP “aws-hello-world” in the AWS Console. That name is just a label, and has no bearing on the association between my “jimtough.com” domain and the IP address.
What about an app with multiple instances running?
For my app, there is only one instance running. I was able to associate my Elastic IP with the single EC2 instance that was hosting my Docker container. If I had set up my app to have multiple instances behind a load balancer, then my approach wouldn’t work.
I’m sure AWS has an easy solution for this scenario, but I haven’t looked into it yet. I want to have an app that is able to interact with AWS and make queries such as “what availability zone is hosting me” before I experiment with load balancing. Once I get there, I’ll revisit Elastic IPs.