forked from getlago/lago-pulumi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frontend.py
113 lines (102 loc) · 3.11 KB
/
frontend.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import json
from pulumi import ComponentResource, Output, ResourceOptions
from pulumi_aws import lb, ecs
class FrontendArgs:
def __init__(self,
lago_version=None,
cluster_arn=None,
role_arn=None,
vpc_id=None,
subnet_ids=None,
security_group_ids=None,
):
self.lago_version = lago_version
self.cluster_arn = cluster_arn
self.role_arn = role_arn
self.vpc_id = vpc_id
self.subnet_ids = subnet_ids
self.security_group_ids = security_group_ids
class Frontend(ComponentResource):
def __init__(self,
name: str,
args: FrontendArgs,
opts: ResourceOptions = None,
):
super().__init__('custom:resource:Frontend', name, {}, opts)
# Create a Load Balancer
self.alb = lb.LoadBalancer(f'{name}-alb',
security_groups=args.security_group_ids,
subnets=args.subnet_ids,
opts=ResourceOptions(parent=self),
)
# Create a Target Group
target_group = lb.TargetGroup(f'{name}-tg',
port=80,
protocol='HTTP',
target_type='ip',
vpc_id=args.vpc_id,
health_check=lb.TargetGroupHealthCheckArgs(
healthy_threshold=2,
interval=5,
timeout=4,
protocol='HTTP',
matcher='200-399',
),
opts=ResourceOptions(parent=self),
)
# Create a Listener
listener = lb.Listener(f'{name}-listener',
load_balancer_arn=self.alb.arn,
port=80,
default_actions=[lb.ListenerDefaultActionArgs(
type='forward',
target_group_arn=target_group.arn,
)],
opts=ResourceOptions(parent=self),
)
# Create the Frontend ECS Task Definition
task_name = f'{name}-task'
container_name = f'{name}-container'
self.task_definition = ecs.TaskDefinition(task_name,
family=task_name,
cpu='256',
memory='512',
network_mode='awsvpc',
requires_compatibilities=['FARGATE'],
execution_role_arn=args.role_arn,
container_definitions=Output.json_dumps([{
'name': container_name,
'image': f'getlago/front:v{args.lago_version}',
'portMappings': [{
'containerPort': 80,
'hostPort': 80,
'protocol': 'tcp',
}],
'environment': [
{
'name': 'APP_ENV',
'value': 'production',
},
],
}]),
opts=ResourceOptions(parent=self),
)
# Create the ECS Frontend Service
self.service = ecs.Service(f'{name}-svc',
cluster=args.cluster_arn,
desired_count=1,
launch_type='FARGATE',
task_definition=self.task_definition.arn,
network_configuration=ecs.ServiceNetworkConfigurationArgs(
assign_public_ip=True,
subnets=args.subnet_ids,
security_groups=args.security_group_ids,
),
load_balancers=[ecs.ServiceLoadBalancerArgs(
target_group_arn=target_group.arn,
container_name=container_name,
container_port=80,
)],
opts=ResourceOptions(depends_on=[listener], parent=self),
)
self.register_outputs({})