是否可以使用 AWS PHP SDK 向 Route53 添加子域? [英] Is it possible to add a subdomain to Route53 using the AWS PHP SDK?

查看:31
本文介绍了是否可以使用 AWS PHP SDK 向 Route53 添加子域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开展一个项目,我们将在 Route53 中创建子域和域.我们希望有一种方法可以以编程方式执行此操作.用于 PHP 的 SDK 文档似乎有点简单,但似乎 createHostedZone 可用于创建域或子域记录,并且 changeResourceRecordSets 可用于创建必要的 DNS 记录.有没有人有如何实际完成此操作的示例?

I am working on a project where we will be creating both subdomains as well as domains in Route53. We are hoping that there is a way to do this programmatically. The SDK for PHP documentation seems a little light, but it appears that createHostedZone can be used to create a domain or subdomain record and that changeResourceRecordSets can be used to create the DNS records necessary. Does anyone have examples of how to actually accomplish this?

推荐答案

是的,正如您已经指出的那样,使用 changeResourceRecordSets 调用可以做到这一点.但这有点笨拙,因为即使您只更改/创建一个记录,您也必须像批处理一样构建它,甚至创建也是更改.这是一个完整的示例,没有凭据方法:

Yes, this is possible using the changeResourceRecordSets call, as you already indicated. But it is a bit clumsy since you have to structure it like a batch even if you're changing/creating only one record, and even creations are changes. Here is a full example, without a credentials method:

<?php

// Include the SDK using the Composer autoloader
require 'vendor/autoload.php';

use AwsRoute53Route53Client;
use AwsCommonCredentialsCredentials;

$client = Route53Client::factory(array(
    'credentials' => $credentials
));

$result = $client->changeResourceRecordSets(array(
    // HostedZoneId is required
    'HostedZoneId' => 'Z2ABCD1234EFGH',
    // ChangeBatch is required
    'ChangeBatch' => array(
        'Comment' => 'string',
        // Changes is required
        'Changes' => array(
            array(
                // Action is required
                'Action' => 'CREATE',
                // ResourceRecordSet is required
                'ResourceRecordSet' => array(
                    // Name is required
                    'Name' => 'myserver.mydomain.com.',
                    // Type is required
                    'Type' => 'A',
                    'TTL' => 600,
                    'ResourceRecords' => array(
                        array(
                            // Value is required
                            'Value' => '12.34.56.78',
                        ),
                    ),
                ),
            ),
        ),
    ),
));

该方法的文档可以在此处.您需要非常仔细地记录必填字段以及其他字段的可能值.例如,name 字段必须是以点 (.) 结尾的 FQDN.

The documentation of this method can be found here. You'll want to take very careful note of the required fields as well as the possible values for others. For instance, the name field must be a FQDN ending with a dot (.).

另外值得注意的是:默认情况下,在此调用后,您不会从 API 得到任何响应,即没有确认或交易 ID.(虽然如果出现问题,它肯定会返回错误.)所以这意味着如果你希望你的代码是防弹的,你应该编写一个 Guzzle 响应处理程序,你可能需要等待几秒钟,然后运行检查新的/changed 记录确实存在.

Also worth noting: You get no response back from the API after this call by default, i.e. there is no confirmation or transaction id. (Though it definitely gives errors back if something is wrong.) So that means that if you want your code to be bulletproof, you should write a Guzzle response handler AND you may want to wait a few seconds and then run a check that the new/changed record indeed exists.

希望这有帮助!

这篇关于是否可以使用 AWS PHP SDK 向 Route53 添加子域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆