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
@thakral729
Copy link

I want to add Different targets with different weights, could you please explain how to do that ?

@yogita-clecotech
Copy link

I want to add multiple targets values for CNAME , Could you explain me please how can we do that ?

@BMonsalvatge
Copy link

Figure this is a bit old, but incase anyone is Googling it and this comes up, here is the example shown in the AWS docs:

response = client.change_resource_record_sets(
    ChangeBatch={
        'Changes': [
            {
                'Action': 'CREATE',
                'ResourceRecordSet': {
                    'HealthCheckId': 'abcdef11-2222-3333-4444-555555fedcba',
                    'Name': 'example.com',
                    'ResourceRecords': [
                        {
                            'Value': '192.0.2.44',
                        },
                    ],
                    'SetIdentifier': 'Seattle data center',
                    'TTL': 60,
                    'Type': 'A',
                    'Weight': 100,
                },
            },
            {
                'Action': 'CREATE',
                'ResourceRecordSet': {
                    'HealthCheckId': 'abcdef66-7777-8888-9999-000000fedcba',
                    'Name': 'example.com',
                    'ResourceRecords': [
                        {
                            'Value': '192.0.2.45',
                        },
                    ],
                    'SetIdentifier': 'Portland data center',
                    'TTL': 60,
                    'Type': 'A',
                    'Weight': 200,
                },
            },
        ],
        'Comment': 'Web servers for example.com',
    },
    HostedZoneId='Z3M3LMPEXAMPLE',
)

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/route53.html#Route53.Client.change_resource_record_sets

I imagine you'd just change it from A records to CNAME, but should be the same idea.

@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