-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute53.py
More file actions
67 lines (61 loc) · 2.28 KB
/
Copy pathroute53.py
File metadata and controls
67 lines (61 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import boto3
import uuid
def create_tags(id):
route53 = boto3.client('route53')
route53.change_tags_for_resource(
ResourceType='hostedzone',
ResourceId=id,
AddTags=[
{
'Key': 'cli',
'Value': 'made'
},
]
)
def create_zone(name):
route53 = boto3.client('route53')
response = route53.create_hosted_zone(
Name = name ,
CallerReference=str(uuid.uuid4())
)
zone_id = response['HostedZone']['Id'].split('/')[-1]
create_tags(zone_id)
print('Zone created!')
def update_records(z_id, action, domain_name, record_type, record_value):
route53 = boto3.client('route53')
tags = route53.list_tags_for_resource(
ResourceType="hostedzone",
ResourceId=z_id
)["ResourceTagSet"].get("Tags", [])
if any(tag["Key"] == "cli" and tag["Value"] == "made" for tag in tags):
route53.change_resource_record_sets(
HostedZoneId=z_id,
ChangeBatch={
'Changes': [
{
'Action': action,
'ResourceRecordSet': {
'Name': domain_name,
'Type': record_type,
'TTL': 3600,
'ResourceRecords': [
{'Value': record_value}
]
}
}
]
}
)
else:
print(f" you dont have any zones.")
def list_hosted_zones_by_tag(tag_key='cli', tag_value='made'):
route53 = boto3.client('route53')
hosted_zones = route53.list_hosted_zones_by_name()['HostedZones']
for z in hosted_zones:
zid = z['Id'].split('/')[-1]
tags = {t['Key']: t['Value'] for t in route53.list_tags_for_resource(
ResourceType='hostedzone', ResourceId=zid)['ResourceTagSet']['Tags']}
if tags.get(tag_key) == tag_value:
print(f"Name: {z['Name']} | ID: {zid} | {'Private' if z['Config']['PrivateZone'] else 'Public'}")
for r in route53.list_resource_record_sets(HostedZoneId=zid)['ResourceRecordSets']:
print(f"Records: {r}")