Garret Wilson
Garret Wilson

Reputation: 21386

How to set multiple MX records in AWS Route 53 using Java SDK 2

I'm using the AWS Route 53 Java SDK 2 software.amazon.awssdk:software.amazon.awssdk:route53:2.8.3 with OpenJDK 11 on Windows 10. I want to set two separate MX records with different priorities, e.g.

If I set those as separate resource records, the last one overrides the first one, so that I only wind up with 20 smpt2.example.com. I read on an AWS forum thread that I need to combine the values into one resource record, with each MX record on a separate line, so I tried setting combining the values using "\n" (e.g. joining them with a newline character):

10 smpt1.example.com.
20 smpt2.example.com.

However that produces an error:

… software.amazon.awssdk.services.route53.model.InvalidChangeBatchException: [Invalid Resource Record: FATAL problem: MXRRDATANotTwoFields (MX record doesn't have 2 fields) encountered with '10 smtp1.example.com.

20 smtp2.example.com.'] (Service: Route53, Status Code: 400, Request ID: …

(Note that the error output starts 20.smtp2.example.com on a new line, indicating that there is a newline character. The Stack Overflow formatter above was removing the newline, so I added an extra blank line to illustrate. The blank line does not occur in the actual error message.)

So I'm stuck. How do I set two MX domains with different priorities in Route 53 using the Java SDK 2?

Update: This seems to be a pretty big blocker issue. Surely it can't be that Route 53 doesn't handle multiple MX records using the Java SDK 2. Is it completely broken? I've opened Issue #1484 with no responses so far.

Upvotes: 5

Views: 6000

Answers (1)

Garret Wilson
Garret Wilson

Reputation: 21386

I had made a mistake in using the Route 53 API model, forgetting that Route 53 groups multiple values for a single name and type into a resource record set. The ResourceRecordSet class has multiple ResourceRecords, each of which contains only a single value. The "resource record set" has a name+type that functions as a key for the set. In other words, I have to group all my values for the same name and type into this ResourceRecordSet, and set them together in a single API call.

My mistake earlier is that I wrote code to set a single value in a ResourceRecordSet, and then I was trying to call that code multiple times, which results in multiple "sets" for the same name+value key, each of which subsequently would replace the previous ones. Once I realized this, I had to refactor the code to group the resource records by name+value and put all the values into a ResourceRecordSet and use UPSERT to set them all at the same time.

Upvotes: 5

Related Questions