-
Notifications
You must be signed in to change notification settings - Fork 1
/
frontend.py
146 lines (134 loc) · 4.72 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import json
from pulumi import ComponentResource, Output, ResourceOptions
from pulumi_aws import lb, iam, ecs, cloudwatch, config, get_caller_identity
class FrontendArgs:
def __init__(
self,
lago_version=None,
cluster_arn=None,
role={},
vpc_id=None,
subnet_ids=None,
security_group_ids=None,
api_url=None,
alb_arn=None,
):
self.lago_version = lago_version
self.cluster_arn = cluster_arn
self.role = role
self.vpc_id = vpc_id
self.subnet_ids = subnet_ids
self.security_group_ids = security_group_ids
self.api_url = api_url
self.alb_arn = alb_arn
class Frontend(ComponentResource):
def __init__(
self,
name: str,
args: FrontendArgs,
opts: ResourceOptions = None,
):
super().__init__("custom:resource:Frontend", name, {}, opts)
# 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=args.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,
task_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",
}
],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-create-group": "true",
"awslogs-group": f"/ecs/{name}-task",
"awslogs-region": "eu-west-2",
"awslogs-stream-prefix": "ecs",
},
},
"environment": [
{
"name": "APP_ENV",
"value": "production",
},
{
"name": "API_URL",
"value": Output.concat("http://", args.api_url),
},
{
"name": "CODEGEN_API",
"value": Output.concat("http://", args.api_url),
},
],
}
]
),
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({})