@cubocicloide/stack-frappe 2.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +33 -0
- package/README.md +37 -0
- package/dist/commands/lint/checks/APP/001.d.ts +13 -0
- package/dist/commands/lint/checks/APP/001.js +73 -0
- package/dist/commands/lint/checks/APP/001.js.map +1 -0
- package/dist/commands/lint/checks/APP/002.d.ts +12 -0
- package/dist/commands/lint/checks/APP/002.js +94 -0
- package/dist/commands/lint/checks/APP/002.js.map +1 -0
- package/dist/commands/lint/checks/APP/003.d.ts +12 -0
- package/dist/commands/lint/checks/APP/003.js +94 -0
- package/dist/commands/lint/checks/APP/003.js.map +1 -0
- package/dist/commands/lint/checks/APP/004.d.ts +12 -0
- package/dist/commands/lint/checks/APP/004.js +117 -0
- package/dist/commands/lint/checks/APP/004.js.map +1 -0
- package/dist/commands/lint/checks/DT/001.d.ts +11 -0
- package/dist/commands/lint/checks/DT/001.js +57 -0
- package/dist/commands/lint/checks/DT/001.js.map +1 -0
- package/dist/commands/lint/checks/DT/002.d.ts +12 -0
- package/dist/commands/lint/checks/DT/002.js +74 -0
- package/dist/commands/lint/checks/DT/002.js.map +1 -0
- package/dist/commands/lint/checks/DT/003.d.ts +13 -0
- package/dist/commands/lint/checks/DT/003.js +96 -0
- package/dist/commands/lint/checks/DT/003.js.map +1 -0
- package/dist/commands/lint/checks/DT/004.d.ts +12 -0
- package/dist/commands/lint/checks/DT/004.js +101 -0
- package/dist/commands/lint/checks/DT/004.js.map +1 -0
- package/dist/commands/lint/checks/PY/001.d.ts +12 -0
- package/dist/commands/lint/checks/PY/001.js +87 -0
- package/dist/commands/lint/checks/PY/001.js.map +1 -0
- package/dist/commands/lint/checks/PY/002.d.ts +12 -0
- package/dist/commands/lint/checks/PY/002.js +80 -0
- package/dist/commands/lint/checks/PY/002.js.map +1 -0
- package/dist/commands/lint/checks/PY/003.d.ts +11 -0
- package/dist/commands/lint/checks/PY/003.js +56 -0
- package/dist/commands/lint/checks/PY/003.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +1987 -0
- package/dist/index.js.map +1 -0
- package/dude.stack.json +12 -0
- package/package.json +41 -0
- package/templates/aws-ecs/_dockerignore +11 -0
- package/templates/aws-ecs/docker/Dockerfile.prod.hbs +80 -0
- package/templates/aws-ecs/docker/nginx-entrypoint.sh +17 -0
- package/templates/aws-ecs/docker/nginx-template.conf +95 -0
- package/templates/aws-ecs/docs/docs/deploy.md.hbs +53 -0
- package/templates/aws-ecs/iac/README.md.hbs +113 -0
- package/templates/aws-ecs/iac/_gitignore +14 -0
- package/templates/aws-ecs/iac/runner/Dockerfile +35 -0
- package/templates/aws-ecs/iac/terraform/bootstrap/main.tf.hbs +93 -0
- package/templates/aws-ecs/iac/terraform/bootstrap/outputs.tf +14 -0
- package/templates/aws-ecs/iac/terraform/bootstrap/variables.tf.hbs +17 -0
- package/templates/aws-ecs/iac/terraform/environments/dev/backend.hcl.hbs +12 -0
- package/templates/aws-ecs/iac/terraform/environments/dev/terraform.tfvars.hbs +32 -0
- package/templates/aws-ecs/iac/terraform/main.tf.hbs +1205 -0
- package/templates/aws-ecs/iac/terraform/outputs.tf.hbs +103 -0
- package/templates/aws-ecs/iac/terraform/providers.tf +19 -0
- package/templates/aws-ecs/iac/terraform/variables.tf.hbs +139 -0
- package/templates/aws-ecs/iac/terraform/versions.tf +19 -0
- package/templates/base/.claude/rules/APP/001.md +26 -0
- package/templates/base/.claude/rules/APP/002.md +26 -0
- package/templates/base/.claude/rules/APP/003.md +25 -0
- package/templates/base/.claude/rules/APP/004.md +27 -0
- package/templates/base/.claude/rules/DT/001.md +25 -0
- package/templates/base/.claude/rules/DT/002.md +25 -0
- package/templates/base/.claude/rules/DT/003.md +24 -0
- package/templates/base/.claude/rules/DT/004.md +22 -0
- package/templates/base/.claude/rules/PY/001.md +30 -0
- package/templates/base/.claude/rules/PY/002.md +31 -0
- package/templates/base/.claude/rules/PY/003.md +29 -0
- package/templates/base/.claude/settings.json +42 -0
- package/templates/base/.claude/skills/create-doctype/SKILL.md +99 -0
- package/templates/base/.dude/commands/README.md +85 -0
- package/templates/base/.dude/commands/hello.ts +34 -0
- package/templates/base/.dude/lint/checks/PRJ/001.ts +26 -0
- package/templates/base/.dude/lint/checks/README.md +69 -0
- package/templates/base/.vscode/tasks.json +44 -0
- package/templates/base/CLAUDE.md.hbs +49 -0
- package/templates/base/README.md.hbs +68 -0
- package/templates/base/_env.example.hbs +5 -0
- package/templates/base/_env.hbs +12 -0
- package/templates/base/_gitignore +26 -0
- package/templates/base/_npmrc +2 -0
- package/templates/base/apps/README.md +24 -0
- package/templates/base/apps/ticketing/README.md +43 -0
- package/templates/base/apps/ticketing/pyproject.toml +34 -0
- package/templates/base/apps/ticketing/ticketing/__init__.py +1 -0
- package/templates/base/apps/ticketing/ticketing/api.py +35 -0
- package/templates/base/apps/ticketing/ticketing/events/__init__.py +0 -0
- package/templates/base/apps/ticketing/ticketing/events/hd_ticket.py +32 -0
- package/templates/base/apps/ticketing/ticketing/fixtures/workflow.json +55 -0
- package/templates/base/apps/ticketing/ticketing/fixtures/workflow_action_master.json +12 -0
- package/templates/base/apps/ticketing/ticketing/fixtures/workflow_state.json +20 -0
- package/templates/base/apps/ticketing/ticketing/hooks.py +54 -0
- package/templates/base/apps/ticketing/ticketing/modules.txt +1 -0
- package/templates/base/apps/ticketing/ticketing/patches/__init__.py +0 -0
- package/templates/base/apps/ticketing/ticketing/patches/v1_0/__init__.py +0 -0
- package/templates/base/apps/ticketing/ticketing/patches/v1_0/set_default_branding.py +26 -0
- package/templates/base/apps/ticketing/ticketing/patches.txt +5 -0
- package/templates/base/apps/ticketing/ticketing/public/images/favicon.png +0 -0
- package/templates/base/apps/ticketing/ticketing/public/images/logo.png +0 -0
- package/templates/base/apps/ticketing/ticketing/tasks.py +99 -0
- package/templates/base/apps/ticketing/ticketing/templates/__init__.py +0 -0
- package/templates/base/apps/ticketing/ticketing/templates/pages/__init__.py +0 -0
- package/templates/base/apps/ticketing/ticketing/templates/pages/ticket_stats.html +30 -0
- package/templates/base/apps/ticketing/ticketing/templates/pages/ticket_stats.py +14 -0
- package/templates/base/apps/ticketing/ticketing/ticketing/__init__.py +0 -0
- package/templates/base/apps/ticketing/ticketing/ticketing/doctype/__init__.py +0 -0
- package/templates/base/apps/ticketing/ticketing/ticketing/doctype/ticket_escalation_rule/__init__.py +0 -0
- package/templates/base/apps/ticketing/ticketing/ticketing/doctype/ticket_escalation_rule/test_ticket_escalation_rule.py +32 -0
- package/templates/base/apps/ticketing/ticketing/ticketing/doctype/ticket_escalation_rule/ticket_escalation_rule.js +23 -0
- package/templates/base/apps/ticketing/ticketing/ticketing/doctype/ticket_escalation_rule/ticket_escalation_rule.json +99 -0
- package/templates/base/apps/ticketing/ticketing/ticketing/doctype/ticket_escalation_rule/ticket_escalation_rule.py +18 -0
- package/templates/base/docker/init.sh +114 -0
- package/templates/base/docker-compose.yml.hbs +62 -0
- package/templates/base/docs/docs/api.md +21 -0
- package/templates/base/docs/docs/dude.md.hbs +220 -0
- package/templates/base/docs/docs/extending.md +238 -0
- package/templates/base/docs/docs/frappe.md.hbs +223 -0
- package/templates/base/docs/docs/index.md.hbs +101 -0
- package/templates/base/docs/docs/mkdocs.md +235 -0
- package/templates/base/docs/mkdocs.yml.hbs +54 -0
- package/templates/base/package.json.hbs +11 -0
|
@@ -0,0 +1,1205 @@
|
|
|
1
|
+
# ECS Fargate deployment for {{projectName}}: ONE production Frappe image (the
|
|
2
|
+
# frappe_docker layered pattern) run in five roles — `frontend` (nginx, the only
|
|
3
|
+
# ALB target), `backend` (gunicorn), `websocket` (socketio), `worker` (bench
|
|
4
|
+
# worker) and `scheduler` (bench schedule) — plus RDS MariaDB, ElastiCache
|
|
5
|
+
# Redis and an EFS file system holding the shared frappe `sites/` volume.
|
|
6
|
+
#
|
|
7
|
+
# Traffic flow: ALB → frontend (nginx) → backend/websocket via Cloud Map
|
|
8
|
+
# service discovery (`backend.<name>.internal:8000`, `websocket.<…>:9000`).
|
|
9
|
+
# The frappe nginx config proxies both HTTP and `/socket.io` itself, so only
|
|
10
|
+
# the frontend needs a target group.
|
|
11
|
+
#
|
|
12
|
+
# One-off Fargate task definitions (run by the CLI, never as services):
|
|
13
|
+
# configurator — writes common_site_config.json on the sites volume and
|
|
14
|
+
# refreshes the built assets (dude iac create-site re-runs it)
|
|
15
|
+
# create-site — bench new-site against RDS (first deploy only)
|
|
16
|
+
# migrate — bench --site all migrate (after every deploy)
|
|
17
|
+
#
|
|
18
|
+
# Why ECS (and not EKS): a Kubernetes control plane costs more per month than
|
|
19
|
+
# this whole workload. Fargate runs the containers serverlessly.
|
|
20
|
+
|
|
21
|
+
locals {
|
|
22
|
+
name = "${var.project_name}-${var.environment}"
|
|
23
|
+
frontend_port = 8080
|
|
24
|
+
backend_port = 8000
|
|
25
|
+
websocket_port = 9000
|
|
26
|
+
with_domain = var.domain_name != "" && var.route53_zone_name != ""
|
|
27
|
+
|
|
28
|
+
# The frappe site served by this environment. nginx forwards it as the Host
|
|
29
|
+
# header (FRAPPE_SITE_NAME_HEADER), so the site name is independent of the
|
|
30
|
+
# domain the ALB answers on.
|
|
31
|
+
site_name = var.site_name != "" ? var.site_name : var.project_name
|
|
32
|
+
|
|
33
|
+
app_host = local.with_domain ? var.domain_name : aws_lb.this.dns_name
|
|
34
|
+
app_url = local.with_domain ? "https://${var.domain_name}" : "http://${aws_lb.this.dns_name}"
|
|
35
|
+
|
|
36
|
+
# Private DNS namespace for frontend → backend/websocket traffic.
|
|
37
|
+
namespace = "${local.name}.internal"
|
|
38
|
+
backend_host = "backend.${local.name}.internal"
|
|
39
|
+
websocket_host = "websocket.${local.name}.internal"
|
|
40
|
+
|
|
41
|
+
bench_dir = "/home/frappe/frappe-bench"
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
data "aws_availability_zones" "available" {
|
|
45
|
+
state = "available"
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
# ── Network ──────────────────────────────────────────────────────────────────
|
|
49
|
+
# A minimal VPC with two public subnets. Tasks run in the public subnets with a
|
|
50
|
+
# public IP (for image pulls / egress) but their security groups only admit
|
|
51
|
+
# traffic from the ALB / each other — this avoids the ~$65/month a NAT gateway
|
|
52
|
+
# pair would cost a dev environment. Move the tasks to private subnets + NAT if
|
|
53
|
+
# your compliance posture requires it.
|
|
54
|
+
|
|
55
|
+
resource "aws_vpc" "this" {
|
|
56
|
+
cidr_block = "10.0.0.0/16"
|
|
57
|
+
enable_dns_support = true
|
|
58
|
+
enable_dns_hostnames = true
|
|
59
|
+
|
|
60
|
+
tags = { Name = local.name }
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
resource "aws_internet_gateway" "this" {
|
|
64
|
+
vpc_id = aws_vpc.this.id
|
|
65
|
+
|
|
66
|
+
tags = { Name = local.name }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
resource "aws_subnet" "public" {
|
|
70
|
+
count = 2
|
|
71
|
+
vpc_id = aws_vpc.this.id
|
|
72
|
+
cidr_block = cidrsubnet(aws_vpc.this.cidr_block, 8, count.index)
|
|
73
|
+
availability_zone = data.aws_availability_zones.available.names[count.index]
|
|
74
|
+
map_public_ip_on_launch = true
|
|
75
|
+
|
|
76
|
+
tags = { Name = "${local.name}-public-${count.index}" }
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
resource "aws_route_table" "public" {
|
|
80
|
+
vpc_id = aws_vpc.this.id
|
|
81
|
+
|
|
82
|
+
route {
|
|
83
|
+
cidr_block = "0.0.0.0/0"
|
|
84
|
+
gateway_id = aws_internet_gateway.this.id
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
tags = { Name = "${local.name}-public" }
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
resource "aws_route_table_association" "public" {
|
|
91
|
+
count = 2
|
|
92
|
+
subnet_id = aws_subnet.public[count.index].id
|
|
93
|
+
route_table_id = aws_route_table.public.id
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
# ── Security groups ──────────────────────────────────────────────────────────
|
|
97
|
+
|
|
98
|
+
resource "aws_security_group" "alb" {
|
|
99
|
+
name_prefix = "${local.name}-alb-"
|
|
100
|
+
description = "ALB - admits HTTP/HTTPS from allowed_cidrs"
|
|
101
|
+
vpc_id = aws_vpc.this.id
|
|
102
|
+
|
|
103
|
+
ingress {
|
|
104
|
+
description = "HTTP"
|
|
105
|
+
from_port = 80
|
|
106
|
+
to_port = 80
|
|
107
|
+
protocol = "tcp"
|
|
108
|
+
cidr_blocks = var.allowed_cidrs
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
ingress {
|
|
112
|
+
description = "HTTPS"
|
|
113
|
+
from_port = 443
|
|
114
|
+
to_port = 443
|
|
115
|
+
protocol = "tcp"
|
|
116
|
+
cidr_blocks = var.allowed_cidrs
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
egress {
|
|
120
|
+
from_port = 0
|
|
121
|
+
to_port = 0
|
|
122
|
+
protocol = "-1"
|
|
123
|
+
cidr_blocks = ["0.0.0.0/0"]
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
lifecycle {
|
|
127
|
+
create_before_destroy = true
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
# One security group for every Frappe task (frontend/backend/websocket/worker/
|
|
132
|
+
# scheduler and the one-off tasks): they all mount the same EFS volume and talk
|
|
133
|
+
# to the same database/redis, and the frontend proxies to backend + websocket
|
|
134
|
+
# inside the group (`self` ingress).
|
|
135
|
+
resource "aws_security_group" "app" {
|
|
136
|
+
name_prefix = "${local.name}-app-"
|
|
137
|
+
description = "Frappe tasks - nginx reachable from the ALB, everything else group-internal"
|
|
138
|
+
vpc_id = aws_vpc.this.id
|
|
139
|
+
|
|
140
|
+
ingress {
|
|
141
|
+
description = "nginx from the ALB"
|
|
142
|
+
from_port = local.frontend_port
|
|
143
|
+
to_port = local.frontend_port
|
|
144
|
+
protocol = "tcp"
|
|
145
|
+
security_groups = [aws_security_group.alb.id]
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
ingress {
|
|
149
|
+
description = "frontend to backend/websocket (service discovery)"
|
|
150
|
+
from_port = 0
|
|
151
|
+
to_port = 0
|
|
152
|
+
protocol = "-1"
|
|
153
|
+
self = true
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
egress {
|
|
157
|
+
from_port = 0
|
|
158
|
+
to_port = 0
|
|
159
|
+
protocol = "-1"
|
|
160
|
+
cidr_blocks = ["0.0.0.0/0"]
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
lifecycle {
|
|
164
|
+
create_before_destroy = true
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
# ── Registry & logs ──────────────────────────────────────────────────────────
|
|
169
|
+
# The single ECR repository is shared across environments and owned by the
|
|
170
|
+
# bootstrap config (one image, promoted dev → staging → prod by tag).
|
|
171
|
+
|
|
172
|
+
data "aws_ecr_repository" "app" {
|
|
173
|
+
name = "${var.project_name}-app"
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
resource "aws_cloudwatch_log_group" "service" {
|
|
177
|
+
for_each = toset(["frontend", "backend", "websocket", "worker", "scheduler"])
|
|
178
|
+
name = "/ecs/${local.name}-${each.key}"
|
|
179
|
+
retention_in_days = var.log_retention_days
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
# ── Database (RDS MariaDB) ───────────────────────────────────────────────────
|
|
183
|
+
# Frappe requires MariaDB. The master ("root") credentials are only used by the
|
|
184
|
+
# one-off create-site task (bench new-site creates the site's own db + user);
|
|
185
|
+
# the running services read the per-site credentials from common_site_config /
|
|
186
|
+
# site_config.json on the EFS volume.
|
|
187
|
+
|
|
188
|
+
resource "random_password" "db_root" {
|
|
189
|
+
length = 32
|
|
190
|
+
special = false
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
resource "aws_db_subnet_group" "this" {
|
|
194
|
+
name = local.name
|
|
195
|
+
subnet_ids = aws_subnet.public[*].id
|
|
196
|
+
|
|
197
|
+
tags = { Name = local.name }
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
resource "aws_security_group" "db" {
|
|
201
|
+
name_prefix = "${local.name}-db-"
|
|
202
|
+
description = "RDS MariaDB - only reachable from the Frappe tasks"
|
|
203
|
+
vpc_id = aws_vpc.this.id
|
|
204
|
+
|
|
205
|
+
ingress {
|
|
206
|
+
description = "MariaDB from the Frappe tasks"
|
|
207
|
+
from_port = 3306
|
|
208
|
+
to_port = 3306
|
|
209
|
+
protocol = "tcp"
|
|
210
|
+
security_groups = [aws_security_group.app.id]
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
egress {
|
|
214
|
+
from_port = 0
|
|
215
|
+
to_port = 0
|
|
216
|
+
protocol = "-1"
|
|
217
|
+
cidr_blocks = ["0.0.0.0/0"]
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
lifecycle {
|
|
221
|
+
create_before_destroy = true
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
resource "aws_db_instance" "this" {
|
|
226
|
+
identifier = "${local.name}-db"
|
|
227
|
+
engine = "mariadb"
|
|
228
|
+
engine_version = "10.6"
|
|
229
|
+
instance_class = var.db_instance_class
|
|
230
|
+
|
|
231
|
+
allocated_storage = var.db_allocated_storage
|
|
232
|
+
storage_type = "gp3"
|
|
233
|
+
|
|
234
|
+
# No initial database: bench new-site creates one per site. "root" is
|
|
235
|
+
# reserved on RDS, hence "admin" — bench receives it via --mariadb-root-username.
|
|
236
|
+
username = "admin"
|
|
237
|
+
password = random_password.db_root.result
|
|
238
|
+
|
|
239
|
+
# Frappe expects utf8mb4 with the classic collation.
|
|
240
|
+
parameter_group_name = aws_db_parameter_group.this.name
|
|
241
|
+
|
|
242
|
+
db_subnet_group_name = aws_db_subnet_group.this.name
|
|
243
|
+
vpc_security_group_ids = [aws_security_group.db.id]
|
|
244
|
+
publicly_accessible = false
|
|
245
|
+
multi_az = false
|
|
246
|
+
|
|
247
|
+
skip_final_snapshot = true
|
|
248
|
+
apply_immediately = true
|
|
249
|
+
|
|
250
|
+
tags = { Name = "${local.name}-db" }
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
resource "aws_db_parameter_group" "this" {
|
|
254
|
+
name_prefix = "${local.name}-mariadb-"
|
|
255
|
+
family = "mariadb10.6"
|
|
256
|
+
|
|
257
|
+
parameter {
|
|
258
|
+
name = "character_set_server"
|
|
259
|
+
value = "utf8mb4"
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
parameter {
|
|
263
|
+
name = "collation_server"
|
|
264
|
+
value = "utf8mb4_unicode_ci"
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
lifecycle {
|
|
268
|
+
create_before_destroy = true
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
# ── Redis (ElastiCache) ──────────────────────────────────────────────────────
|
|
273
|
+
# One node; frappe's two logical brokers use separate redis databases
|
|
274
|
+
# (cache → /0, queue + socketio → /1).
|
|
275
|
+
|
|
276
|
+
resource "aws_security_group" "redis" {
|
|
277
|
+
name_prefix = "${local.name}-redis-"
|
|
278
|
+
description = "ElastiCache Redis - only reachable from the Frappe tasks"
|
|
279
|
+
vpc_id = aws_vpc.this.id
|
|
280
|
+
|
|
281
|
+
ingress {
|
|
282
|
+
description = "Redis from the Frappe tasks"
|
|
283
|
+
from_port = 6379
|
|
284
|
+
to_port = 6379
|
|
285
|
+
protocol = "tcp"
|
|
286
|
+
security_groups = [aws_security_group.app.id]
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
egress {
|
|
290
|
+
from_port = 0
|
|
291
|
+
to_port = 0
|
|
292
|
+
protocol = "-1"
|
|
293
|
+
cidr_blocks = ["0.0.0.0/0"]
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
lifecycle {
|
|
297
|
+
create_before_destroy = true
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
resource "aws_elasticache_subnet_group" "redis" {
|
|
302
|
+
name = "${local.name}-redis"
|
|
303
|
+
subnet_ids = aws_subnet.public[*].id
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
resource "aws_elasticache_cluster" "redis" {
|
|
307
|
+
cluster_id = "${local.name}-redis"
|
|
308
|
+
engine = "redis"
|
|
309
|
+
node_type = var.redis_node_type
|
|
310
|
+
num_cache_nodes = 1
|
|
311
|
+
port = 6379
|
|
312
|
+
subnet_group_name = aws_elasticache_subnet_group.redis.name
|
|
313
|
+
security_group_ids = [aws_security_group.redis.id]
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
locals {
|
|
317
|
+
redis_host = aws_elasticache_cluster.redis.cache_nodes[0].address
|
|
318
|
+
redis_cache = "redis://${local.redis_host}:6379/0"
|
|
319
|
+
redis_queue = "redis://${local.redis_host}:6379/1"
|
|
320
|
+
redis_socketio = "redis://${local.redis_host}:6379/1"
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
# ── Sites volume (EFS) ───────────────────────────────────────────────────────
|
|
324
|
+
# The frappe `sites/` directory (site_config.json, uploaded files, assets) must
|
|
325
|
+
# be shared by every service and survive task churn — Fargate task storage is
|
|
326
|
+
# ephemeral. The access point pins ownership to the image's `frappe` user
|
|
327
|
+
# (uid/gid 1000).
|
|
328
|
+
|
|
329
|
+
resource "aws_efs_file_system" "sites" {
|
|
330
|
+
creation_token = "${local.name}-sites"
|
|
331
|
+
encrypted = true
|
|
332
|
+
|
|
333
|
+
tags = { Name = "${local.name}-sites" }
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
resource "aws_security_group" "efs" {
|
|
337
|
+
name_prefix = "${local.name}-efs-"
|
|
338
|
+
description = "EFS - only reachable from the Frappe tasks"
|
|
339
|
+
vpc_id = aws_vpc.this.id
|
|
340
|
+
|
|
341
|
+
ingress {
|
|
342
|
+
description = "NFS from the Frappe tasks"
|
|
343
|
+
from_port = 2049
|
|
344
|
+
to_port = 2049
|
|
345
|
+
protocol = "tcp"
|
|
346
|
+
security_groups = [aws_security_group.app.id]
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
egress {
|
|
350
|
+
from_port = 0
|
|
351
|
+
to_port = 0
|
|
352
|
+
protocol = "-1"
|
|
353
|
+
cidr_blocks = ["0.0.0.0/0"]
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
lifecycle {
|
|
357
|
+
create_before_destroy = true
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
resource "aws_efs_mount_target" "sites" {
|
|
362
|
+
count = 2
|
|
363
|
+
file_system_id = aws_efs_file_system.sites.id
|
|
364
|
+
subnet_id = aws_subnet.public[count.index].id
|
|
365
|
+
security_groups = [aws_security_group.efs.id]
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
resource "aws_efs_access_point" "sites" {
|
|
369
|
+
file_system_id = aws_efs_file_system.sites.id
|
|
370
|
+
|
|
371
|
+
posix_user {
|
|
372
|
+
uid = 1000
|
|
373
|
+
gid = 1000
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
root_directory {
|
|
377
|
+
path = "/sites"
|
|
378
|
+
|
|
379
|
+
creation_info {
|
|
380
|
+
owner_uid = 1000
|
|
381
|
+
owner_gid = 1000
|
|
382
|
+
permissions = "755"
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
tags = { Name = "${local.name}-sites" }
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
# ── Secrets ──────────────────────────────────────────────────────────────────
|
|
390
|
+
# The DB master password and the site's Administrator password live in Secrets
|
|
391
|
+
# Manager and are injected into the one-off create-site task via `secrets`.
|
|
392
|
+
# recovery_window_in_days = 0 so `dude iac destroy` + re-apply doesn't collide
|
|
393
|
+
# with a secret pending deletion.
|
|
394
|
+
|
|
395
|
+
resource "aws_secretsmanager_secret" "db_root_password" {
|
|
396
|
+
name = "${local.name}/db-root-password"
|
|
397
|
+
recovery_window_in_days = 0
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
resource "aws_secretsmanager_secret_version" "db_root_password" {
|
|
401
|
+
secret_id = aws_secretsmanager_secret.db_root_password.id
|
|
402
|
+
secret_string = random_password.db_root.result
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
resource "random_password" "admin" {
|
|
406
|
+
length = 24
|
|
407
|
+
special = false
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
resource "aws_secretsmanager_secret" "admin_password" {
|
|
411
|
+
name = "${local.name}/admin-password"
|
|
412
|
+
recovery_window_in_days = 0
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
resource "aws_secretsmanager_secret_version" "admin_password" {
|
|
416
|
+
secret_id = aws_secretsmanager_secret.admin_password.id
|
|
417
|
+
secret_string = random_password.admin.result
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
# ── IAM ──────────────────────────────────────────────────────────────────────
|
|
421
|
+
|
|
422
|
+
data "aws_iam_policy_document" "task_assume" {
|
|
423
|
+
statement {
|
|
424
|
+
actions = ["sts:AssumeRole"]
|
|
425
|
+
|
|
426
|
+
principals {
|
|
427
|
+
type = "Service"
|
|
428
|
+
identifiers = ["ecs-tasks.amazonaws.com"]
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
resource "aws_iam_role" "execution" {
|
|
434
|
+
name = "${local.name}-ecs-execution"
|
|
435
|
+
assume_role_policy = data.aws_iam_policy_document.task_assume.json
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
resource "aws_iam_role_policy_attachment" "execution" {
|
|
439
|
+
role = aws_iam_role.execution.name
|
|
440
|
+
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
# The execution role (not the task role) resolves `secrets` entries at task
|
|
444
|
+
# start, so it needs read access to exactly the two secrets injected above.
|
|
445
|
+
resource "aws_iam_role_policy" "execution_secrets" {
|
|
446
|
+
name = "read-app-secrets"
|
|
447
|
+
role = aws_iam_role.execution.id
|
|
448
|
+
|
|
449
|
+
policy = jsonencode({
|
|
450
|
+
Version = "2012-10-17"
|
|
451
|
+
Statement = [{
|
|
452
|
+
Effect = "Allow"
|
|
453
|
+
Action = ["secretsmanager:GetSecretValue"]
|
|
454
|
+
Resource = [
|
|
455
|
+
aws_secretsmanager_secret.db_root_password.arn,
|
|
456
|
+
aws_secretsmanager_secret.admin_password.arn,
|
|
457
|
+
]
|
|
458
|
+
}]
|
|
459
|
+
})
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
resource "aws_iam_role" "task" {
|
|
463
|
+
name = "${local.name}-ecs-task"
|
|
464
|
+
assume_role_policy = data.aws_iam_policy_document.task_assume.json
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
# ECS Exec — lets `aws ecs execute-command` open a shell in a running task.
|
|
468
|
+
resource "aws_iam_role_policy" "task_ecs_exec" {
|
|
469
|
+
name = "ecs-exec"
|
|
470
|
+
role = aws_iam_role.task.id
|
|
471
|
+
|
|
472
|
+
policy = jsonencode({
|
|
473
|
+
Version = "2012-10-17"
|
|
474
|
+
Statement = [{
|
|
475
|
+
Effect = "Allow"
|
|
476
|
+
Action = [
|
|
477
|
+
"ssmmessages:CreateControlChannel",
|
|
478
|
+
"ssmmessages:CreateDataChannel",
|
|
479
|
+
"ssmmessages:OpenControlChannel",
|
|
480
|
+
"ssmmessages:OpenDataChannel",
|
|
481
|
+
]
|
|
482
|
+
Resource = "*"
|
|
483
|
+
}]
|
|
484
|
+
})
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
# ── Load balancer ────────────────────────────────────────────────────────────
|
|
488
|
+
# Only the frontend (nginx) is a target: the frappe nginx config proxies API,
|
|
489
|
+
# websocket and asset traffic itself.
|
|
490
|
+
|
|
491
|
+
resource "aws_lb" "this" {
|
|
492
|
+
name = local.name
|
|
493
|
+
load_balancer_type = "application"
|
|
494
|
+
security_groups = [aws_security_group.alb.id]
|
|
495
|
+
subnets = aws_subnet.public[*].id
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
resource "aws_lb_target_group" "frontend" {
|
|
499
|
+
name = "${local.name}-fe"
|
|
500
|
+
port = local.frontend_port
|
|
501
|
+
protocol = "HTTP"
|
|
502
|
+
target_type = "ip"
|
|
503
|
+
vpc_id = aws_vpc.this.id
|
|
504
|
+
|
|
505
|
+
# `/api/method/ping` answers 200 once the site exists. Before the first
|
|
506
|
+
# `dude iac create-site` there is no site yet, so anything below a 5xx is
|
|
507
|
+
# accepted — “nginx and the backend are reachable” is what this probes.
|
|
508
|
+
health_check {
|
|
509
|
+
path = "/api/method/ping"
|
|
510
|
+
matcher = "200-499"
|
|
511
|
+
interval = 30
|
|
512
|
+
timeout = 5
|
|
513
|
+
healthy_threshold = 2
|
|
514
|
+
unhealthy_threshold = 3
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
deregistration_delay = 30
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
# Plain HTTP when no domain is configured…
|
|
521
|
+
resource "aws_lb_listener" "http" {
|
|
522
|
+
count = local.with_domain ? 0 : 1
|
|
523
|
+
load_balancer_arn = aws_lb.this.arn
|
|
524
|
+
port = 80
|
|
525
|
+
protocol = "HTTP"
|
|
526
|
+
|
|
527
|
+
default_action {
|
|
528
|
+
type = "forward"
|
|
529
|
+
target_group_arn = aws_lb_target_group.frontend.arn
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
# …or HTTPS with an ACM certificate + HTTP→HTTPS redirect when one is.
|
|
534
|
+
data "aws_route53_zone" "this" {
|
|
535
|
+
count = local.with_domain ? 1 : 0
|
|
536
|
+
name = var.route53_zone_name
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
resource "aws_acm_certificate" "this" {
|
|
540
|
+
count = local.with_domain ? 1 : 0
|
|
541
|
+
domain_name = var.domain_name
|
|
542
|
+
validation_method = "DNS"
|
|
543
|
+
|
|
544
|
+
lifecycle {
|
|
545
|
+
create_before_destroy = true
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
resource "aws_route53_record" "cert_validation" {
|
|
550
|
+
for_each = local.with_domain ? {
|
|
551
|
+
for dvo in aws_acm_certificate.this[0].domain_validation_options : dvo.domain_name => {
|
|
552
|
+
name = dvo.resource_record_name
|
|
553
|
+
record = dvo.resource_record_value
|
|
554
|
+
type = dvo.resource_record_type
|
|
555
|
+
}
|
|
556
|
+
} : {}
|
|
557
|
+
|
|
558
|
+
zone_id = data.aws_route53_zone.this[0].zone_id
|
|
559
|
+
name = each.value.name
|
|
560
|
+
type = each.value.type
|
|
561
|
+
ttl = 60
|
|
562
|
+
records = [each.value.record]
|
|
563
|
+
allow_overwrite = true
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
resource "aws_acm_certificate_validation" "this" {
|
|
567
|
+
count = local.with_domain ? 1 : 0
|
|
568
|
+
certificate_arn = aws_acm_certificate.this[0].arn
|
|
569
|
+
validation_record_fqdns = [for r in aws_route53_record.cert_validation : r.fqdn]
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
resource "aws_lb_listener" "https" {
|
|
573
|
+
count = local.with_domain ? 1 : 0
|
|
574
|
+
load_balancer_arn = aws_lb.this.arn
|
|
575
|
+
port = 443
|
|
576
|
+
protocol = "HTTPS"
|
|
577
|
+
ssl_policy = "ELBSecurityPolicy-TLS13-1-2-2021-06"
|
|
578
|
+
certificate_arn = aws_acm_certificate_validation.this[0].certificate_arn
|
|
579
|
+
|
|
580
|
+
default_action {
|
|
581
|
+
type = "forward"
|
|
582
|
+
target_group_arn = aws_lb_target_group.frontend.arn
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
resource "aws_lb_listener" "http_redirect" {
|
|
587
|
+
count = local.with_domain ? 1 : 0
|
|
588
|
+
load_balancer_arn = aws_lb.this.arn
|
|
589
|
+
port = 80
|
|
590
|
+
protocol = "HTTP"
|
|
591
|
+
|
|
592
|
+
default_action {
|
|
593
|
+
type = "redirect"
|
|
594
|
+
|
|
595
|
+
redirect {
|
|
596
|
+
port = "443"
|
|
597
|
+
protocol = "HTTPS"
|
|
598
|
+
status_code = "HTTP_301"
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
resource "aws_route53_record" "app" {
|
|
604
|
+
count = local.with_domain ? 1 : 0
|
|
605
|
+
zone_id = data.aws_route53_zone.this[0].zone_id
|
|
606
|
+
name = var.domain_name
|
|
607
|
+
type = "A"
|
|
608
|
+
|
|
609
|
+
alias {
|
|
610
|
+
name = aws_lb.this.dns_name
|
|
611
|
+
zone_id = aws_lb.this.zone_id
|
|
612
|
+
evaluate_target_health = true
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
# ── Service discovery (Cloud Map) ────────────────────────────────────────────
|
|
617
|
+
# Private DNS so the frontend's nginx can reach gunicorn and socketio by name.
|
|
618
|
+
|
|
619
|
+
resource "aws_service_discovery_private_dns_namespace" "this" {
|
|
620
|
+
name = local.namespace
|
|
621
|
+
vpc = aws_vpc.this.id
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
resource "aws_service_discovery_service" "backend" {
|
|
625
|
+
name = "backend"
|
|
626
|
+
|
|
627
|
+
dns_config {
|
|
628
|
+
namespace_id = aws_service_discovery_private_dns_namespace.this.id
|
|
629
|
+
routing_policy = "MULTIVALUE"
|
|
630
|
+
|
|
631
|
+
dns_records {
|
|
632
|
+
ttl = 10
|
|
633
|
+
type = "A"
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
health_check_custom_config {
|
|
638
|
+
failure_threshold = 1
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
resource "aws_service_discovery_service" "websocket" {
|
|
643
|
+
name = "websocket"
|
|
644
|
+
|
|
645
|
+
dns_config {
|
|
646
|
+
namespace_id = aws_service_discovery_private_dns_namespace.this.id
|
|
647
|
+
routing_policy = "MULTIVALUE"
|
|
648
|
+
|
|
649
|
+
dns_records {
|
|
650
|
+
ttl = 10
|
|
651
|
+
type = "A"
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
health_check_custom_config {
|
|
656
|
+
failure_threshold = 1
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
# ── ECS (Fargate) ────────────────────────────────────────────────────────────
|
|
661
|
+
|
|
662
|
+
resource "aws_ecs_cluster" "this" {
|
|
663
|
+
name = local.name
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
locals {
|
|
667
|
+
image = "${data.aws_ecr_repository.app.repository_url}:${var.image_tag}"
|
|
668
|
+
|
|
669
|
+
# The shared EFS `sites/` volume, mounted into every container.
|
|
670
|
+
sites_volume = {
|
|
671
|
+
name = "sites"
|
|
672
|
+
efs_volume_configuration = {
|
|
673
|
+
file_system_id = aws_efs_file_system.sites.id
|
|
674
|
+
transit_encryption = "ENABLED"
|
|
675
|
+
authorization_config = {
|
|
676
|
+
access_point_id = aws_efs_access_point.sites.id
|
|
677
|
+
iam = "DISABLED"
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
sites_mount = [
|
|
683
|
+
{
|
|
684
|
+
sourceVolume = "sites"
|
|
685
|
+
containerPath = "${local.bench_dir}/sites"
|
|
686
|
+
readOnly = false
|
|
687
|
+
}
|
|
688
|
+
]
|
|
689
|
+
|
|
690
|
+
log_configuration = {
|
|
691
|
+
for svc in ["frontend", "backend", "websocket", "worker", "scheduler"] :
|
|
692
|
+
svc => {
|
|
693
|
+
logDriver = "awslogs"
|
|
694
|
+
options = {
|
|
695
|
+
awslogs-group = aws_cloudwatch_log_group.service[svc].name
|
|
696
|
+
awslogs-region = var.region
|
|
697
|
+
awslogs-stream-prefix = svc
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
# Every service task definition shares this shape; only the command, port and
|
|
704
|
+
# environment differ.
|
|
705
|
+
resource "aws_ecs_task_definition" "frontend" {
|
|
706
|
+
family = "${local.name}-frontend"
|
|
707
|
+
requires_compatibilities = ["FARGATE"]
|
|
708
|
+
network_mode = "awsvpc"
|
|
709
|
+
cpu = var.frontend_cpu
|
|
710
|
+
memory = var.frontend_memory
|
|
711
|
+
execution_role_arn = aws_iam_role.execution.arn
|
|
712
|
+
task_role_arn = aws_iam_role.task.arn
|
|
713
|
+
|
|
714
|
+
volume {
|
|
715
|
+
name = local.sites_volume.name
|
|
716
|
+
|
|
717
|
+
efs_volume_configuration {
|
|
718
|
+
file_system_id = local.sites_volume.efs_volume_configuration.file_system_id
|
|
719
|
+
transit_encryption = local.sites_volume.efs_volume_configuration.transit_encryption
|
|
720
|
+
|
|
721
|
+
authorization_config {
|
|
722
|
+
access_point_id = local.sites_volume.efs_volume_configuration.authorization_config.access_point_id
|
|
723
|
+
iam = local.sites_volume.efs_volume_configuration.authorization_config.iam
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
container_definitions = jsonencode([
|
|
729
|
+
{
|
|
730
|
+
name = "frontend"
|
|
731
|
+
image = local.image
|
|
732
|
+
essential = true
|
|
733
|
+
command = ["nginx-entrypoint.sh"]
|
|
734
|
+
|
|
735
|
+
portMappings = [
|
|
736
|
+
{ containerPort = local.frontend_port, protocol = "tcp" }
|
|
737
|
+
]
|
|
738
|
+
|
|
739
|
+
environment = [
|
|
740
|
+
{ name = "BACKEND", value = "${local.backend_host}:${local.backend_port}" },
|
|
741
|
+
{ name = "SOCKETIO", value = "${local.websocket_host}:${local.websocket_port}" },
|
|
742
|
+
{ name = "FRAPPE_SITE_NAME_HEADER", value = local.site_name },
|
|
743
|
+
{ name = "UPSTREAM_REAL_IP_ADDRESS", value = "10.0.0.0/16" },
|
|
744
|
+
{ name = "UPSTREAM_REAL_IP_HEADER", value = "X-Forwarded-For" },
|
|
745
|
+
{ name = "UPSTREAM_REAL_IP_RECURSIVE", value = "on" },
|
|
746
|
+
]
|
|
747
|
+
|
|
748
|
+
mountPoints = local.sites_mount
|
|
749
|
+
logConfiguration = local.log_configuration["frontend"]
|
|
750
|
+
}
|
|
751
|
+
])
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
resource "aws_ecs_service" "frontend" {
|
|
755
|
+
name = "${local.name}-frontend"
|
|
756
|
+
cluster = aws_ecs_cluster.this.id
|
|
757
|
+
task_definition = aws_ecs_task_definition.frontend.arn
|
|
758
|
+
desired_count = var.frontend_desired_count
|
|
759
|
+
launch_type = "FARGATE"
|
|
760
|
+
|
|
761
|
+
enable_execute_command = true
|
|
762
|
+
|
|
763
|
+
network_configuration {
|
|
764
|
+
subnets = aws_subnet.public[*].id
|
|
765
|
+
security_groups = [aws_security_group.app.id]
|
|
766
|
+
assign_public_ip = true
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
load_balancer {
|
|
770
|
+
target_group_arn = aws_lb_target_group.frontend.arn
|
|
771
|
+
container_name = "frontend"
|
|
772
|
+
container_port = local.frontend_port
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
health_check_grace_period_seconds = 120
|
|
776
|
+
|
|
777
|
+
deployment_circuit_breaker {
|
|
778
|
+
enable = true
|
|
779
|
+
rollback = true
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
depends_on = [aws_lb_listener.http, aws_lb_listener.https]
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
resource "aws_ecs_task_definition" "backend" {
|
|
786
|
+
family = "${local.name}-backend"
|
|
787
|
+
requires_compatibilities = ["FARGATE"]
|
|
788
|
+
network_mode = "awsvpc"
|
|
789
|
+
cpu = var.backend_cpu
|
|
790
|
+
memory = var.backend_memory
|
|
791
|
+
execution_role_arn = aws_iam_role.execution.arn
|
|
792
|
+
task_role_arn = aws_iam_role.task.arn
|
|
793
|
+
|
|
794
|
+
volume {
|
|
795
|
+
name = local.sites_volume.name
|
|
796
|
+
|
|
797
|
+
efs_volume_configuration {
|
|
798
|
+
file_system_id = local.sites_volume.efs_volume_configuration.file_system_id
|
|
799
|
+
transit_encryption = local.sites_volume.efs_volume_configuration.transit_encryption
|
|
800
|
+
|
|
801
|
+
authorization_config {
|
|
802
|
+
access_point_id = local.sites_volume.efs_volume_configuration.authorization_config.access_point_id
|
|
803
|
+
iam = local.sites_volume.efs_volume_configuration.authorization_config.iam
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
container_definitions = jsonencode([
|
|
809
|
+
{
|
|
810
|
+
name = "backend"
|
|
811
|
+
image = local.image
|
|
812
|
+
essential = true
|
|
813
|
+
# No command: the image's CMD is the gunicorn server (see docker/Dockerfile.prod).
|
|
814
|
+
|
|
815
|
+
portMappings = [
|
|
816
|
+
{ containerPort = local.backend_port, protocol = "tcp" }
|
|
817
|
+
]
|
|
818
|
+
|
|
819
|
+
mountPoints = local.sites_mount
|
|
820
|
+
logConfiguration = local.log_configuration["backend"]
|
|
821
|
+
}
|
|
822
|
+
])
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
resource "aws_ecs_service" "backend" {
|
|
826
|
+
name = "${local.name}-backend"
|
|
827
|
+
cluster = aws_ecs_cluster.this.id
|
|
828
|
+
task_definition = aws_ecs_task_definition.backend.arn
|
|
829
|
+
desired_count = var.backend_desired_count
|
|
830
|
+
launch_type = "FARGATE"
|
|
831
|
+
|
|
832
|
+
enable_execute_command = true
|
|
833
|
+
|
|
834
|
+
network_configuration {
|
|
835
|
+
subnets = aws_subnet.public[*].id
|
|
836
|
+
security_groups = [aws_security_group.app.id]
|
|
837
|
+
assign_public_ip = true
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
service_registries {
|
|
841
|
+
registry_arn = aws_service_discovery_service.backend.arn
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
deployment_circuit_breaker {
|
|
845
|
+
enable = true
|
|
846
|
+
rollback = true
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
resource "aws_ecs_task_definition" "websocket" {
|
|
851
|
+
family = "${local.name}-websocket"
|
|
852
|
+
requires_compatibilities = ["FARGATE"]
|
|
853
|
+
network_mode = "awsvpc"
|
|
854
|
+
cpu = var.websocket_cpu
|
|
855
|
+
memory = var.websocket_memory
|
|
856
|
+
execution_role_arn = aws_iam_role.execution.arn
|
|
857
|
+
task_role_arn = aws_iam_role.task.arn
|
|
858
|
+
|
|
859
|
+
volume {
|
|
860
|
+
name = local.sites_volume.name
|
|
861
|
+
|
|
862
|
+
efs_volume_configuration {
|
|
863
|
+
file_system_id = local.sites_volume.efs_volume_configuration.file_system_id
|
|
864
|
+
transit_encryption = local.sites_volume.efs_volume_configuration.transit_encryption
|
|
865
|
+
|
|
866
|
+
authorization_config {
|
|
867
|
+
access_point_id = local.sites_volume.efs_volume_configuration.authorization_config.access_point_id
|
|
868
|
+
iam = local.sites_volume.efs_volume_configuration.authorization_config.iam
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
container_definitions = jsonencode([
|
|
874
|
+
{
|
|
875
|
+
name = "websocket"
|
|
876
|
+
image = local.image
|
|
877
|
+
essential = true
|
|
878
|
+
command = ["node", "${local.bench_dir}/apps/frappe/socketio.js"]
|
|
879
|
+
|
|
880
|
+
portMappings = [
|
|
881
|
+
{ containerPort = local.websocket_port, protocol = "tcp" }
|
|
882
|
+
]
|
|
883
|
+
|
|
884
|
+
mountPoints = local.sites_mount
|
|
885
|
+
logConfiguration = local.log_configuration["websocket"]
|
|
886
|
+
}
|
|
887
|
+
])
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
resource "aws_ecs_service" "websocket" {
|
|
891
|
+
name = "${local.name}-websocket"
|
|
892
|
+
cluster = aws_ecs_cluster.this.id
|
|
893
|
+
task_definition = aws_ecs_task_definition.websocket.arn
|
|
894
|
+
desired_count = 1
|
|
895
|
+
launch_type = "FARGATE"
|
|
896
|
+
|
|
897
|
+
enable_execute_command = true
|
|
898
|
+
|
|
899
|
+
network_configuration {
|
|
900
|
+
subnets = aws_subnet.public[*].id
|
|
901
|
+
security_groups = [aws_security_group.app.id]
|
|
902
|
+
assign_public_ip = true
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
service_registries {
|
|
906
|
+
registry_arn = aws_service_discovery_service.websocket.arn
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
deployment_circuit_breaker {
|
|
910
|
+
enable = true
|
|
911
|
+
rollback = true
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
# Queue worker — one service consuming all three queues. Split it into
|
|
916
|
+
# per-queue services (short/default/long) when workload demands it.
|
|
917
|
+
resource "aws_ecs_task_definition" "worker" {
|
|
918
|
+
family = "${local.name}-worker"
|
|
919
|
+
requires_compatibilities = ["FARGATE"]
|
|
920
|
+
network_mode = "awsvpc"
|
|
921
|
+
cpu = var.worker_cpu
|
|
922
|
+
memory = var.worker_memory
|
|
923
|
+
execution_role_arn = aws_iam_role.execution.arn
|
|
924
|
+
task_role_arn = aws_iam_role.task.arn
|
|
925
|
+
|
|
926
|
+
volume {
|
|
927
|
+
name = local.sites_volume.name
|
|
928
|
+
|
|
929
|
+
efs_volume_configuration {
|
|
930
|
+
file_system_id = local.sites_volume.efs_volume_configuration.file_system_id
|
|
931
|
+
transit_encryption = local.sites_volume.efs_volume_configuration.transit_encryption
|
|
932
|
+
|
|
933
|
+
authorization_config {
|
|
934
|
+
access_point_id = local.sites_volume.efs_volume_configuration.authorization_config.access_point_id
|
|
935
|
+
iam = local.sites_volume.efs_volume_configuration.authorization_config.iam
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
container_definitions = jsonencode([
|
|
941
|
+
{
|
|
942
|
+
name = "worker"
|
|
943
|
+
image = local.image
|
|
944
|
+
essential = true
|
|
945
|
+
command = ["bench", "worker", "--queue", "short,default,long"]
|
|
946
|
+
|
|
947
|
+
mountPoints = local.sites_mount
|
|
948
|
+
logConfiguration = local.log_configuration["worker"]
|
|
949
|
+
}
|
|
950
|
+
])
|
|
951
|
+
}
|
|
952
|
+
|
|
953
|
+
resource "aws_ecs_service" "worker" {
|
|
954
|
+
name = "${local.name}-worker"
|
|
955
|
+
cluster = aws_ecs_cluster.this.id
|
|
956
|
+
task_definition = aws_ecs_task_definition.worker.arn
|
|
957
|
+
desired_count = var.worker_desired_count
|
|
958
|
+
launch_type = "FARGATE"
|
|
959
|
+
|
|
960
|
+
enable_execute_command = true
|
|
961
|
+
|
|
962
|
+
network_configuration {
|
|
963
|
+
subnets = aws_subnet.public[*].id
|
|
964
|
+
security_groups = [aws_security_group.app.id]
|
|
965
|
+
assign_public_ip = true
|
|
966
|
+
}
|
|
967
|
+
|
|
968
|
+
deployment_circuit_breaker {
|
|
969
|
+
enable = true
|
|
970
|
+
rollback = true
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
# Scheduler — exactly one task: it must be a singleton or every scheduled job
|
|
975
|
+
# fires once per replica.
|
|
976
|
+
resource "aws_ecs_task_definition" "scheduler" {
|
|
977
|
+
family = "${local.name}-scheduler"
|
|
978
|
+
requires_compatibilities = ["FARGATE"]
|
|
979
|
+
network_mode = "awsvpc"
|
|
980
|
+
cpu = 256
|
|
981
|
+
memory = 512
|
|
982
|
+
execution_role_arn = aws_iam_role.execution.arn
|
|
983
|
+
task_role_arn = aws_iam_role.task.arn
|
|
984
|
+
|
|
985
|
+
volume {
|
|
986
|
+
name = local.sites_volume.name
|
|
987
|
+
|
|
988
|
+
efs_volume_configuration {
|
|
989
|
+
file_system_id = local.sites_volume.efs_volume_configuration.file_system_id
|
|
990
|
+
transit_encryption = local.sites_volume.efs_volume_configuration.transit_encryption
|
|
991
|
+
|
|
992
|
+
authorization_config {
|
|
993
|
+
access_point_id = local.sites_volume.efs_volume_configuration.authorization_config.access_point_id
|
|
994
|
+
iam = local.sites_volume.efs_volume_configuration.authorization_config.iam
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
container_definitions = jsonencode([
|
|
1000
|
+
{
|
|
1001
|
+
name = "scheduler"
|
|
1002
|
+
image = local.image
|
|
1003
|
+
essential = true
|
|
1004
|
+
command = ["bench", "schedule"]
|
|
1005
|
+
|
|
1006
|
+
mountPoints = local.sites_mount
|
|
1007
|
+
logConfiguration = local.log_configuration["scheduler"]
|
|
1008
|
+
}
|
|
1009
|
+
])
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
resource "aws_ecs_service" "scheduler" {
|
|
1013
|
+
name = "${local.name}-scheduler"
|
|
1014
|
+
cluster = aws_ecs_cluster.this.id
|
|
1015
|
+
task_definition = aws_ecs_task_definition.scheduler.arn
|
|
1016
|
+
desired_count = 1
|
|
1017
|
+
launch_type = "FARGATE"
|
|
1018
|
+
|
|
1019
|
+
enable_execute_command = true
|
|
1020
|
+
|
|
1021
|
+
network_configuration {
|
|
1022
|
+
subnets = aws_subnet.public[*].id
|
|
1023
|
+
security_groups = [aws_security_group.app.id]
|
|
1024
|
+
assign_public_ip = true
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
deployment_circuit_breaker {
|
|
1028
|
+
enable = true
|
|
1029
|
+
rollback = true
|
|
1030
|
+
}
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1033
|
+
# ── One-off task definitions ─────────────────────────────────────────────────
|
|
1034
|
+
# Run by the CLI with `aws ecs run-task` — never as long-lived services.
|
|
1035
|
+
|
|
1036
|
+
# configurator: points the bench at RDS/ElastiCache by writing
|
|
1037
|
+
# common_site_config.json on the sites volume, and refreshes the built assets
|
|
1038
|
+
# from the copy baked into the image (sites/ shadows the image's own assets).
|
|
1039
|
+
resource "aws_ecs_task_definition" "configurator" {
|
|
1040
|
+
family = "${local.name}-configurator"
|
|
1041
|
+
requires_compatibilities = ["FARGATE"]
|
|
1042
|
+
network_mode = "awsvpc"
|
|
1043
|
+
cpu = 256
|
|
1044
|
+
memory = 512
|
|
1045
|
+
execution_role_arn = aws_iam_role.execution.arn
|
|
1046
|
+
task_role_arn = aws_iam_role.task.arn
|
|
1047
|
+
|
|
1048
|
+
volume {
|
|
1049
|
+
name = local.sites_volume.name
|
|
1050
|
+
|
|
1051
|
+
efs_volume_configuration {
|
|
1052
|
+
file_system_id = local.sites_volume.efs_volume_configuration.file_system_id
|
|
1053
|
+
transit_encryption = local.sites_volume.efs_volume_configuration.transit_encryption
|
|
1054
|
+
|
|
1055
|
+
authorization_config {
|
|
1056
|
+
access_point_id = local.sites_volume.efs_volume_configuration.authorization_config.access_point_id
|
|
1057
|
+
iam = local.sites_volume.efs_volume_configuration.authorization_config.iam
|
|
1058
|
+
}
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
1061
|
+
|
|
1062
|
+
container_definitions = jsonencode([
|
|
1063
|
+
{
|
|
1064
|
+
name = "configurator"
|
|
1065
|
+
image = local.image
|
|
1066
|
+
essential = true
|
|
1067
|
+
command = ["bash", "-c", join("; ", [
|
|
1068
|
+
"set -e",
|
|
1069
|
+
"ls -1 apps > sites/apps.txt",
|
|
1070
|
+
"test -f sites/common_site_config.json || echo '{}' > sites/common_site_config.json",
|
|
1071
|
+
"bench set-config -g db_host \"$DB_HOST\"",
|
|
1072
|
+
"bench set-config -gp db_port 3306",
|
|
1073
|
+
"bench set-config -g redis_cache \"$REDIS_CACHE\"",
|
|
1074
|
+
"bench set-config -g redis_queue \"$REDIS_QUEUE\"",
|
|
1075
|
+
"bench set-config -g redis_socketio \"$REDIS_SOCKETIO\"",
|
|
1076
|
+
"bench set-config -gp socketio_port ${local.websocket_port}",
|
|
1077
|
+
"mkdir -p sites/assets",
|
|
1078
|
+
"cp -rf ${local.bench_dir}/assets-baked/. sites/assets/ 2>/dev/null || true",
|
|
1079
|
+
"echo configurator done",
|
|
1080
|
+
])]
|
|
1081
|
+
|
|
1082
|
+
environment = [
|
|
1083
|
+
{ name = "DB_HOST", value = aws_db_instance.this.address },
|
|
1084
|
+
{ name = "REDIS_CACHE", value = local.redis_cache },
|
|
1085
|
+
{ name = "REDIS_QUEUE", value = local.redis_queue },
|
|
1086
|
+
{ name = "REDIS_SOCKETIO", value = local.redis_socketio },
|
|
1087
|
+
]
|
|
1088
|
+
|
|
1089
|
+
mountPoints = local.sites_mount
|
|
1090
|
+
logConfiguration = merge(local.log_configuration["backend"], {
|
|
1091
|
+
options = merge(local.log_configuration["backend"].options, { awslogs-stream-prefix = "configurator" })
|
|
1092
|
+
})
|
|
1093
|
+
}
|
|
1094
|
+
])
|
|
1095
|
+
}
|
|
1096
|
+
|
|
1097
|
+
# create-site: first-deploy only. Idempotent — exits 0 when the site exists.
|
|
1098
|
+
resource "aws_ecs_task_definition" "create_site" {
|
|
1099
|
+
family = "${local.name}-create-site"
|
|
1100
|
+
requires_compatibilities = ["FARGATE"]
|
|
1101
|
+
network_mode = "awsvpc"
|
|
1102
|
+
cpu = var.backend_cpu
|
|
1103
|
+
memory = var.backend_memory
|
|
1104
|
+
execution_role_arn = aws_iam_role.execution.arn
|
|
1105
|
+
task_role_arn = aws_iam_role.task.arn
|
|
1106
|
+
|
|
1107
|
+
volume {
|
|
1108
|
+
name = local.sites_volume.name
|
|
1109
|
+
|
|
1110
|
+
efs_volume_configuration {
|
|
1111
|
+
file_system_id = local.sites_volume.efs_volume_configuration.file_system_id
|
|
1112
|
+
transit_encryption = local.sites_volume.efs_volume_configuration.transit_encryption
|
|
1113
|
+
|
|
1114
|
+
authorization_config {
|
|
1115
|
+
access_point_id = local.sites_volume.efs_volume_configuration.authorization_config.access_point_id
|
|
1116
|
+
iam = local.sites_volume.efs_volume_configuration.authorization_config.iam
|
|
1117
|
+
}
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
1120
|
+
|
|
1121
|
+
container_definitions = jsonencode([
|
|
1122
|
+
{
|
|
1123
|
+
name = "create-site"
|
|
1124
|
+
image = local.image
|
|
1125
|
+
essential = true
|
|
1126
|
+
command = ["bash", "-c", join("; ", [
|
|
1127
|
+
"set -e",
|
|
1128
|
+
"if [ -d \"sites/$SITE_NAME\" ]; then echo \"site $SITE_NAME already exists\"; exit 0; fi",
|
|
1129
|
+
join(" ", [
|
|
1130
|
+
"bench new-site \"$SITE_NAME\"",
|
|
1131
|
+
"--no-mariadb-socket",
|
|
1132
|
+
"--mariadb-root-username admin",
|
|
1133
|
+
"--mariadb-root-password \"$DB_ROOT_PASSWORD\"",
|
|
1134
|
+
"--admin-password \"$ADMIN_PASSWORD\"",
|
|
1135
|
+
{{#if withHelpdesk}}
|
|
1136
|
+
"--install-app helpdesk",
|
|
1137
|
+
{{/if}}
|
|
1138
|
+
"--install-app ticketing",
|
|
1139
|
+
"--set-default",
|
|
1140
|
+
]),
|
|
1141
|
+
# RDS: frappe must not attempt grants that need SUPER privileges.
|
|
1142
|
+
"bench --site \"$SITE_NAME\" set-config rds_db 1",
|
|
1143
|
+
"echo site created",
|
|
1144
|
+
])]
|
|
1145
|
+
|
|
1146
|
+
environment = [
|
|
1147
|
+
{ name = "SITE_NAME", value = local.site_name },
|
|
1148
|
+
]
|
|
1149
|
+
|
|
1150
|
+
secrets = [
|
|
1151
|
+
{ name = "DB_ROOT_PASSWORD", valueFrom = aws_secretsmanager_secret.db_root_password.arn },
|
|
1152
|
+
{ name = "ADMIN_PASSWORD", valueFrom = aws_secretsmanager_secret.admin_password.arn },
|
|
1153
|
+
]
|
|
1154
|
+
|
|
1155
|
+
mountPoints = local.sites_mount
|
|
1156
|
+
logConfiguration = merge(local.log_configuration["backend"], {
|
|
1157
|
+
options = merge(local.log_configuration["backend"].options, { awslogs-stream-prefix = "create-site" })
|
|
1158
|
+
})
|
|
1159
|
+
}
|
|
1160
|
+
])
|
|
1161
|
+
}
|
|
1162
|
+
|
|
1163
|
+
# migrate: after every deploy that changes DocTypes/patches/fixtures. Also
|
|
1164
|
+
# refreshes the baked assets, so a plain `ship + migrate` cycle is complete.
|
|
1165
|
+
resource "aws_ecs_task_definition" "migrate" {
|
|
1166
|
+
family = "${local.name}-migrate"
|
|
1167
|
+
requires_compatibilities = ["FARGATE"]
|
|
1168
|
+
network_mode = "awsvpc"
|
|
1169
|
+
cpu = var.backend_cpu
|
|
1170
|
+
memory = var.backend_memory
|
|
1171
|
+
execution_role_arn = aws_iam_role.execution.arn
|
|
1172
|
+
task_role_arn = aws_iam_role.task.arn
|
|
1173
|
+
|
|
1174
|
+
volume {
|
|
1175
|
+
name = local.sites_volume.name
|
|
1176
|
+
|
|
1177
|
+
efs_volume_configuration {
|
|
1178
|
+
file_system_id = local.sites_volume.efs_volume_configuration.file_system_id
|
|
1179
|
+
transit_encryption = local.sites_volume.efs_volume_configuration.transit_encryption
|
|
1180
|
+
|
|
1181
|
+
authorization_config {
|
|
1182
|
+
access_point_id = local.sites_volume.efs_volume_configuration.authorization_config.access_point_id
|
|
1183
|
+
iam = local.sites_volume.efs_volume_configuration.authorization_config.iam
|
|
1184
|
+
}
|
|
1185
|
+
}
|
|
1186
|
+
}
|
|
1187
|
+
|
|
1188
|
+
container_definitions = jsonencode([
|
|
1189
|
+
{
|
|
1190
|
+
name = "migrate"
|
|
1191
|
+
image = local.image
|
|
1192
|
+
essential = true
|
|
1193
|
+
command = ["bash", "-c", join("; ", [
|
|
1194
|
+
"set -e",
|
|
1195
|
+
"cp -rf ${local.bench_dir}/assets-baked/. sites/assets/ 2>/dev/null || true",
|
|
1196
|
+
"bench --site all migrate",
|
|
1197
|
+
])]
|
|
1198
|
+
|
|
1199
|
+
mountPoints = local.sites_mount
|
|
1200
|
+
logConfiguration = merge(local.log_configuration["backend"], {
|
|
1201
|
+
options = merge(local.log_configuration["backend"].options, { awslogs-stream-prefix = "migrate" })
|
|
1202
|
+
})
|
|
1203
|
+
}
|
|
1204
|
+
])
|
|
1205
|
+
}
|