Can I add a subdomain to Route53 using the AWS PHP SDK?

I am working on a project in which we will create both subdomains and domains in Route53. We hope there is a way to do this programmatically. The PHP SDK documentation seems a bit light, but it looks like createHostedZone can be used to create a domain or subdomain record, and changeResourceRecordSets can be used to create the required DNS records. Does anyone have examples on how to do this?

+4


source to share


2 answers


Yes, it is possible with a changeResourceRecordSets

challenge changeResourceRecordSets

, as you have already indicated. But this is a bit clunky as you have to structure it as a package even if you only modify / create one entry and even creations are changes. Here's a complete example, without the credential 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 for this method can be found here . You will need to very carefully mark the required fields as well as possible values ​​for others. For example, the field name

must be a fully qualified domain name

ending dot (.).



Also worth noting: you will not receive a response from the API after this call by default, i.e. no confirmation or transaction ID. (Although this will definitely return errors if something is wrong.) This means that if you want your code to be bulletproof, you have to write a Guzzle response handler and you can wait a few seconds and then run a check that the new / the modified entry does exist.

Hope this helps!

+10


source


Yes, I did with the changeResourceRecordSets

method changeResourceRecordSets

.

<?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 receive any error, please check the error.log file on the server. If you get an error from the SDK library, the PHP version is probably not supported. if you run this code from your local machine you may get a " SignatureDoesNotMatch " error , then make sure this code is running in the same (AWS) server environment.

0


source







All Articles