in Coding, DevOps

Create Route 53 Alias record Using Boto3

Simple boto3 helper function to create or update an alias for elb endpoint in Amazon route53.

Note: As per Amazon Route 53 API Reference, an AliasTarget record should not have a TTL entry.

import boto3

client = boto3.client(
            'route53',
            aws_access_key_id='XXXXXXXXXXXXXXXXXXXXX',
            aws_secret_access_key='YYYYYYYYYYYYYYYYY'
 )

response = client.change_resource_record_sets(
    HostedZoneId='Z2SXXXXXXXXXXX',
    ChangeBatch={
        'Comment': 'Create/Update ELB dns entry',
        'Changes': [
            {
                'Action': 'UPSERT',
                'ResourceRecordSet': {
                    'Name': 'test.internal.example.com.',
                    'Type': 'A',
                    'AliasTarget': {
                        'HostedZoneId': 'ZXXXXXXXXXXXX',
                        'DNSName': 'internal-a8xxxxxxxxxxxxxxxxxxxxxxxxxxxxx-777777777.us-west-1.elb.amazonaws.com',
                        'EvaluateTargetHealth': False
                    }
                }
            },
        ]
    }
)

print(response)

 

Reference:-

http://boto3.readthedocs.io/en/latest/reference/services/route53.html