Skip to content

Instantly share code, notes, and snippets.

@rotemtam
Created August 3, 2016 16:36
Show Gist options
  • Save rotemtam/b4474f732674a9fd5884719d8c5bdf3e to your computer and use it in GitHub Desktop.
Save rotemtam/b4474f732674a9fd5884719d8c5bdf3e to your computer and use it in GitHub Desktop.
Add a CNAME record in Route53 using python boto3
import boto3
client = boto3.client('route53')
def add_cname_record(source, target):
try:
response = client.change_resource_record_sets(
HostedZoneId='<hosted zone id>',
ChangeBatch= {
'Comment': 'add %s -> %s' % (source, target),
'Changes': [
{
'Action': 'UPSERT',
'ResourceRecordSet': {
'Name': source,
'Type': 'CNAME',
'TTL': 300,
'ResourceRecords': [{'Value': target}]
}
}]
})
except Exception as e:
print e
@Fedorov16
Copy link

Fedorov16 commented Aug 5, 2022

If you use Route53Client for PHP, copy this code and change variables

$zoneId = ZONE123ID45EXAMPLE;
$domainName = 'www.example.com';
$cnameTo = 'www.example.com.iam.cname';
$response = $this->client->changeResourceRecordSets([
            'HostedZoneId' => $zoneId, 
            'ChangeBatch' => [
                'Changes' => [
                    [
                        'Action' => ChangeAction::CREATE, //CREATE
                        "ResourceRecordSet" => [
                            'Name' => $domainName,
                            'Type' => RRType::CNAME,  //CNAME
                            'TTL' => 300,
                            'ResourceRecords' => [
                                [
                                    'Value' => $cnameTo
                                ],
                            ],
                        ],
                    ],
                ],
            ],
        ]);

You can also wait for the answer from route53

$this->client->resourceRecordSetsChanged(['Id' => $response->getChangeInfo()->getId()])->wait();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment