def create_hosted_zone(self, domain, vpcs): """ Creates a route53 hosted zone object either public (vpcs=None) or private (vpcs=[vpc1,...]) AWS: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-route53-hostedzone.html Troposphere: https://github.com/cloudtools/troposphere/blob/master/troposphere/route53.py :param domain: The domain you would like for your hosted zone. MUST be 'something.something' (eg 'example.com') :param vpcs: A list of VPCs to associate this hosted zone with (if none, a public hosted zone is created) """ hz_type = 'private' if vpcs else 'public' hz_config = route53.HostedZoneConfiguration( Comment=Join('', [ hz_type, ' hosted zone created by Amazonia for stack: ', Ref('AWS::StackName') ])) hz = self.template.add_resource( route53.HostedZone(hz_type + 'HostedZone', HostedZoneConfig=hz_config, Name=domain)) if vpcs: hz.VPCs = [] for vpc in vpcs: hz.VPCs.append( HostedZoneVPCs(VPCId=vpc, VPCRegion=Ref('AWS::Region'))) return hz
def create_template(self): variables = self.get_variables() hosted_zone_name = variables["HostedZoneName"] hosted_zone_id = variables["HostedZoneId"] hosted_zone_comment = variables["Comment"] if all([hosted_zone_comment, hosted_zone_id]): logger.warning( "The Comment variable works when HostedZoneName is passed." "When HostedZoneId in variables, Comment is ignored." ) if all([hosted_zone_name, hosted_zone_id]): raise ValueError("Cannot specify both 'HostedZoneName' and " "'HostedZoneId' variables.") if not any([hosted_zone_name, hosted_zone_id]): raise ValueError("Please specify either a 'HostedZoneName' or " "'HostedZoneId' variable.") if hosted_zone_id: self.hosted_zone_id = hosted_zone_id else: hosted_zone_config = route53.HostedZoneConfiguration( "HostedZoneConfiguration", Comment=hosted_zone_comment ) hosted_zone = route53.HostedZone( "HostedZone", Name=hosted_zone_name, HostedZoneConfig=hosted_zone_config ) if variables["VPC"]: vpc = route53.HostedZoneVPCs( VPCId=variables["VPC"], VPCRegion=Region ) hosted_zone.VPCs = [vpc] else: nameservers = Join(',', GetAtt(hosted_zone, "NameServers")) self.template.add_output( Output("NameServers", Value=nameservers) ) self.template.add_resource(hosted_zone) self.hosted_zone_id = Ref(hosted_zone) self.template.add_output( Output("HostedZoneId", Value=self.hosted_zone_id) ) self.create_record_set_groups(variables["RecordSetGroups"]) return self.create_record_sets(variables["RecordSets"])
def __init__(self, key): for n, v in getattr(cfg, key).items(): mapname = f'{key}{n}' resname = v['ResourceName'] output_zonename = resname.replace('HostedZone', 'HostedZoneName') output_zoneidname = resname.replace('HostedZone', 'HostedZoneId') # parameters if n.startswith('Public'): p_HostedZone = Parameter(f'{mapname}Enabled') p_HostedZone.Description = ( f'Create Public {resname} - can be created in only one ' 'Region - empty for default based on env/role') p_HostedZoneId = Parameter(f'{mapname}Id') p_HostedZoneId.Description = ( f'Id of Public {resname} - required in all Regions where' ' HostedZonePublicEnv is not created - ' 'empty for default based on env/role') add_obj([ p_HostedZone, p_HostedZoneId, ]) # conditions c_Enabled = get_condition(resname, 'not_equals', 'None', f'{mapname}Enabled') add_obj(c_Enabled) # resources r_HostedZone = r53.HostedZone(v['ResourceName']) auto_get_props(r_HostedZone, v, recurse=True, mapname=mapname) if n.startswith('Public'): r_HostedZone.Condition = resname add_obj(r_HostedZone) # outputs o_HostedZoneName = Output(output_zonename) # o_HostedZoneName.Value = Sub(cfg.HostedZoneNamePrivate) o_HostedZoneName.Value = get_endvalue(f'{mapname}Name') o_HostedZoneId = Output(output_zoneidname) o_HostedZoneId.Value = If( resname, Ref(resname), get_endvalue(f'{mapname}Id') ) if n.startswith('Public') else Ref(resname) o_HostedZoneId.Export = Export(output_zoneidname) add_obj([ o_HostedZoneName, o_HostedZoneId, ])
def add_r53_dns(self): zone_name_index = 0 name, tags = self._name_tags('r53_dns') for zone in self.aws['r53_dns.zones'].split(','): zone_name = name + str(zone_name_index) self.r53_dns = self.t.add_resource( route53.HostedZone( zone_name, Name=zone, )) zone_output_name = "HostedZoneName" + str(zone_name_index) self.t.add_output(Output( zone_output_name, Value=Ref(self.r53_dns) )) zone_name_index += 1
def create_dns_records(self, bastion_host): private_hosted_zone = self.add_resource( r53.HostedZone('dnsPrivateHostedZone', Name=Join( '', [Ref(self.private_hosted_zone_name), '.']), VPCs=[ r53.HostedZoneVPCs(VPCId=Ref(self.vpc), VPCRegion=self.region) ])) self.add_resource( r53.RecordSetGroup( 'dnsPublicRecords', HostedZoneName=Join('', [Ref(self.public_hosted_zone_name), '.']), RecordSets=[ r53.RecordSet( 'dnsMonitoringServer', Name=Join('', [ 'monitoring.', Ref(self.public_hosted_zone_name), '.' ]), Type='A', TTL='300', ResourceRecords=[GetAtt(bastion_host, 'PublicIp')]) ])) self.add_resource( r53.RecordSetGroup( 'dnsPrivateRecords', HostedZoneId=Ref(private_hosted_zone), RecordSets=[ r53.RecordSet( 'dnsBastionHost', Name=Join('', [ 'monitoring.service.', Ref(self.private_hosted_zone_name), '.' ]), Type='A', TTL='10', ResourceRecords=[GetAtt(bastion_host, 'PrivateIp')]) ])) return private_hosted_zone
def add_hosted_zone(self, name, region): """ Helper that creates a hosted zone @param name [string] name of the private hosted zone that will be attached to the vpc @param region [string] name of the region, as if it could be different than the vpc's """ return self.add_resource( route53.HostedZone( "PrivateHostedZone", HostedZoneConfig=route53.HostedZoneConfiguration( Comment="Private HostedZone"), Name=name, VPCs=[ route53.HostedZoneVPCs(VPCId=Ref(self.vpc_id), VPCRegion=region) ]))
def create_internal_zone(self): t = self.template variables = self.get_variables() if variables["InternalDomain"]: t.add_resource( route53.HostedZone("InternalZone", Name=variables["InternalDomain"], VPCs=[ route53.HostedZoneVPCs( VPCId=VPC_ID, VPCRegion=Ref("AWS::Region")) ])) t.add_output(Output( "InternalZoneId", Value=Ref("InternalZone"), )) t.add_output( Output( "InternalZoneName", Value=variables["InternalDomain"], ))
def build_template(self): t = self._init_template() zone = t.add_resource( route53.HostedZone("{}HostedZone".format(self.name), Name=self.domain_name)) if self.vpc: vpc_param = ensure_param(t, self.vpc.output_vpc()) zone.VPCs = [ route53.HostedZoneVPCs(VPCId=Ref(vpc_param), VPCRegion=Ref("AWS::Region")) ] group = t.add_resource( route53.RecordSetGroup( "{}RecordGroup".format(self.name), HostedZoneId=Ref(zone), DependsOn=zone, RecordSets=[rs.add_to_template(t) for rs in self.records])) return t
def create_template(self): variables = self.get_variables() hosted_zone_name = variables["HostedZoneName"] hosted_zone_id = variables["HostedZoneId"] hosted_zone_comment = variables["Comment"] if all([hosted_zone_comment, hosted_zone_id]): logger.warning( "The Comment variable works when HostedZoneName is passed." "When HostedZoneId in variables, Comment is ignored.") if all([hosted_zone_name, hosted_zone_id]): raise ValueError("Cannot specify both 'HostedZoneName' and " "'HostedZoneId' variables.") if not any([hosted_zone_name, hosted_zone_id]): raise ValueError("Please specify either a 'HostedZoneName' or " "'HostedZoneId' variable.") if hosted_zone_id: self.hosted_zone_id = hosted_zone_id else: hosted_zone_config = route53.HostedZoneConfiguration( "HostedZoneConfiguration", Comment=hosted_zone_comment) self.template.add_resource( route53.HostedZone("HostedZone", Name=hosted_zone_name, HostedZoneConfig=hosted_zone_config)) self.hosted_zone_id = Ref("HostedZone") self.nameservers = Join(',', GetAtt("HostedZone", "NameServers")) self.template.add_output( Output("NameServers", Value=self.nameservers)) self.template.add_output( Output("HostedZoneId", Value=self.hosted_zone_id)) # return a list of troposphere record set objects. return self.create_record_sets(variables["RecordSets"])
def hosted_zone(self): return route53.HostedZone( 'SandboxPublicHostedZone', Name=self.domain, )
"VPC", EnableDnsSupport=True, CidrBlock=options.cidr, EnableDnsHostnames=True, Tags=Tags( Application=Ref("AWS::StackId"), ), )) HostedZone = t.add_resource(route53.HostedZone( "HostedZone", HostedZoneConfig=route53.HostedZoneConfiguration( Comment="Cell hosted zone" ), Name=Join("", cell_domain()), VPCs=[ route53.HostedZoneVPCs( VPCId=Ref(VPC), VPCRegion=Ref("AWS::Region") ) ] )) # region domain is ec2.internal in us-east-1, REGION.compute.internal for # others, according to # http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_DHCP_Options.html DHCP = t.add_resource(ec2.DHCPOptions( 'DHCP', DomainName=If( "RegionIsUsEast1", "ec2.internal",
def hosted_zone(self): return route53.HostedZone( 'PilosaZone', Name=Join('', [Ref(self.cluster_name), '.{domain}'.format(domain=self.domain)]), VPCs=[route53.HostedZoneVPCs(VPCId=Ref(self.vpc), VPCRegion=Ref('AWS::Region'))])
HttpVersion='http2', # Logging=cloudfront.Logging( # Bucket=GetAtt(CloudfrontLogBucket, 'DomainName'), # IncludeCookies=False # ), PriceClass='PriceClass_100', ), Tags=Tags(**shared_tags_args))) ### # Route 53 ### HostedZone = t.add_resource( route53.HostedZone( 'HostedZone', Name=CONFIG['DOMAIN_NAME'], HostedZoneConfig=route53.HostedZoneConfiguration( Comment=f'{CONFIG["STACK_NAME"]} stack HostedZone'), )) t.add_resource( route53.RecordSetGroup( 'HostedZoneRecordSetGroup', HostedZoneId=Ref(HostedZone), RecordSets=[ route53.RecordSet( 'HostedZoneAliasToCloudFront', Name=f'{CONFIG["DOMAIN_NAME"]}.', Type='A', AliasTarget=route53.AliasTarget( HostedZoneId= 'Z2FDTNDATAQYW2', # CloudFront HostedZoneId magic string