forked from caktus/aws-web-stacks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstances.py
95 lines (87 loc) · 2.92 KB
/
instances.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from troposphere import AWS_STACK_NAME, Equals, Join, Ref, autoscaling
from .common import use_aes256_encryption
from .containers import (
container_instance_profile,
container_instance_type,
container_volume_size,
desired_container_instances,
max_container_instances
)
from .load_balancer import load_balancer, web_worker_health_check
from .security_groups import container_security_group
from .template import template
from .utils import ParameterWithDefaults as Parameter
from .vpc import private_subnet_a, private_subnet_b
ami = Ref(template.add_parameter(
Parameter(
"AMI",
Description="The Amazon Machine Image (AMI) to use for instances. Make "
"sure to use the correct AMI for your region and instance "
"type (t2 instances require HVM AMIs).",
Type="String",
Default="",
),
group="Application Server",
label="Amazon Machine Image (AMI)",
))
key_name = template.add_parameter(
Parameter(
"KeyName",
Description="Name of an existing EC2 KeyPair to enable SSH access to "
"the AWS EC2 instances",
Type="AWS::EC2::KeyPair::KeyName",
ConstraintDescription="must be the name of an existing EC2 KeyPair."
),
group="Application Server",
label="SSH Key Name",
)
tcp_health_check_condition = "TcpHealthCheck"
template.add_condition(
tcp_health_check_condition,
Equals(web_worker_health_check, ""),
)
instance_configuration_name = "LaunchConfiguration"
autoscaling_group_name = "AutoScalingGroup"
container_instance_configuration = autoscaling.LaunchConfiguration(
instance_configuration_name,
template=template,
SecurityGroups=[Ref(container_security_group)],
InstanceType=container_instance_type,
ImageId=ami,
IamInstanceProfile=Ref(container_instance_profile),
BlockDeviceMappings=[
autoscaling.BlockDeviceMapping(
DeviceName="/dev/sda1",
Ebs=autoscaling.EBSBlockDevice(
VolumeType="gp2",
VolumeSize=container_volume_size,
Encrypted=use_aes256_encryption,
)
),
],
KeyName=Ref(key_name),
)
autoscaling_group = autoscaling.AutoScalingGroup(
autoscaling_group_name,
template=template,
VPCZoneIdentifier=[Ref(private_subnet_a), Ref(private_subnet_b)],
MinSize=desired_container_instances,
MaxSize=max_container_instances,
DesiredCapacity=desired_container_instances,
LaunchConfigurationName=Ref(container_instance_configuration),
LoadBalancerNames=[Ref(load_balancer)],
HealthCheckType="EC2",
HealthCheckGracePeriod=300,
Tags=[
{
"Key": "Name",
"Value": Join("-", [Ref(AWS_STACK_NAME), "web_worker"]),
"PropagateAtLaunch": True,
},
{
"Key": "aws-web-stacks:role",
"Value": "worker",
"PropagateAtLaunch": True,
},
],
)