This repository has been archived by the owner on Jul 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaws_frederick_ecs.py
96 lines (84 loc) · 3.9 KB
/
aws_frederick_ecs.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
96
from aws_frederick_common import AWSFrederickCommonTemplate
from troposphere import Ref, GetAtt, Base64, Join, Output, Template
from troposphere.policies import UpdatePolicy, AutoScalingRollingUpdate
from troposphere import ecs
import troposphere.elasticloadbalancing as elb
import troposphere.constants as tpc
import troposphere.autoscaling as autoscaling
import troposphere.cloudwatch as cloudwatch
class AWSFrederickECSTemplate(AWSFrederickCommonTemplate):
"""
Enhances basic template by providing AWS Frederick ECS resources
"""
# Collect all the values we need to assemble our SuperBowlOnARoll stack
def __init__(self, env_name, region, cidr_range, aws_frederick_config):
super(AWSFrederickECSTemplate, self).__init__('AWSFrederickECS')
self.env_name = env_name
self.region = region
self.cidr_range = cidr_range
self.config = aws_frederick_config
def build_hook(self):
print "Building Template for AWS Frederick ECS"
hosted_zone_name = self.config.get('hosted_zone')
ecs_config = self.config.get('ecs')
if ecs_config is not None:
cluster = self.add_resource(ecs.Cluster('filesharefrederick',
ClusterName='filesharefrederick'))
for ecs in ecs_config:
self.add_ecs(
ecs.get('name'),
ecs.get('image'),
ecs.get('cpu'),
ecs.get('memory'),
ecs.get('container_port'),
ecs.get('alb_port'),
ecs.get('envvars'),
self.cidr_range,
hosted_zone_name
)
def add_ecs(self, name, image, cpu, memory, container_port, alb_port, envvars, cidr, hosted_zone):
"""
Helper method creates ingress given a source cidr range and a set of
ports
@param name [string] Name of the service
@param image [string] Docker image name
@param memory [int] Sets the memory size of the service
@param envvars [list] List of envvars
@param cidr [string] Range of addresses for this vpc
@param hosted_zone [string] Name of the hosted zone the elb will be
mapped to
"""
print "Creating ECS"
self.internal_security_group = self.add_sg_with_cidr_port_list(
"ASGSG",
"Security Group for ECS",
'vpcId',
cidr,
[{"80": "80"}]
)
self.public_lb_security_group = self.add_sg_with_cidr_port_list(
"ELBSG",
"Security Group for accessing ECS publicly",
'vpcId',
'0.0.0.0/0',
[{"443": "443"}]
)
container_def = ecs.ContainerDefinition(name + 'containerdef',
Name=name,
Image=image,
Cpu=cpu,
Memory=memory,
PortMappings=[ecs.PortMapping(
ContainerPort=container_port
)])
task_def = self.add_resource(ecs.TaskDefinition(name + 'taskdef',
Cpu=cpu,
Memory=memory,
RequiresCompatibilities=['FARGATE'],
NetworkMode='awsvpc',
ContainerDefinitions=[container_def]))
self.add_resource(ecs.Service(name + 'service',
Cluster=Ref(cluster),
LaunchType='FARGATE',
TaskDefinition=Ref(task_def),
DesiredCount=1))