def create_dns_records(self): t = self.template variables = self.get_variables() # Setup CNAME to cluster if self.should_create_internal_hostname(): hostname = "%s.%s" % ( variables["InternalHostname"], variables["InternalZoneName"] ) read_hostname = "read.%s" % hostname t.add_resource( RecordSetType( DNS_RECORD, # Appends a "." to the end of the domain HostedZoneId=variables["InternalZoneId"], Comment="RDS DB CNAME Record", Name=hostname, Type="CNAME", TTL="120", ResourceRecords=[self.get_master_endpoint()], ) ) t.add_resource( RecordSetType( DNS_READ_RECORD, HostedZoneId=variables["InternalZoneId"], Comment="RDS DB CNAME Record (read endpoint)", Name=read_hostname, Type="CNAME", TTL="120", ResourceRecords=[self.get_read_endpoint()], ) )
def route53(template, instance, hostedzonename, type="A", subdomain=None, dns=None, depends=None): """Is is assumed that a hosted zone already has been registered with Amazon Route 53""" mydnsrecord = template.add_resource( RecordSetType( title=instance.title + "dns", HostedZoneName=Join("", [Ref(hostedzonename), '.']), Name=Join("", [ '' if not subdomain else subdomain + '.', Ref("AWS::StackName"), '' if not dns else "-" + dns, ".", Ref(hostedzonename), '.' ]), Type=type, TTL="300", ResourceRecords=[GetAtt(instance.title, "PublicIp")], )) if depends: mydnsrecord.DependsOn = depends return mydnsrecord
def create_load_balancer(self): t = self.template t.add_resource( elb.LoadBalancer('EmpireControllerLoadBalancer', HealthCheck=elb.HealthCheck( Target='HTTP:8080/health', HealthyThreshold=3, UnhealthyThreshold=3, Interval=5, Timeout=3), Listeners=self.setup_listeners(), SecurityGroups=[ Ref(ELB_SG_NAME), ], Subnets=Ref("PublicSubnets"))) # Setup ELB DNS t.add_resource( RecordSetType('EmpireControllerElbDnsRecord', Condition="UseDNS", HostedZoneName=Join("", [Ref("ExternalDomain"), "."]), Comment='Router ELB DNS', Name=Join('.', ["empire", Ref("ExternalDomain")]), Type='CNAME', TTL='120', ResourceRecords=[ GetAtt("EmpireControllerLoadBalancer", 'DNSName') ]))
def create_load_balancer(self): t = self.template elb_name = ELB_NAME % self.name elb_sg = ELB_SG_NAME % self.name t.add_resource(elb.LoadBalancer( elb_name, HealthCheck=elb.HealthCheck( Target='HTTP:80/', HealthyThreshold=3, UnhealthyThreshold=3, Interval=5, Timeout=3), Listeners=self.setup_listeners(), SecurityGroups=[Ref(elb_sg), ], Subnets=Ref("PublicSubnets"), Condition="CreateELB")) # Setup ELB DNS t.add_resource( RecordSetType( '%sDnsRecord' % elb_name, # Appends a '.' to the end of the domain HostedZoneName=Join("", [Ref("BaseDomain"), "."]), Comment='Router ELB DNS', Name=Join('.', [Ref("ELBHostName"), Ref("BaseDomain")]), Type='CNAME', TTL='120', ResourceRecords=[ GetAtt(elb_name, 'DNSName')], Condition="SetupELBDNS"))
def create_load_balancer(self): t = self.template t.add_resource( elb.LoadBalancer( "LoadBalancer", HealthCheck=elb.HealthCheck(Target="HTTP:8081/health", HealthyThreshold=3, UnhealthyThreshold=3, Interval=5, Timeout=3), ConnectionSettings=elb.ConnectionSettings( IdleTimeout=3600), # 1 hour Listeners=self.setup_listeners(), SecurityGroups=[ Ref(ELB_SG_NAME), ], Subnets=Ref("PublicSubnets"))) # Setup ELB DNS t.add_resource( RecordSetType("ElbDnsRecord", HostedZoneName=Join("", [Ref("ExternalDomain"), "."]), Comment="Router ELB DNS", Name=Join(".", ["empire", Ref("ExternalDomain")]), Type="CNAME", TTL="120", ResourceRecords=[GetAtt("LoadBalancer", "DNSName")]))
def define_dns(template, alb): route53_record = template.add_resource( RecordSetType(stack_name_strict + "WebDNS", HostedZoneName="taxweb.com.br.", Name=stack_name + ".taxweb.com.br.", ResourceRecords=[GetAtt(alb, "DNSName")], TTL=60, Type="CNAME")) return route53_record
def _add_resources(self): self.vpn_recordset = RecordSetType( "VPNRecordSet", HostedZoneName=Ref(self.hosted_zone), Name=Join(".", ["vpn", Ref(self.hosted_zone)]), Type="A", TTL=60, ResourceRecords=[Ref(self.vpn_public_ip)], ) self.template.add_resource(self.vpn_recordset)
def create_record(name, route53_zone, route53_stack, target_elbv2, elbv2_stack) -> None: """ Create a new RecordResource with the given DNS Name pointing to the ELB :param str name: :param ecs_composex.route53.route53_zone.HostedZone route53_zone: :param ecs_composex.route53.route53_stack.XStack route53_stack: :param ecs_composex.elbv2.elbv2_stack.Elbv2 target_elbv2: :param ComposeXStack elbv2_stack: """ if not target_elbv2.attributes_outputs: target_elbv2.init_outputs() target_elbv2.generate_outputs() add_outputs(elbv2_stack.stack_template, target_elbv2.outputs) lb_zone_id = target_elbv2.attributes_outputs[LB_DNS_ZONE_ID] lb_dns_name = target_elbv2.attributes_outputs[LB_DNS_NAME] add_parameters( route53_stack.stack_template, [lb_zone_id["ImportParameter"], lb_dns_name["ImportParameter"]], ) route53_stack.Parameters.update({ lb_zone_id["ImportParameter"].title: lb_zone_id["ImportValue"], lb_dns_name["ImportParameter"].title: lb_dns_name["ImportValue"], }) elbv2_alias = AliasTarget( HostedZoneId=Ref(lb_zone_id["ImportParameter"]), DNSName=Ref(lb_dns_name["ImportParameter"]), ) record_props = { "AliasTarget": elbv2_alias, "Region": Ref(AWS_REGION), "Type": "A", "Name": name, } if not keyisset("SetIdentifier", record_props): record_props["SetIdentifier"] = Ref(AWS_STACK_NAME) if route53_zone.cfn_resource: zone_id_attribute = GetAtt(route53_zone.cfn_resource, PUBLIC_DNS_ZONE_ID.return_value) record_props["HostedZoneId"] = zone_id_attribute elif route53_zone.mappings: zone_id_attribute = route53_zone.attributes_outputs[PUBLIC_DNS_ZONE_ID] record_props["HostedZoneId"] = zone_id_attribute["ImportValue"] cfn_resource = RecordSetType( f"elbv2{target_elbv2.logical_name}Route53{record_props['Type']}{NONALPHANUM.sub('', record_props['Name'])}"[: 128], **record_props, ) if cfn_resource.title not in route53_stack.stack_template.resources: route53_stack.stack_template.add_resource(cfn_resource) if elbv2_stack.title not in route53_stack.DependsOn: route53_stack.DependsOn.append(elbv2_stack.title)
def dns_adder(self, dns, elbid): dns_record_id = "DNS" + str(elbid) self.template.add_resource( RecordSetType( dns_record_id, HostedZoneName=Join("", [Ref("BaseURL"), "."]), Name=Join("", [dns, ".", Ref("BaseURL"), "."]), Type="CNAME", TTL="900", ResourceRecords=[GetAtt(elbid, "DNSName")], ))
def host_dns_adder(self, dns, instanceid): dns_record_id = "DNS" + str(instanceid) self.template.add_resource( RecordSetType( dns_record_id, HostedZoneName=Join("", [Ref("BaseURL"), "."]), Name=Join("", [dns, ".", Ref("BaseURL"), "."]), Type="A", TTL="600", ResourceRecords=[GetAtt(instanceid, "PrivateIp") ], # PrivateIP PrivateDnsName ))
def create_route53_records(self, load_balancer): self.t.add_resource( RecordSetType("ARecord", AliasTarget=AliasTarget( HostedZoneId=GetAtt(load_balancer, "CanonicalHostedZoneNameID"), DNSName=GetAtt(load_balancer, "DNSName")), HostedZoneId="Z2XJ10AKMCBNEF", Comment="A Record Alias to ELB", Name="loremipsum.tv17.co.uk.", Type="A", DependsOn="LoadBalancer"))
def create_private_dns_elb(template, hosted_zone, name, elb, cf_resource_name): dns_record = template.add_resource(RecordSetType( cf_resource_name, HostedZoneName=Join("", [hosted_zone, "."]), Comment="DNS name for my instance.", Name=Join(".", [name, hosted_zone, ""]), Type="A", AliasTarget=AliasTarget(GetAtt(elb, "CanonicalHostedZoneNameID"), GetAtt(elb, "CanonicalHostedZoneName")), DependsOn=elb )) return dns_record
def rds_adder(self, instance_identifier, allocated_storage, db_subnet_group, rds_group, db_size, db_name='MyDB', storage_type='gp2', engine_version='5.5.40a', storage_engine='MySQL', publicly_accessible=False): db_names = '' db_asf = (db_name.upper(), 'DNS') if publicly_accessible is False: publicly_accessible = "false" else: publicly_accessible = "true" dbinstance = rds.DBInstance( db_name, DBInstanceIdentifier=instance_identifier, Engine=storage_engine, EngineVersion=engine_version, MasterUsername=If("NotRestoringFromSnapshot", Ref("RDSDBUser"), Ref("AWS::NoValue")), MasterUserPassword=If("NotRestoringFromSnapshot", Ref("RDSDBPassword"), Ref("AWS::NoValue")), AllocatedStorage=allocated_storage, DBSnapshotIdentifier=If("NotRestoringFromSnapshot", Ref("AWS::NoValue"), Ref("RDSSnapshot")), StorageType=storage_type, DBSubnetGroupName=db_subnet_group, PubliclyAccessible=publicly_accessible, VPCSecurityGroups=rds_group, DBInstanceClass=db_size, StorageEncrypted=If("NotRestoringFromSnapshot", True, Ref("AWS::NoValue"))) dbdnsrecord = RecordSetType( db_names.join(db_asf), HostedZoneName=Join("", [Ref("RDSDNSDomain"), "."]), Name=Join("", [Ref("RDSDNSName"), ".", Ref("RDSDNSDomain"), "."]), Type="CNAME", TTL="900", ResourceRecords=[GetAtt(dbinstance, "Endpoint.Address")], ) self.template.add_resource(dbinstance) self.template.add_resource(dbdnsrecord)
def create_dns_records(self): t = self.template primary_endpoint = self.get_primary_address() t.add_resource( RecordSetType( DNS_RECORD, HostedZoneId=Ref("InternalZoneId"), Comment="ReplicationGroup CNAME Record", Name=Join(".", [Ref("InternalHostname"), Ref("InternalZoneName")]), Type="CNAME", TTL="120", ResourceRecords=[primary_endpoint], Condition="CreateInternalHostname"))
def elb(template, name, elasticLB, hostedzonename, subdomain=None, dns=None): """Is is assumed that a hosted zone already has been registered with Amazon Route 53""" mydnsrecord = template.add_resource( RecordSetType( name, HostedZoneName=Join("", [Ref(hostedzonename), '.']), Name=Join("", [ '' if not subdomain else subdomain + '.', Ref("AWS::StackName"), '' if not dns else "-" + dns, ".", Ref(hostedzonename), '.' ]), Type="CNAME", TTL="300", ResourceRecords=[GetAtt(elasticLB, "DNSName")], )) return mydnsrecord
def create_record(awsclient, name_prefix, instance_reference, type="A", host_zone_name=None): """ Builds route53 record entries enabling DNS names for services Note: gcdt.route53 create_record(awsclient, ...) is used in dataplatform cloudformation.py templates! :param name_prefix: The sub domain prefix to use :param instance_reference: The EC2 troposphere reference which's private IP should be linked to :param type: The type of the record A or CNAME (default: A) :param host_zone_name: The host zone name to use (like preprod.ds.glomex.cloud. - DO NOT FORGET THE DOT!) :return: RecordSetType """ # Only fetch the host zone from the COPS stack if nessary if host_zone_name is None: host_zone_name = _retrieve_stack_host_zone_name(awsclient) if not (type == "A" or type == "CNAME"): raise Exception("Record set type is not supported!") name_of_record = name_prefix \ .replace('.', '') \ .replace('-', '') \ .title() + "HostRecord" # Reference EC2 instance automatically to their private IP if isinstance(instance_reference, Instance): resource_record = troposphere.GetAtt(instance_reference, "PrivateIp") else: resource_record = instance_reference return RecordSetType( name_of_record, HostedZoneName=host_zone_name, Name=troposphere.Join("", [ name_prefix + ".", host_zone_name, ]), Type=type, TTL=TTL_DEFAULT, ResourceRecords=[resource_record], )
def create_dns_records(self): t = self.template endpoint = self.get_db_endpoint() # Setup CNAME to db t.add_resource( RecordSetType( DNS_RECORD, # Appends a "." to the end of the domain HostedZoneId=Ref("InternalZoneId"), Comment="RDS DB CNAME Record", Name=Join(".", [Ref("InternalHostname"), Ref("InternalZoneName")]), Type="CNAME", TTL="120", ResourceRecords=[endpoint], Condition="CreateInternalHostname"))
def create_dns_records(self): t = self.template variables = self.get_variables() primary_endpoint = self.get_primary_address() if self.should_create_internal_cname(): t.add_resource( RecordSetType(DNS_RECORD, HostedZoneId=variables["InternalZoneId"], Comment="ReplicationGroup CNAME Record", Name=Join(".", [ variables["InternalHostname"], variables["InternalZoneName"] ]), Type="CNAME", TTL="120", ResourceRecords=[primary_endpoint]))
def create_or_update_dns_record(stack, record_name, record_type, record_value, hosted_zone_name, condition_field=''): """Create or Update Route53 Record Resource.""" return stack.stack.add_resource( RecordSetType('{0}'.format( record_name.replace('.', '').replace('*', 'wildcard')), Condition=condition_field, HostedZoneName='{0}.'.format(hosted_zone_name), Type=record_type, TTL='60', Name='{0}.'.format(record_name), ResourceRecords=record_value))
def create_rds(self): t = self.template db_name = RDS_INSTANCE_NAME % self.name t.add_resource( DBInstance(db_name, AllocatedStorage=Ref('AllocatedStorage'), AllowMajorVersionUpgrade=False, AutoMinorVersionUpgrade=True, BackupRetentionPeriod=30, DBName=Ref('DBName'), DBInstanceClass=Ref('InstanceType'), DBSubnetGroupName=Ref(RDS_SUBNET_GROUP % self.name), Engine='postgres', EngineVersion='9.3.5', MasterUsername=Ref('MasterUser'), MasterUserPassword=Ref('MasterUserPassword'), MultiAZ=True, PreferredBackupWindow=Ref('PreferredBackupWindow'), VPCSecurityGroups=[ Ref(RDS_SG_NAME % self.name), ])) endpoint = GetAtt(db_name, 'Endpoint.Address') # Setup CNAME to db t.add_resource( RecordSetType( '%sDnsRecord' % db_name, # Appends a '.' to the end of the domain HostedZoneId=Ref("InternalZoneId"), Comment='RDS DB CNAME Record', Name=Join(".", [Ref("InternalHostname"), Ref("InternalZoneName")]), Type='CNAME', TTL='120', ResourceRecords=[endpoint], Condition="CreateInternalHostname")) t.add_output(Output('DBAddress', Value=endpoint)) t.add_output( Output('DBCname', Condition="CreateInternalHostname", Value=Ref("%sDnsRecord" % db_name)))
def redis_adder_replcation(self, name, tags, instance_type='cache.m3.medium', cache_clusters=2, version='3.2.4'): rule = self.rule_adder(6379, cidr='10.0.0.0/16') subnetname = Join("", [name, Ref("DeploymentEnvironment")]) self.group_adder("redissg", [rule]) subnetgroup = self.template.add_resource( elasticache.SubnetGroup( "SubnetGroup", CacheSubnetGroupName=subnetname, Description='Subnet Group for ElasticCache Redis {0}'.format( name), SubnetIds=self.config['subnets'])) self.template.add_resource( elasticache.ReplicationGroup( 'RedisReplicationGroup', ReplicationGroupId=name, Engine='redis', EngineVersion=version, CacheNodeType=instance_type, NumCacheClusters=cache_clusters, Tags=tags, CacheSubnetGroupName=Ref(subnetgroup), ReplicationGroupDescription="%s replication group" % name, SecurityGroupIds=[GetAtt('redissg', "GroupId")], )) redisdnsrecord = RecordSetType( "RedisDNSRecord", HostedZoneName=Join("", [Ref("RedisDNSDomain"), "."]), Name=Join("", [Ref("RedisDNSName"), ".", Ref("RedisDNSDomain"), "."]), Type="CNAME", TTL="900", ResourceRecords=[ GetAtt("RedisReplicationGroup", "PrimaryEndPoint.Address") ], ) self.template.add_resource(redisdnsrecord)
InstanceType="t1.micro", KeyName="mcheriyath", SecurityGroups=["mcheriyath-sg"], Tags=Tags(**{ 'Name': 'DevOpsDenver', 'Owner': '*****@*****.**' }))) myDNSRecord = template.add_resource( RecordSetType( "pkdenec2mc", HostedZoneName=Join("", [Ref(hostedzone), "."]), Comment="DNS name for my instance.", Name=Join("", [ Ref(ec2_instance_1), ".", Ref("AWS::Region"), ".", Ref(hostedzone), "." ]), Type="A", TTL="900", ResourceRecords=[GetAtt("Ec2Instance1", "PublicIp")], )) template.add_output([ Output( "InstanceId1", Description="InstanceId of the newly created EC2 instance", Value=Ref(ec2_instance_1), ), Output( "PublicIP1",
ErrorCode='503', ), cf.CustomErrorResponse( ErrorCachingMinTTL='0', ErrorCode='504', ), ], ), Tags=DefaultTags)) cdnARecord = t.add_resource( RecordSetType("{}{}Adns".format(app_group_ansi, src_domain.replace('.', '0')), HostedZoneName='{}.'.format(dns_domain), Comment="{} domain record".format(cdn_domain), Name='{}'.format(cdn_domain), Type="A", AliasTarget=AliasTarget(HostedZoneId=cfront_zone_id, DNSName=GetAtt( cdnDistribution, "DomainName")))) cdnAAAARecord = t.add_resource( RecordSetType("{}{}AAAAdns".format(app_group_ansi, src_domain.replace('.', '0')), HostedZoneName='{}.'.format(dns_domain), Comment="{} domain record".format(cdn_domain), Name='{}'.format(cdn_domain), Type="AAAA", AliasTarget=AliasTarget(HostedZoneId=cfront_zone_id, DNSName=GetAtt( cdnDistribution,
def generate_cloudformation_template(): enable_elb = sys.argv[1] input_scaling_policies = ast.literal_eval(sys.argv[2]) input_alarms = ast.literal_eval(sys.argv[3]) enable_elb = enable_elb == 'True' elb_listeners = ast.literal_eval(sys.argv[4]) template = Template() template.add_description("""\ Configures Auto Scaling Group for the app""") project_name = template.add_parameter( Parameter( "Name", Type="String", Description="Instances will be tagged with this name", )) scalecapacity = template.add_parameter( Parameter( "ScaleCapacity", Default="1", Type="String", Description="Number of api servers to run", )) minsize = template.add_parameter( Parameter( "MinScale", Type="String", Description="Minimum number of servers to keep in the ASG", )) maxsize = template.add_parameter( Parameter( "MaxScale", Type="String", Description="Maximum number of servers to keep in the ASG", )) signalcount = template.add_parameter( Parameter( "SignalCount", Default="1", Type="String", Description= "No. of signals CF must receive before it sets the status as CREATE_COMPLETE", )) signaltimeout = template.add_parameter( Parameter( "SignalTimeout", Default="PT5M", Type="String", Description= "Time that CF waits for the number of signals that was specified in Count ", )) minsuccessfulinstancespercent = template.add_parameter( Parameter( "MinSuccessfulInstancesPercent", Default="100", Type="String", Description= "% instances in a rolling update that must signal success for CF to succeed", )) environment = template.add_parameter( Parameter( "Environment", Type="String", Description="The environment being deployed into", )) subnet = template.add_parameter( Parameter( "Subnets", Type="CommaDelimitedList", )) launchconfigurationname = template.add_parameter( Parameter( "LaunchConfigurationName", Type="String", )) health_check_grace_period = template.add_parameter( Parameter( "HealthCheckGracePeriod", Type="String", Default="300", )) if enable_elb: elb_subnets = template.add_parameter( Parameter( "LoadBalancerSubnets", Type="CommaDelimitedList", )) elb_bucket_name = template.add_parameter( Parameter("LoadBalancerBucketName", Type="String", Description="S3 Bucket for the ELB access logs")) template.add_condition("ElbLoggingCondition", Not(Equals(Ref(elb_bucket_name), ""))) elb_schema = template.add_parameter( Parameter( "LoadBalancerSchema", Type="String", )) health_check_interval = template.add_parameter( Parameter( "LoadBalancerHealthCheckInterval", Type="String", )) health_check_timeout = template.add_parameter( Parameter( "LoadBalancerHealthCheckTimeout", Type="String", )) healthy_threshold = template.add_parameter( Parameter( "LoadBalancerHealthyThreshold", Type="String", )) unhealthy_threshold = template.add_parameter( Parameter( "LoadBalancerUnHealthyThreshold", Type="String", )) enable_connection_draining = template.add_parameter( Parameter( "LoadBalancerEnableConnectionDraining", Type="String", Default="True", )) connection_draining_timeout = template.add_parameter( Parameter( "LoadBalancerConnectionDrainingTimeout", Type="String", Default="30", )) loadbalancersecuritygroup = template.add_parameter( Parameter( "LoadBalancerSecurityGroup", Type="CommaDelimitedList", Description="Security group for api app load balancer.", )) hostedzone = template.add_parameter( Parameter( "HostedZoneName", Description= "The DNS name of an existing Amazon Route 53 hosted zone", Type="String", )) dns_record = template.add_parameter( Parameter( "DNSRecord", Type="String", )) dns_ttl = template.add_parameter( Parameter( "DNSTTL", Default="300", Type="String", )) new_weight = template.add_parameter( Parameter( "NewDnsWeight", Type="String", Default="100", )) health_check_protocol = template.add_parameter( Parameter( "LoadBalancerHealthCheckProtocol", Type="String", )) template.add_condition("ElbTCPProtocolCondition", Equals(Ref(health_check_protocol), "TCP")) health_check_port = template.add_parameter( Parameter( "LoadBalancerHealthCheckPort", Type="String", )) health_check_path = template.add_parameter( Parameter( "LoadBalancerHealthCheckPath", Type="String", )) load_balancer_listeners = [] for listener in elb_listeners: load_balancer_listeners.append( elb.Listener( LoadBalancerPort=listener['load_balancer_port'], InstancePort=listener['instance_port'], Protocol=listener['protocol'], InstanceProtocol=Ref(health_check_protocol), )) loadbalancer = template.add_resource( elb.LoadBalancer( "LoadBalancer", AccessLoggingPolicy=If( "ElbLoggingCondition", elb.AccessLoggingPolicy(EmitInterval=60, Enabled=True, S3BucketName=Ref(elb_bucket_name), S3BucketPrefix="ELBLogs"), Ref("AWS::NoValue")), ConnectionDrainingPolicy=elb.ConnectionDrainingPolicy( Enabled=Ref(enable_connection_draining), Timeout=Ref(connection_draining_timeout), ), Subnets=Ref(elb_subnets), HealthCheck=elb.HealthCheck( Target=Join("", [ Ref(health_check_protocol), ":", Ref(health_check_port), If("ElbTCPProtocolCondition", Ref("AWS::NoValue"), Ref(health_check_path)) ]), HealthyThreshold=Ref(healthy_threshold), UnhealthyThreshold=Ref(unhealthy_threshold), Interval=Ref(health_check_interval), Timeout=Ref(health_check_timeout), ), Listeners=load_balancer_listeners, CrossZone=True, SecurityGroups=Ref(loadbalancersecuritygroup), Scheme=Ref(elb_schema))) route53record = template.add_resource( RecordSetType( "DNS", HostedZoneName=Join("", [Ref(hostedzone), "."]), Name=Join("", [Ref(dns_record), ".", Ref(hostedzone), "."]), ResourceRecords=[GetAtt(loadbalancer, "DNSName")], SetIdentifier=Ref(project_name), TTL=Ref(dns_ttl), Type="CNAME", Weight=Ref(new_weight), )) autoscalinggroup = template.add_resource( AutoScalingGroup( "AutoscalingGroup", Tags=[ Tag("Name", Ref(project_name), True), Tag("Environment", Ref(environment), True) ], LaunchConfigurationName=Ref(launchconfigurationname), MinSize=Ref(minsize), MaxSize=Ref(maxsize), DesiredCapacity=Ref(scalecapacity), VPCZoneIdentifier=Ref(subnet), HealthCheckGracePeriod=Ref(health_check_grace_period), CreationPolicy=CreationPolicy( ResourceSignal=ResourceSignal(Count=Ref(signalcount), Timeout=Ref(signaltimeout)), AutoScalingCreationPolicy=AutoScalingCreationPolicy( MinSuccessfulInstancesPercent=Ref( minsuccessfulinstancespercent))), UpdatePolicy=UpdatePolicy( AutoScalingRollingUpdate=AutoScalingRollingUpdate( MaxBatchSize='1', MinInstancesInService='1', MinSuccessfulInstancesPercent=Ref( minsuccessfulinstancespercent), PauseTime=Ref(signaltimeout), WaitOnResourceSignals=True)))) autoscalinggroup.HealthCheckType = 'EC2' if enable_elb: autoscalinggroup.LoadBalancerNames = [Ref(loadbalancer)] autoscalinggroup.HealthCheckType = 'ELB' created_scaling_policies = dict() for scaling_policy in input_scaling_policies: policy_properties = { 'AdjustmentType': scaling_policy['adjustment_type'], 'AutoScalingGroupName': Ref(autoscalinggroup), 'Cooldown': scaling_policy['cooldown'], 'PolicyType': scaling_policy['policy_type'], 'ScalingAdjustment': scaling_policy['scaling_adjustment'], } if scaling_policy['policy_type'] != "SimpleScaling" \ and 'estimated_instance_warmup' in scaling_policy: policy_properties['EstimatedInstanceWarmup'] = \ scaling_policy['estimated_instance_warmup'] if scaling_policy['policy_type'] != "SimpleScaling" \ and 'metric_aggregation_type' in scaling_policy: policy_properties['MetricAggregationType'] = scaling_policy[ 'metric_aggregation_type'] if scaling_policy['adjustment_type'] == "PercentChangeInCapacity" \ and 'min_adjustment_magnitude' in scaling_policy: policy_properties['MinAdjustmentMagnitude'] = scaling_policy[ 'min_adjustment_magnitude'] if 'step_adjustments' in scaling_policy: policy_properties['StepAdjustments'] = scaling_policy[ 'step_adjustments'] created_scaling_policies[ scaling_policy['name']] = template.add_resource( ScalingPolicy(scaling_policy['name'], **policy_properties)) for alarm in input_alarms: template.add_resource( Alarm( alarm['name'], ActionsEnabled=True, AlarmActions=[ Ref(created_scaling_policies[alarm['scaling_policy_name']]) ], AlarmDescription=alarm['description'], ComparisonOperator=alarm['comparison'], Dimensions=[ MetricDimension(Name="AutoScalingGroupName", Value=Ref(autoscalinggroup)), ], EvaluationPeriods=alarm['evaluation_periods'], InsufficientDataActions=[], MetricName=alarm['metric'], Namespace=alarm['namespace'], OKActions=[], Period=alarm['period'], Statistic=alarm['statistics'], Threshold=str(alarm['threshold']), Unit=alarm['unit'], )) template.add_output( Output("StackName", Value=Ref(project_name), Description="Stack Name")) if enable_elb: template.add_output( Output("DomainName", Value=Ref(route53record), Description="DNS to access the service")) template.add_output( Output("LoadBalancer", Value=GetAtt(loadbalancer, "DNSName"), Description="ELB dns")) template.add_output( Output("AutoScalingGroup", Value=Ref(autoscalinggroup), Description="Auto Scaling Group")) template.add_output( Output("LaunchConfiguration", Value=Ref(launchconfigurationname), Description="LaunchConfiguration for this deploy")) return template
def create_cloudfront_template(): template = Template() cname = template.add_parameter( parameter=Parameter(title='Cname', Type='String')) acm_certificate_arn = template.add_parameter( parameter=Parameter(title='AcmCertificateArn', Type='String')) host_zone_id = template.add_parameter( parameter=Parameter(title='HostZoneId', Type='String')) bucket = template.add_resource( resource=Bucket(title='SampleBucket', BucketName=Sub('sample-bucket-${AWS::AccountId}'))) identity = template.add_resource(resource=CloudFrontOriginAccessIdentity( title='SampleOriginAccessIdentity', CloudFrontOriginAccessIdentityConfig= CloudFrontOriginAccessIdentityConfig(Comment='sample'))) template.add_resource(resource=BucketPolicy( title='SampleBucketPolicy', Bucket=Ref(bucket), PolicyDocument={ 'Statement': [{ 'Action': 's3:GetObject', 'Effect': 'Allow', 'Resource': Join(delimiter='/', values=[GetAtt(bucket, 'Arn'), '*']), 'Principal': { 'CanonicalUser': GetAtt(logicalName=identity, attrName='S3CanonicalUserId') } }] })) distribution = template.add_resource(resource=Distribution( title='SampleDistribution', DistributionConfig=DistributionConfig( Aliases=[Ref(cname)], # CustomErrorResponses=[ # CustomErrorResponse( # ErrorCode=403, # ResponseCode=200, # ResponsePagePath='/404.html', # ErrorCachingMinTTL=30 # ) # ], DefaultCacheBehavior=DefaultCacheBehavior( ForwardedValues=ForwardedValues(QueryString=True, ), TargetOriginId=Sub('S3-${' + bucket.title + '}'), ViewerProtocolPolicy='redirect-to-https', ), # DefaultRootObject='index.html', Enabled=True, Origins=[ Origin(Id=Sub('S3-${' + bucket.title + '}'), DomainName=Sub('${' + bucket.title + '}.s3.amazonaws.com'), S3OriginConfig=S3OriginConfig(OriginAccessIdentity=Sub( 'origin-access-identity/cloudfront/${' + identity.title + '}'))) ], ViewerCertificate=ViewerCertificate(AcmCertificateArn=Ref( acm_certificate_arn), SslSupportMethod='sni-only')))) template.add_resource(resource=RecordSetType( title='SampleRecordSet', AliasTarget=AliasTarget(HostedZoneId='Z2FDTNDATAQYW2', DNSName=GetAtt(logicalName=distribution, attrName='DomainName')), HostedZoneId=Ref(host_zone_id), Name=Ref(cname), Type='A')) with open('./cloudfront.yml', mode='w') as file: file.write(template.to_yaml())
def generate_cloudformation_template(): template = Template() template.add_description("""\ Configures Auto Scaling Group for the app""") hostedzone = template.add_parameter( Parameter( "HostedZoneName", Description= "The DNS name of an existing Amazon Route 53 hosted zone", Type="String", )) dnsRecord = template.add_parameter(Parameter( "DNSRecord", Type="String", )) loadbalancersecuritygroup = template.add_parameter( Parameter( "LoadBalancerSecurityGroup", Type="String", Description="Security group for api app load balancer.", )) scalecapacity = template.add_parameter( Parameter( "ScaleCapacity", Default="1", Type="String", Description="Number of api servers to run", )) minsize = template.add_parameter( Parameter( "MinScale", Type="String", Description="Minimum number of servers to keep in the ASG", )) maxsize = template.add_parameter( Parameter( "MaxScale", Type="String", Description="Maximum number of servers to keep in the ASG", )) environment = template.add_parameter( Parameter( "Environment", Type="String", Description="The environment being deployed into", )) subnet = template.add_parameter( Parameter( "Subnets", Type="CommaDelimitedList", )) loadbalancername = template.add_parameter( Parameter( "LoadBalancerName", Type="String", )) elbSchema = template.add_parameter( Parameter( "LoadBalancerSchema", Type="String", )) healthCheckTarget = template.add_parameter( Parameter( "LoadBalancerHealthCheckTarget", Type="String", )) healthCheckInterval = template.add_parameter( Parameter( "LoadBalancerHealthCheckInterval", Type="String", )) healthCheckTimeout = template.add_parameter( Parameter( "LoadBalancerHealthCheckTimeout", Type="String", )) healthyThreshold = template.add_parameter( Parameter( "LoadBalancerHealthyThreshold", Type="String", )) unhealthyThreshold = template.add_parameter( Parameter( "LoadBalancerUnHealthyThreshold", Type="String", )) launchconfigurationname = template.add_parameter( Parameter( "LaunchConfigurationName", Type="String", )) loadbalancer = template.add_resource( elb.LoadBalancer( "LoadBalancer", ConnectionDrainingPolicy=elb.ConnectionDrainingPolicy( Enabled=True, Timeout=30, ), Subnets=Ref(subnet), HealthCheck=elb.HealthCheck( Target=Ref(healthCheckTarget), HealthyThreshold=Ref(healthyThreshold), UnhealthyThreshold=Ref(unhealthyThreshold), Interval=Ref(healthCheckInterval), Timeout=Ref(healthCheckTimeout), ), Listeners=[ elb.Listener( LoadBalancerPort="80", InstancePort="80", Protocol="HTTP", InstanceProtocol="HTTP", ), ], CrossZone=True, SecurityGroups=[Ref(loadbalancersecuritygroup)], LoadBalancerName=Ref(loadbalancername), Scheme=Ref(elbSchema), )) autoscalinggroup = template.add_resource( AutoScalingGroup("AutoscalingGroup", Tags=[Tag("Environment", Ref(environment), True)], LaunchConfigurationName=Ref(launchconfigurationname), MinSize=Ref(minsize), MaxSize=Ref(maxsize), DesiredCapacity=Ref(scalecapacity), LoadBalancerNames=[Ref(loadbalancer)], VPCZoneIdentifier=Ref(subnet), HealthCheckType='ELB', HealthCheckGracePeriod=30, UpdatePolicy=UpdatePolicy( AutoScalingRollingUpdate=AutoScalingRollingUpdate( PauseTime='PT1M', MinInstancesInService="1", MaxBatchSize='1')))) route53record = template.add_resource( RecordSetType( "myDNSRecord", HostedZoneName=Join("", [Ref(hostedzone), "."]), Name=Join("", [Ref(dnsRecord), ".", Ref(hostedzone), "."]), Type="CNAME", TTL="300", ResourceRecords=[GetAtt(loadbalancer, "DNSName")], )) template.add_output( Output("DomainName", Value=Ref(route53record), Description="DNS to access the service")) template.add_output( Output("LoadBalancer", Value=GetAtt(loadbalancer, "DNSName"), Description="ELB dns")) template.add_output( Output("AutoScalingGroup", Value=Ref(autoscalinggroup), Description="Created Auto Scaling Group")) template.add_output( Output("LaunchConfiguration", Value=Ref(launchconfigurationname), Description="LaunchConfiguration for this deploy")) return template
t.set_description( "AWS CloudFormation Sample Template Route53_CNAME: Sample template " "showing how to create an Amazon Route 53 CNAME record. It assumes that " "you already have a Hosted Zone registered with Amazon Route 53. " "**WARNING** This template creates an Amazon EC2 instance. " "You will be billed for the AWS resources used if you create " "a stack from this template.") hostedzone = t.add_parameter(Parameter( "HostedZone", Description="The DNS name of an existing Amazon Route 53 hosted zone", Type="String", )) myDNSRecord = t.add_resource(RecordSetType( "myDNSRecord", HostedZoneName=Join("", [Ref(hostedzone), "."]), Comment="CNAME redirect to aws.amazon.com.", Name=Join("", [Ref("AWS::StackName"), ".", Ref("AWS::Region"), ".", Ref(hostedzone), "."]), Type="CNAME", TTL="900", ResourceRecords=["aws.amazon.com"] )) t.add_output(Output("DomainName", Value=Ref(myDNSRecord))) print(t.to_json())
)) ec2EIP = t.add_resource( EIP( "ec2EIP", InstanceId=Ref(ec2Instance), Domain='vpc', )) hostRecordSet = t.add_resource( RecordSetType( "hostRecordSet", HostedZoneName=Join("", [Ref(hostedZone_param), "."]), Comment="DNS name for my instance.", Name=Join( ".", [Ref(hostName_param), Ref(hostedZone_param)]), Type="A", TTL="900", ResourceRecords=[GetAtt("Ec2Instance", "PublicIp")], )) t.add_output([ Output( "InstanceId", Description="InstanceId of the newly created EC2 instance", Value=Ref(ec2Instance), ), Output( "AZ", Description="Availability Zone of the newly created EC2 instance",
def main(): template = Template() template.add_version("2010-09-09") template.set_description("AWS CloudFormation ECS Service") # Add the Parameters Application = template.add_parameter( Parameter( "Application", Type="String", )) DockerImage = template.add_parameter( Parameter( "DockerImage", Type="String", )) USERNAME = template.add_parameter(Parameter( "USERNAME", Type="String", )) ClusterName = template.add_parameter( Parameter( "ClusterName", Type="String", )) ContainerPort = template.add_parameter( Parameter( "ContainerPort", Type="String", )) HostPort = template.add_parameter(Parameter( "HostPort", Type="String", )) HostedZoneName = template.add_parameter( Parameter( "HostedZoneName", Type="String", )) CertArn = template.add_parameter(Parameter( "CertArn", Type="String", )) ExecutionRoleArn = template.add_parameter( Parameter("ExecutionRoleArn", Type="String", Description="Execution Role to get creadentials from ssm")) HealthCheckPath = template.add_parameter( Parameter( "HealthCheckPath", Type="String", )) HealthCheckIntervalSeconds = template.add_parameter( Parameter( "HealthCheckIntervalSeconds", Type="String", )) HealthyThresholdCount = template.add_parameter( Parameter( "HealthyThresholdCount", Type="String", )) HealthCheckTimeoutSeconds = template.add_parameter( Parameter( "HealthCheckTimeoutSeconds", Type="String", )) UnhealthyThresholdCount = template.add_parameter( Parameter( "UnhealthyThresholdCount", Type="String", )) VpcId = template.add_parameter(Parameter( "VpcId", Type="String", )) Subnets = template.add_parameter( Parameter( "Subnets", Type="List<AWS::EC2::Subnet::Id>", )) # Add the application ELB NetworkLB = template.add_resource( elb.LoadBalancer("NetworkLB", Name=Join("", [Ref(Application), "-nlb"]), Scheme="internet-facing", Subnets=Ref(Subnets), Type='network')) NlbTargetGroup = template.add_resource( elb.TargetGroup( "NlbTargetGroup", Name='ecs-service-targetgroup', HealthCheckIntervalSeconds=Ref(HealthCheckIntervalSeconds), HealthCheckProtocol="TCP", HealthyThresholdCount=Ref(HealthyThresholdCount), Port=80, Protocol="TCP", UnhealthyThresholdCount=Ref(UnhealthyThresholdCount), VpcId=Ref(VpcId))) NlbListener = template.add_resource( elb.Listener( "Listener", DependsOn=["NlbTargetGroup", "NetworkLB"], Certificates=[elb.Certificate(CertificateArn=Ref(CertArn))], Port="443", Protocol="TLS", LoadBalancerArn=Ref(NetworkLB), DefaultActions=[ elb.Action(Type="forward", TargetGroupArn=Ref(NlbTargetGroup)) ])) Task_Definition = template.add_resource( TaskDefinition( 'TaskDefinition', Memory='500', ExecutionRoleArn=Ref(ExecutionRoleArn), ContainerDefinitions=[ ContainerDefinition( Name=Join("", [Ref(Application)]), Image=Ref(DockerImage), Essential=True, Secrets=[Secret(Name='USERNAME', ValueFrom=Ref(USERNAME))], Environment=[ Environment(Name="DOCKER_LABELS", Value="true") ], DockerLabels={ 'aws-account': Ref("AWS::AccountId"), 'region': Ref("AWS::Region"), 'stack': Ref("AWS::StackName") }, PortMappings=[ PortMapping(ContainerPort=Ref(ContainerPort), HostPort=Ref(HostPort)) ]) ])) app_service = template.add_resource( Service("AppService", DependsOn=["Listener", "TaskDefinition"], Cluster=Ref(ClusterName), DesiredCount=1, TaskDefinition=Ref(Task_Definition), ServiceName=Join("", [Ref(Application), "-ecs-service"]), LoadBalancers=[ ecs.LoadBalancer(ContainerName=Join( "", [Ref(Application)]), ContainerPort=Ref(ContainerPort), TargetGroupArn=Ref(NlbTargetGroup)) ])) AppDNSRecord = template.add_resource( RecordSetType( "AppDNSRecord", DependsOn=["AppService"], HostedZoneName=Join("", [Ref(HostedZoneName), "."]), Name=Join("", [Ref(Application), ".", Ref(HostedZoneName), "."]), Type="CNAME", TTL="900", ResourceRecords=[GetAtt(NetworkLB, "DNSName")])) template.add_output( Output("URL", Description="DomainName", Value=Join("", ["https://", Ref(AppDNSRecord)]))) with open("ecs-ec2-service-cf.yaml", "w") as yamlout: yamlout.write(template.to_yaml())
ec2.Instance("Ec2Instance1", ImageId=FindInMap("RegionMap", Ref("AWS::Region"), "AMI"), InstanceType="t1.micro", KeyName="mcheriyath", SecurityGroups=["mcheriyath-sg"], Tags=Tags(**{ 'Name': 'DevOpsDenver', 'Owner': '*****@*****.**' }))) myDNSRecord = template.add_resource( RecordSetType( "myDNSRecord", HostedZoneName=Join("", ["example.com", "."]), Comment="DNS name for my instance.", Name=Join("", ["ops1", ".", "applicationname", ".", "example.com", "."]), Type="A", TTL="900", ResourceRecords=[GetAtt("Ec2Instance1", "PublicIp")], )) template.add_output([ Output( "InstanceId1", Description="InstanceId of the newly created EC2 instance", Value=Ref(ec2_instance_1), ), Output( "PublicIP1", Description="Public IP address of the newly created EC2 instance", Value=GetAtt(ec2_instance_1, "PublicIp"),