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?
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' => '22.214.171.124', ), ), ), ), ), ), ));
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!