Search code examples
amazon-web-servicesamazon-rdsamazon-route53aws-sdk-java

AWS Java SDK: updating a Route53 zone record with new RDS instance info


Java 8 using the AWS Java SDK here. I have a private hosted zone (PHZ) of, say, myapp.example.com and I have an A record in that zone called db.myapp.example.com which points to an RDS instance.

I am now writing a Lambda (Java) that will create a new RDS instance, and update the db.myapp.example.com zone record to point to the new RDS instance.

So far the gist of my code looks like this:

CreateDBInstanceRequest createDbRequest = getSomehow();
DBInstance rdsInstance = amazonRDS.createDBInstance(createDbRequest);

ListHostedZonesResult hostedZonesResult = amazonRoute53.listHostedZones();
Optional<HostedZone> hostedZoneOpt = hostedZonesResult.getHostedZones().stream()
    .filter(zone -> "db.myapp.example.com".equals(zone.getName())).findFirst();
if (hostedZoneOpt.isPresent()) {

    // TODO: how to update the record so that it points to 'rdsInstance'?
    ResourceRecordSet alias = new ResourceRecordSet(aliasName, "A");
    
    Change updateAlias = new Change(ChangeAction.UPSERT, alias);
    List<Change> changes = Collections.singletonList(updateAlias);
    ChangeBatch changeBatch = new ChangeBatch(changes);

    ChangeResourceRecordSetsRequest changeRecordRequest =
        new ChangeResourceRecordSetsRequest(hostedZoneOpt.get().getId(), changeBatch);
    amazonRoute53.changeResourceRecordSets(changeRecordRequest);
    
} else {
  // handle
}

I think this is correct for the most part. However, after scouring the Route53 SDK API docs I cannot for the life of me figure out how/where I configure the alias : ResourceRecordSet instance with the new RDS (rdsInstance) info so that db.myapp.example.com now points to it.

Any ideas? Thanks in advance!

Update

I see there is the concept of TrafficPolicy in Route53 and apparently I can send a JSON document to AWS and configure a traffic policy for my A record, so maybe this is the correct way to go. But looking at the Traffic Policy document definition, I need to be able to specify an IP address in its Value field, and I don't believe the AWS SDK exposes IP addresses anywhere!


Solution

  • Per the AWS docs on alias routing, you can't create an alias DNS record pointing to an RDS instance. What you can do, however, is create a regular CNAME record in Route 53, pointing to the RDS instance's domain name. Your record would have type CNAME, Name db.myapp.example.com, and the Value would be the RDS instance's domain name, i.e. some-instance.cxu5ec943k5u.us-east-1.rds.amazonaws.com. You can get this from rdsInstance.getEndpoint().getAddress().