是否可以使用Java AWS API更新Route53记录?

时间:2012-04-16 06:36:56

标签: java amazon-web-services amazon-route53

我在AWS Route53上有一个托管域。在该域名下,我有一个子域名的“A”记录。

我希望能够使用Java API更新“A”记录的IP地址。但是,在查看setAction类的com.amazonaws.services.route53.model.Change方法时,它只接受CREATEDELETE值。这似乎与Java API在幕后发送的XML消息中的允许值相匹配。

有没有办法只更新IP地址,还是我必须删除原始记录然后再创建它?

由于

3 个答案:

答案 0 :(得分:4)

使用这段代码对我有用:

ResourceRecord record = new ResourceRecord(loadBalancer);
List<ResourceRecord> records = new ArrayList<ResourceRecord>();
records.add(record);
ResourceRecordSet recordsSet = new ResourceRecordSet();
recordsSet.setResourceRecords(records);
recordsSet.setType(RRType.CNAME);
recordsSet.setTTL(900L);
recordsSet.setName(subdomain + ".");
Change change = new Change(ChangeAction.CREATE, recordsSet);
List<Change> changes = new ArrayList<Change>();
changes.add(change);
ChangeBatch batch = new ChangeBatch(changes);
ChangeResourceRecordSetsRequest request = new ChangeResourceRecordSetsRequest();
request.setChangeBatch(batch);
request.setHostedZoneId(hostedZoneId);
ChangeResourceRecordSetsResult result = getRoute53Client().changeResourceRecordSets(request);
System.out.println(result);

只需将我使用的变量替换为适当的数据即可。 (subdomain,loadBalancer和hostedZoneId)。 getRoute53Client()方法从AWS API返回AmazonRoute53Client类的实例。

答案 1 :(得分:2)

唯一的方法是使用提到here的DELETE / CREATE序列。

  

创建更改批处理请求

     

要创建更改批处理请求,请使用ChangeResourceRecordSets   动作ChangeBatch元素。您在其中使用CREATE和DELETE操作   要更新的每个记录的ChangeBatch元素。如果   您只创建记录,然后您将只使用CREATE操作。

答案 2 :(得分:2)

        ResourceRecord rr = new ResourceRecord(IPAdress); // IPAddress will be String variable that has IP value
        List<ResourceRecord> rrList = new ArrayList<ResourceRecord>();
        rrList.add(rr);

        // Create a ResourceRecordSet
        ResourceRecordSet resourceRecordSet = new ResourceRecordSet();
        resourceRecordSet.setName(domainName); //domainName is String value of your domain
        resourceRecordSet.setType(RRType.A); //type of ResourceRecordSet
        resourceRecordSet.setTTL(new Long(300));
        resourceRecordSet.setWeight(new Long(0));
        resourceRecordSet.setResourceRecords(rrList);

        // Create a change
        Change change = new Change(ChangeAction.CREATE, resourceRecordSet);
        List<Change> changesList = new ArrayList<Change>();
        changesList.add(change);

        // Create a change batch
        ChangeBatch changeBatch = new ChangeBatch(changesList);

        // Create ChangeResourceRecordSetRequest.
        ChangeResourceRecordSetsRequest request = new ChangeResourceRecordSetsRequest(hostedZoneID, changeBatch); //hostedZoneId is variable that is the id of HostedZone

        // Send the request and get the response.
        ChangeResourceRecordSetsResult result = amazonRoute53Client.changeResourceRecordSets(request);

        // Print the result
        System.out.println(result.getChangeInfo());