Пример #1
0
 def create_bastion_asg(self, sg):
     lc = asg.LaunchConfiguration('BastionLC',
                                  ImageId=tp.FindInMap(AMI_REGION_MAP_NAME, REF_REGION, 'BASTION'),
                                  InstanceType=self.bastion_instance_type,
                                  SecurityGroups=[tp.Ref(sg)],
                                  KeyName=tp.Ref(self.BASTION_KEY_PARM_NAME),
                                  InstanceMonitoring=False,
                                  AssociatePublicIpAddress=True,
                                  UserData=self._create_bastion_userdata())
     group = asg.AutoScalingGroup('BastionASG',
                                  MinSize=1, MaxSize=1,
                                  LaunchConfigurationName=tp.Ref(lc),
                                  VPCZoneIdentifier=self.public_subnet_ids,
                                  Tags=asgtag(self._rename('{} Bastion')))
     self.add_resources(group, lc)
     self.output(group)
     return group
Пример #2
0
 def create_server_asg(self, index, instance_sg, iam_profile, subnet_id, eni):
     lc_name = self._server_lc_name(index)
     lc = asg.LaunchConfiguration(
         lc_name,
         ImageId=tp.FindInMap(AMI_REGION_MAP_NAME, self.region, "GENERAL"),
         InstanceType=self.server_instance_type,
         SecurityGroups=[tp.Ref(instance_sg)],
         KeyName=tp.Ref(self.CONSUL_KEY_PARAM_NAME),
         IamInstanceProfile=tp.Ref(iam_profile),
         InstanceMonitoring=False,
         AssociatePublicIpAddress=False,
         UserData=self._create_server_userdata(eni, index),
     )
     group = asg.AutoScalingGroup(
         self._server_asg_name(index),
         MinSize=1,
         MaxSize=1,
         LaunchConfigurationName=tp.Ref(lc),
         VPCZoneIdentifier=[subnet_id],
         Tags=asgtag(self._rename("{} Server-" + str(index))),
     )
     self.add_resources(lc, group)
     self.output(group)
     return group
Пример #3
0
 def test_asg_tag(self):
     src = Tags(Application='ApplicationTag', Name='NameTag', Zip='Blah')
     result = asgtag(src)
     for t in result.tags:
         self.assertIsNotNone(t['PropagateAtLaunch'], msg=t['Key'])
Пример #4
0
 def create_ui_asg(self, instance_sg, iam_profile):
     lc = asg.LaunchConfiguration(
         "ConsulUILC",
         ImageId=tp.FindInMap(AMI_REGION_MAP_NAME, self.region, "GENERAL"),
         InstanceType=self.ui_instance_type,
         SecurityGroups=[tp.Ref(self.consul_sg), tp.Ref(instance_sg)],
         KeyName=tp.Ref(self.CONSUL_KEY_PARAM_NAME),
         IamInstanceProfile=tp.Ref(iam_profile),
         InstanceMonitoring=False,
         AssociatePublicIpAddress=True,  # TODO: Do we need this if we are behind an ELB?
         UserData=self._create_ui_userdata(),
     )
     group = asg.AutoScalingGroup(
         "ConsulUIASG",
         MinSize=1,
         MaxSize=2,
         LaunchConfigurationName=tp.Ref(lc),
         Cooldown=600,
         HealthCheckGracePeriod=600,
         HealthCheckType="EC2",  # TODO: switch to ELB
         TerminationPolicies=["OldestLaunchConfiguration", "OldestInstance"],
         # LoadBalancerNames=...  # TODO
         VPCZoneIdentifier=self.ui_subnet_ids,
         Tags=asgtag(self._rename("{} UI")),
     )
     scale_out = asg.ScalingPolicy(
         "ConsulUIScaleOutPolicy",
         AutoScalingGroupName=tp.Ref(group),
         AdjustmentType="ChangeInCapacity",
         Cooldown=600,
         PolicyType="SimpleScaling",
         ScalingAdjustment=1,
     )
     scale_in = asg.ScalingPolicy(
         "ConsulUIScaleInPolicy",
         AutoScalingGroupName=tp.Ref(group),
         AdjustmentType="ChangeInCapacity",
         Cooldown=600,
         PolicyType="SimpleScaling",
         ScalingAdjustment=-1,
     )
     # TODO: better metrics, like response time or something
     scale_out_alarm = cw.Alarm(
         "ConsulUIScaleOutAlarm",
         ActionsEnabled=True,
         AlarmActions=[tp.Ref(scale_out)],
         AlarmDescription="Scale out ConsulUIASG when instance CPU exceeds 50% for 15 minutes",
         ComparisonOperator="GreaterThanThreshold",
         Dimensions=[cw.MetricDimension(Name="AutoScalingGroupName", Value=tp.Ref(group))],
         EvaluationPeriods=3,
         MetricName="CPUUtilization",
         Namespace="AWS/EC2",
         Period=300,
         Statistic="Average",
         Threshold="50",
         Unit="Percent",
     )
     scale_in_alarm = cw.Alarm(
         "ConsulUIScaleInAlarm",
         ActionsEnabled=True,
         AlarmActions=[tp.Ref(scale_in)],
         AlarmDescription="Scale in ConsulUIASG when instance CPU < 25% for 15 minutes",
         ComparisonOperator="LessThanThreshold",
         Dimensions=[cw.MetricDimension(Name="AutoScalingGroupName", Value=tp.Ref(group))],
         EvaluationPeriods=3,
         MetricName="CPUUtilization",
         Namespace="AWS/EC2",
         Period=300,
         Statistic="Average",
         Threshold="25",
         Unit="Percent",
     )
     self.add_resources(lc, group, scale_out, scale_in, scale_out_alarm, scale_in_alarm)
     self.output(group)
     return group