Is it possible to add a subdomain to Route53 using the AWS PHP SDK?

邮差的信 提交于 2019-12-04 08:33:59

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 Aws\Route53\Route53Client;
use Aws\Common\Credentials\Credentials;

$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',
                        ),
                    ),
                ),
            ),
        ),
    ),
));

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

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.

Hope this helps!

Yes, I done using changeResourceRecordSets method.

<?php
require 'vendor/autoload.php';

use Aws\Route53\Route53Client;
use Aws\Exception\CredentialsException;
use Aws\Route53\Exception\Route53Exception;

//To build connection
try {
    $client = Route53Client::factory(array(
        'region' => 'string', //eg . us-east-1
        'version' => 'date', // eg. latest or 2013-04-01
        'credentials' => [
                    'key' => 'XXXXXXXXXXXXXXXXXXX', // eg. VSDFAJH6KXE7TXXXXXXXXXX
                    'secret' => 'XXXXXXXXXXXXXXXXXXXXXXX', //eg. XYZrnl/ejPEKyiME4dff45Pds54dfgr5XXXXXX
              ]
    ));
} catch (Exception $e) {
        echo $e->getMessage();
}

/* Create sub domain */

try {

    $dns = 'yourdomainname.com';
    $HostedZoneId = 'XXXXXXXXXXXX'; // eg. A4Z9SD7DRE84I ( like 13 digit )
    $name = 'test.yourdomainname.com.'; //eg. subdomain name you want to create 
    $ip = 'XX.XXXX.XX.XXX'; // aws domain Server ip address
    $ttl = 300;
    $recordType = 'CNAME';
    $ResourceRecordsValue = array('Value' => $ip);

    $client->changeResourceRecordSets([
        'ChangeBatch'  => [
            'Changes' => [
                [
                    'Action'            => 'CREATE',
                    "ResourceRecordSet" => [
                        'Name'            => $name,
                        'Type'            => $recordType,
                        'TTL'             => $ttl,
                        'ResourceRecords' => [
                            $ResourceRecordsValue
                        ]
                    ]
                ]
            ]
        ],
        'HostedZoneId' => $HostedZoneId
    ]);
}

If you get any error please check into server error.log file. If you get error from SDK library then there is might PHP version not supported. if you run this code from your local machine then you might get "SignatureDoesNotMatch" error then Make sure run this code into same (AWS)server environment.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!