Search code examples
amazon-web-servicesamazon-ec2hostnamedomain-name

AWS ec2 access local app via different hostname/domain name without Route 53


I have a web app running locally on my ec2. I can access it publicly via ec2 Public DNS (IPv4) - ec2-x-xxx-xxx-xxx.compute-1.amazonaws.com:8000

But this app has the functionality to trigger two different web apps based on the domain url.

localhost:8000 -> app 1

myapp.localhost:8000 -> app 2

I cannot add the string myapp to my ec2 Public DNS address and make the app to load due to the the domain name not resolving.

I don't want to go the Route 53 route.

Can this be done any other way ? Maybe by adding hostnames to the ec2 hosts config file ?

If so please point me to the right direction. Thanks.


Solution

  • From the description, I can see you have an app that checks the DNS name sent in the HTTP request to distinguish 2 different business logic in one application. Technically, there is only one application since you only have one process running which listens to port 8000. The app 1 and app 2 are just 2 different execution paths internally inside the application.

    On your local machine (which is an EC2 instance in this case), by changing the /etc/hosts file, you can have both localhost and myapp.localhost pointing to 127.0.0.1, which is the IP on you default loopback network interface. With this, you can access the app locally by using these 2 locally resolved domain names. They will be resolved to the same IP and target the same running process on your machine.

    As you can see how it works locally on your machine, in order to achieve your goal, you will need 2 DNS records pointing to the same IP. Since you cannot use Route 53 or other similar services to create more public DNS records to map to your EC2 instance's public IP, there is no way for you to have the second DNS name for your machine.

    It's actually a bit weird to have your app's business logic relying on the DNS names which you cannot actually control. I would suggest you use the HTTP headers or query parameters to solve your problem.

    One solution by using custom HTTP header can be done like:

    (For App1)
    HTTP GET ec2-x-xxx-xxx-xxx.compute-1.amazonaws.com:8000
    Header: X-APP-NAM=app1
    
    (For App2)
    HTTP GET ec2-x-xxx-xxx-xxx.compute-1.amazonaws.com:8000
    Header: X-APP-NAME=app2
    

    In your app, at the place where you check the DNS name, you can change the logic to check X-APP-NAME HTTP header value instead, then proceed with different execution path (different apps as you said). Thus, you only need one DNS name to support 2 apps.

    Hopefully, this helps you.