By using this site you agree to the use of cookies by Brugbart and our partners.

Learn more

Using the changeResourceRecordSets Route53Client Class Function

How to use the changeResourceRecordSets function from the aws sdk for PHP.

Edited: 2013-07-08 11:52

This tutorial is for the aws sdk for php Amazon Route 53 changeResourceRecordSets client class function. In this tutorial, it will be demonstrated how to correctly fill out the parameters for the function – note that the official documentation was plain wrong when we wrote this tutorial – and with no decent examples in the official documentation, it can be hard enough as it is.

Before you can use the Route53Client, you need to load it somehow, in the below example we use the composer autoloader.

  // Include AWS SDK by Composer Autoloader
  require $_SERVER["DOCUMENT_ROOT"] . '/lib/awssdk2/vendor/autoload.php';

  use Aws\Route53\Route53Client;

  $client = Route53Client::factory(array(
  'key'    => '',
  'secret' => '',
  ));

  $client->changeResourceRecordSets($args);

Required arguments of changeResourceRecordSets

Some arguments are required by the function, which exactly is still somewhat unclear, but you should be able to figure it out by bruteforcing the function. In such siturations it can be a really good idea to have errors turned on while testing.

Something that only adds to the confusion, is that some values need to be of type (object), specifically the HostedZoneId key and the ChangeBatch key. To turn an array into an object, we can do like below.

$object_example = (object) array('One' => '1', 'Two' => '2');

The documentation refers to certain keys as containing a complex type, which likely just means that you have to cast the associative array to an object.

Route53 changeResourceRecordSets Example

The below is an example of how to correctly form the arguments for the changeResourceRecordSets function,

  $args = array(
    'HostedZoneId' => $zone_id,
    'ChangeBatch' => (object) array(
      'Changes' => (object) array(array(
        'Action' => 'CREATE',
          'ResourceRecordSet' => array(
          'Name' => $name,
          'Type' => $type,
          'TTL' => 60*60*24,
          'ResourceRecords' => array(array(
            'Value' => $value
          ))
        )
      ))
    )
  );

To make it easier you can just make a custom function that will allow you to easily fill out the $zone_id $name, and $type variables that we used in the last example. I.e.

function create_record($zone_id, $name, $type, $value) {
  $args = array(
    'HostedZoneId' => $zone_id,
    'ChangeBatch' => (object) array(
      'Changes' => (object) array(array(
        'Action' => 'CREATE',
          'ResourceRecordSet' => array(
          'Name' => $name,
          'Type' => $type,
          'TTL' => 60*60*24,
          'ResourceRecords' => array(array(
            'Value' => $value
          ))
        )
      ))
    )
  );
  $result = $client->changeResourceRecordSets($args);
  print_r($result->toArray());
}