Created
April 10, 2023 03:01
-
-
Save micheleberardi/57e20ca9a84839a66478fe529cca8655 to your computer and use it in GitHub Desktop.
update_up_route53.py
This file contains 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 | |
def update_route53_record(hosted_zone_id, domain_name, action, ip_address, ttl=300): | |
client = boto3.client('route53') | |
# Check if the action is valid | |
if action not in ('UPSERT', 'DELETE'): | |
print("Invalid action. Use 'UPSERT' to add/update or 'DELETE' to remove.") | |
return | |
# Prepare the change record | |
change_record = { | |
'Action': action, | |
'ResourceRecordSet': { | |
'Name': domain_name, | |
'Type': 'A', | |
'TTL': ttl, | |
'ResourceRecords': [ | |
{ | |
'Value': ip_address | |
}, | |
], | |
} | |
} | |
try: | |
response = client.change_resource_record_sets( | |
HostedZoneId=hosted_zone_id, | |
ChangeBatch={ | |
'Changes': [change_record] | |
} | |
) | |
print(f"Change submitted: {response['ChangeInfo']['Id']}") | |
except Exception as e: | |
print(f"Error updating the record: {e}") | |
# Usage example | |
hosted_zone_id = 'YOUR_HOSTED_ZONE_ID' | |
domain_name = 'example.com.' | |
ip_address = '1.2.3.4' | |
# Add or update the IP address for the domain | |
update_route53_record(hosted_zone_id, domain_name, 'UPSERT', ip_address) | |
# Remove the IP address from the domain | |
# update_route53_record(hosted_zone_id, domain_name, 'DELETE', ip_address) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment