AWS

How to Build and Deploy a Java Application on AWS Using ECS and Fargate – Part 3

In Part 2, we deployed ourJava Books API on ECS Fargate behind an Application Load Balancer (ALB). Our app was working, but the ALB DNS was long and unfriendly, like:

<http://BookCd-Books-XXXXXXX-XXXXXXX.us-west-2.elb.amazonaws.com/books>

In Part 3, we’ll give our API a more readable name with:

  • Route 53
  • custom domain / subdomain

Why Use Route 53 for Your Domain?

Using Route 53 gives us:

  • Friendly DNS names like api.books.com.
  • Easy mapping to AWS resources (like ALB).
  • Integration with ACM for SSL/TLS certificates.

Even if you purchased your domain elsewhere (GoDaddy, Hostinger, Namecheap), Route 53 can manage DNS records once you delegate the domain.

Step 1: Delegate Your Domain to Route 53

I bought aparnavikraman.dev via GoDaddy. To use Route 53:

  1. Go to AWS Console → Route 53 → Hosted Zones → Create Hosted Zone.
    • Domain name: aparnavikraman.dev
    • Type: Public hosted zone
  2. Copy the NS (nameserver) records from Route 53.
  1. Log in to GoDaddy → Manage DNS → Nameservers → Enter custom nameservers
    • Paste the 4 NS records from Route 53
  2. Wait for DNS propagation (30 min – 2 hrs usually).

You can verify the delegation by

dig NS aparnavikraman.dev +short

You should see the Route 53 NS servers.

Step 2: Create a Subdomain for the API

Instead of pointing the root domain to AWS, we’ll use a subdomain like api.aparnavikraman.dev.

Using CDK

We’ll create a separate DNS stack for modularity:

import * as cdk from "aws-cdk-lib";
import * as route53 from "aws-cdk-lib/aws-route53";
import * as targets from "aws-cdk-lib/aws-route53-targets";
import * as ecs_patterns from "aws-cdk-lib/aws-ecs-patterns";

interface DnsStackProps extends cdk.StackProps {
  domainName: string;
  fargateService: ecs_patterns.ApplicationLoadBalancedFargateService;
}

export class DnsStack extends cdk.Stack {
  constructor(scope: cdk.App, id: string, props: DnsStackProps) {
    super(scope, id, props);

    // Lookup existing hosted zone
    const hostedZone = route53.HostedZone.fromLookup(this, "HostedZone", {
      domainName: props.domainName
    });

    // Create a subdomain record pointing to ALB
    new route53.ARecord(this, "ApiSubdomain", {
      zone: hostedZone,
      recordName: "api", // api.yourdomain.com
      target: route53.RecordTarget.fromAlias(new targets.LoadBalancerTarget(props.fargateService.loadBalancer))
    });
  }
}

Update the main application stack to include DNS stack

#!/usr/bin/env node
import * as cdk from 'aws-cdk-lib';
import { ApplicationStack } from '../lib/application-stack';
import { DnsStack } from '../lib/dns-stack';

// Refer <https://docs.aws.amazon.com/cdk/v2/guide/configure-env.html>
const envVal  = { account: 'XXXXXX', region: 'us-west-2' };

const app = new cdk.App();
const bookStack = new ApplicationStack(app, 'BookCdkStack', { env: envVal });

// DNS stack
const dnsStack = new DnsStack(app, "DnsStack", {
    domainName: "aparnavikraman.dev",
    fargateService: bookStack.service,
    env: envVal
  });

Step 3: Deploy the DNS Stack

cdk deploy --all
  • CDK will deploy both ECS stack and DNS stack in the correct order if you added:
dnsStack.addDependency(bookStack);
  • If your ECS service is running and GoDaddy delegation has propagated, your API will now be accessible at:
<http://api.aparnavikraman.dev/books>

Step 4: Verify DNS & API

curl <http://api.aparnavikraman.dev/books>

Expected output:

["Harry Potter", "The Fourth Wing", "The Midnight Library", "The Way of Kings"]

Troubleshooting & Gotchas

While deploying multiple stacks and setting up a custom domain, I ran into a few common issues:


Issue 1: Hosted Zone Context Error

cdk deploy

Error:

ValidationError: Cannot retrieve value from context provider hosted-zone since account/region are not specified at the stack level. Configure "env" with an account and region when you define your stack.

Fix: Add env to the stack:

const dnsStack = new DnsStack(app, "DnsStack", {
  domainName: "aparnavikraman.dev",
  fargateService: bookStack.fargateService,
  env: envVal // which includesaccount ID and region
});

Issue 2: Multiple Stacks Deployment Warning

Since this app includes more than a single stack, specify which stacks to use (wildcards are supported) or specify `--all`
Stacks: BookCdkStack · DnsStack

Fix 1: Deploy all stacks at once:

cdk deploy --all

Fix 2: Ensure proper deployment order if one stack depends on another:

dnsStack.addDependency(bookStack);

This ensures BookCdkStack deploys first, so DNS points to a valid ALB.


Step 5: Next Steps – HTTPS

Once the subdomain works:

  1. Request an ACM certificate for api.aparnavikraman.dev.
  2. Attach the certificate to the ALB listener.
  3. Your API will be secure via HTTPS.

We’ll cover this in Part 4, along with endpoint protection

Series Roadmap

  1. (Done) Build and containerize the Java app
  2. (Done) Deploy to ECS Fargate with ALB (via CDK)
  3. (This post)Add Route 53 and custom domain
  4. Secure with HTTPS (ACM) and protect endpoint
  5. Scaling, monitoring, and production-ready tweaks

One comment on “How to Build and Deploy a Java Application on AWS Using ECS and Fargate – Part 3

Leave a Reply

Your email address will not be published. Required fields are marked *