简体   繁体   English

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

[英]Is it possible using the Java AWS API to update a Route53 record?

I have a hosted domain on AWS Route53.我在 AWS Route53 上有一个托管域。 Under that domain I have an 'A' record for a subdomain.在该域下,我有一个子域的“A”记录。

I would like to be able to update the IP address of the 'A' record using the Java API.我希望能够使用 Java API 更新“A”记录的 IP 地址。 However, when looking at the setAction method of the com.amazonaws.services.route53.model.Change class, it only accepts the CREATE or DELETE values.但是,在查看com.amazonaws.services.route53.model.Change类的setAction方法时,它只接受CREATEDELETE值。 This seems to match the allowed values in the XML message that the Java API sends behind the scenes.这似乎与 Java API 在后台发送的 XML 消息中的允许值相匹配。

Is there any way to just update the IP address, or do I have to delete the original record and then create it again?有没有办法只更新IP地址,还是必须删除原始记录然后重新创建?

Thanks谢谢

It worked for me using this piece of code: 它使用这段代码对我有用:

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);

Just replace the variables I used with proper data. 只需将我使用的变量替换为适当的数据即可。 (subdomain, loadBalancer and hostedZoneId). (subdomain,loadBalancer和hostedZoneId)。 The method getRoute53Client() returns an instance of the AmazonRoute53Client class from the AWS API. getRoute53Client()方法从AWS API返回AmazonRoute53Client类的实例。

The only way is to use DELETE / CREATE sequence as mentioned here . 唯一的方法是使用此处提到的DELETE / CREATE序列。

Creating a Change Batch Request 创建更改批处理请求

To create a change batch request, use the ChangeResourceRecordSets action ChangeBatch element. 要创建更改批处理请求,请使用ChangeResourceRecordSets操作ChangeBatch元素。 You use CREATE and DELETE actions within the ChangeBatch element for each record that you want to update. 您可以在ChangeBatch元素中为要更新的每个记录使用CREATE和DELETE操作。 If you are only creating records, then you will only use CREATE actions. 如果您只是创建记录,那么您将只使用CREATE操作。

        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());

hostedzoneId = " hosted zone id from the route53 " aliasTargetHostedzoneId = " zone id of the connected resource such as loadbalancer, cloudfront etc.. " hostedzoneId = “来自 route53 的托管区域 ID ” aliasTargetHostedzoneId = “连接资源的区域 ID,例如负载均衡器、云端等。

Route53Client route53Client = Route53Client.builder()
            .credentialsProvider(StaticCredentialsProvider.create(awsCreds))
            .region(Region.AWS_GLOBAL)
            .build();


    AliasTarget aliasTarget = AliasTarget.builder()
            .dnsName(loadBalancerDomain)
            .evaluateTargetHealth(false)
            .hostedZoneId(aliasTargetHostedzoneId)
            .build();

    // Create a ResourceRecordSet
    ResourceRecordSet resourceRecordSet = ResourceRecordSet.builder()
            .name(fullDomainName+".")
            .type(RRType.A)
            .aliasTarget(aliasTarget)
            .build();


    // Create a change
    Change change = Change.builder()
            .action(ChangeAction.CREATE)
            .resourceRecordSet(resourceRecordSet)
            .build();

    List<Change> changesList = new ArrayList<Change>();
    changesList.add(change);

    // Create a change batch
    ChangeBatch changeBatch = ChangeBatch.builder()
            .changes(changesList)
            .build();

    // Create ChangeResourceRecordSetRequest.
    ChangeResourceRecordSetsRequest request = ChangeResourceRecordSetsRequest.builder()
            .hostedZoneId(hostedzoneId)
            .changeBatch(changeBatch)
            .build();

    // Send the request and get the response.
    ChangeResourceRecordSetsResponse result = route53Client.changeResourceRecordSets(request);
    

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AWS APIGateway 仅来自 route53 - AWS APIGateway From route53 only 指向经典负载均衡器的 AWS Route53 A 记录(由 Kube.netes 创建)适用于 http 但不适用于 https - AWS Route53 A-record pointing to classic load balancer (created by Kubernetes) works with http but not https http2 请求在 aws 上由 route53 连接的云端不工作 - http2 request not working in cloudfront connected by route53 on aws 使用 AWS CloudFormation json 创建堆栈时 AWS::Route53::RecordSet 中出现无效请求错误 - Invalid request error in AWS::Route53::RecordSet when creating stack with AWS CloudFormation json 如何将 AWS Amplify 应用程序添加为 AWS Route53 中托管域的子域? - How to add an AWS Amplify application as subdomain for domain hosted in AWS Route53? 如何使用 Route53 DNS 将 AWS Fargate ECS 容器公开到 inte.net? - How to expose AWS Fargate ECS containers to internet with Route53 DNS? Amazon aws route53,将子域重定向到在特定端口下运行的 ec2 应用程序 - Amazon aws route53, redirect subdomain to ec2 app running under specific port cdk api 网关 route53 lambda 自定义域名不工作 - cdk api gateway route53 lambda custom domain name not working 我可以将 cloudfront 与 route53 流量策略一起使用吗? - Can I use cloudfront with a route53 traffic policy? 如何在重定向到第 3 方云页面的 AWS Route 53 中添加记录? - How to add record in AWS Route 53 that redirects to 3rd party cloud page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM