以编程方式分配Route53 DNS

问题描述 投票:0回答:2

我有以下代码可以正常工作,前提是我有一个serverBase.mydomain.net的A记录,它指向IP 10.20.30.40但是如果我想创建一个指向IP的记录serverProxy(在注释掉的行中交换),但是它给出了错误:

“试图创建一个目标为10.20.30.40。的别名,在区域*********中键入A,但别名目标名称不在目标区域内”

我可以手动执行此操作,因此代码中的某些内容不正确?

    String subDomainName = "serverProxy.mydomain.net";
    String zoneId = "**********";
    //String target = "serverBase.mydomain.net";
    String target = "10.20.30.40";

    AmazonRoute53Client client = getAmazonRoute53Client();
    List<Change> changes = new ArrayList<Change>();
    List<ResourceRecord> recourceRecords = new ArrayList<>();
    ResourceRecordSet resourceRecordSet = new ResourceRecordSet(subDomainName+".",RRType.A);

    //AliasTarget alias = new AliasTarget(zoneId, target);
    //alias.setEvaluateTargetHealth(false);
    //resourceRecordSet.setAliasTarget(alias);

    changes.add(new Change( ChangeAction.CREATE, resourceRecordSet));
    ChangeBatch changeBatch = new ChangeBatch(changes);

    try {
        ChangeResourceRecordSetsRequest changeResourceRecordSetsRequest = new ChangeResourceRecordSetsRequest(zoneId, changeBatch);
        client.changeResourceRecordSets(changeResourceRecordSetsRequest);
    }
    catch( Exception e)
    {
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
amazon-web-services amazon-route53
2个回答
1
投票

ALIAS记录集只能指向DNS名称。它不能直接指向IP地址。

如果要创建指向IP地址的记录,则需要创建A记录。


0
投票
    String zoneId = "myzoneID";
    String cname = "myserver.mydomain.net";

    AmazonRoute53Client client = getAmazonRoute53Client();
    List<Change> changes = new ArrayList<>();

    Change change = new Change()
            .withAction(ChangeAction.CREATE)
            .withResourceRecordSet(new ResourceRecordSet()
                    .withType(RRType.CNAME)
                    .withName( cname )
                    .withTTL(300L)
                    .withResourceRecords(new ResourceRecord( "10.20.30.40" )));

    changes.add( change );
    ChangeBatch changeBatch = new ChangeBatch(changes);
    try {
        ChangeResourceRecordSetsRequest changeResourceRecordSetsRequest = new ChangeResourceRecordSetsRequest(zoneId, changeBatch);
        client.changeResourceRecordSets(changeResourceRecordSetsRequest);
        logback.info( "DNS Record " + cname +  " created");
    }
    catch( InvalidChangeBatchException icbe )
    {
        String error = icbe.getMessage();
        if( error.contains("already exists" ))
        {
            logback.info( "DNS Record already exists");
        }
        else
        {
            throw icbe;
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.