Created
August 3, 2016 16:36
-
-
Save rotemtam/b4474f732674a9fd5884719d8c5bdf3e to your computer and use it in GitHub Desktop.
Add a CNAME record in Route53 using python boto3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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',
)
I imagine you'd just change it from A
records to CNAME
, but should be the same idea.
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
I want to add multiple targets values for CNAME , Could you explain me please how can we do that ?