@cubocicloide/stack-react-django 3.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 +66 -0
- package/dist/commands/lint/checks/BE/001.d.ts +6 -0
- package/dist/commands/lint/checks/BE/001.js +94 -0
- package/dist/commands/lint/checks/BE/001.js.map +1 -0
- package/dist/commands/lint/checks/BE/002.d.ts +6 -0
- package/dist/commands/lint/checks/BE/002.js +136 -0
- package/dist/commands/lint/checks/BE/002.js.map +1 -0
- package/dist/commands/lint/checks/BE/003.d.ts +6 -0
- package/dist/commands/lint/checks/BE/003.js +89 -0
- package/dist/commands/lint/checks/BE/003.js.map +1 -0
- package/dist/commands/lint/checks/BE/004.d.ts +6 -0
- package/dist/commands/lint/checks/BE/004.js +68 -0
- package/dist/commands/lint/checks/BE/004.js.map +1 -0
- package/dist/commands/lint/checks/BE/005.d.ts +6 -0
- package/dist/commands/lint/checks/BE/005.js +55 -0
- package/dist/commands/lint/checks/BE/005.js.map +1 -0
- package/dist/commands/lint/checks/BE/006.d.ts +6 -0
- package/dist/commands/lint/checks/BE/006.js +95 -0
- package/dist/commands/lint/checks/BE/006.js.map +1 -0
- package/dist/commands/lint/checks/BE/007.d.ts +6 -0
- package/dist/commands/lint/checks/BE/007.js +53 -0
- package/dist/commands/lint/checks/BE/007.js.map +1 -0
- package/dist/commands/lint/checks/BE/008.d.ts +6 -0
- package/dist/commands/lint/checks/BE/008.js +70 -0
- package/dist/commands/lint/checks/BE/008.js.map +1 -0
- package/dist/commands/lint/checks/BE/009.d.ts +6 -0
- package/dist/commands/lint/checks/BE/009.js +112 -0
- package/dist/commands/lint/checks/BE/009.js.map +1 -0
- package/dist/commands/lint/checks/BE/010.d.ts +6 -0
- package/dist/commands/lint/checks/BE/010.js +76 -0
- package/dist/commands/lint/checks/BE/010.js.map +1 -0
- package/dist/commands/lint/checks/BE/011.d.ts +6 -0
- package/dist/commands/lint/checks/BE/011.js +76 -0
- package/dist/commands/lint/checks/BE/011.js.map +1 -0
- package/dist/commands/lint/checks/BE/012.d.ts +6 -0
- package/dist/commands/lint/checks/BE/012.js +61 -0
- package/dist/commands/lint/checks/BE/012.js.map +1 -0
- package/dist/commands/lint/checks/BE/013.d.ts +6 -0
- package/dist/commands/lint/checks/BE/013.js +98 -0
- package/dist/commands/lint/checks/BE/013.js.map +1 -0
- package/dist/commands/lint/checks/BE/014.d.ts +6 -0
- package/dist/commands/lint/checks/BE/014.js +64 -0
- package/dist/commands/lint/checks/BE/014.js.map +1 -0
- package/dist/commands/lint/checks/E2E/001.d.ts +6 -0
- package/dist/commands/lint/checks/E2E/001.js +35 -0
- package/dist/commands/lint/checks/E2E/001.js.map +1 -0
- package/dist/commands/lint/checks/E2E/002.d.ts +6 -0
- package/dist/commands/lint/checks/E2E/002.js +37 -0
- package/dist/commands/lint/checks/E2E/002.js.map +1 -0
- package/dist/commands/lint/checks/E2E/003.d.ts +6 -0
- package/dist/commands/lint/checks/E2E/003.js +42 -0
- package/dist/commands/lint/checks/E2E/003.js.map +1 -0
- package/dist/commands/lint/checks/E2E/004.d.ts +6 -0
- package/dist/commands/lint/checks/E2E/004.js +37 -0
- package/dist/commands/lint/checks/E2E/004.js.map +1 -0
- package/dist/commands/lint/checks/E2E/005.d.ts +6 -0
- package/dist/commands/lint/checks/E2E/005.js +47 -0
- package/dist/commands/lint/checks/E2E/005.js.map +1 -0
- package/dist/commands/lint/checks/E2E/006.d.ts +6 -0
- package/dist/commands/lint/checks/E2E/006.js +28 -0
- package/dist/commands/lint/checks/E2E/006.js.map +1 -0
- package/dist/commands/lint/checks/E2E/007.d.ts +6 -0
- package/dist/commands/lint/checks/E2E/007.js +47 -0
- package/dist/commands/lint/checks/E2E/007.js.map +1 -0
- package/dist/commands/lint/checks/FE/001.d.ts +6 -0
- package/dist/commands/lint/checks/FE/001.js +56 -0
- package/dist/commands/lint/checks/FE/001.js.map +1 -0
- package/dist/commands/lint/checks/FE/002.d.ts +6 -0
- package/dist/commands/lint/checks/FE/002.js +80 -0
- package/dist/commands/lint/checks/FE/002.js.map +1 -0
- package/dist/commands/lint/checks/FE/003.d.ts +6 -0
- package/dist/commands/lint/checks/FE/003.js +65 -0
- package/dist/commands/lint/checks/FE/003.js.map +1 -0
- package/dist/commands/lint/checks/FE/004.d.ts +6 -0
- package/dist/commands/lint/checks/FE/004.js +72 -0
- package/dist/commands/lint/checks/FE/004.js.map +1 -0
- package/dist/commands/lint/checks/FE/005.d.ts +6 -0
- package/dist/commands/lint/checks/FE/005.js +103 -0
- package/dist/commands/lint/checks/FE/005.js.map +1 -0
- package/dist/commands/lint/checks/FE/006.d.ts +6 -0
- package/dist/commands/lint/checks/FE/006.js +102 -0
- package/dist/commands/lint/checks/FE/006.js.map +1 -0
- package/dist/commands/lint/checks/FE/007.d.ts +6 -0
- package/dist/commands/lint/checks/FE/007.js +60 -0
- package/dist/commands/lint/checks/FE/007.js.map +1 -0
- package/dist/commands/lint/checks/FE/008.d.ts +6 -0
- package/dist/commands/lint/checks/FE/008.js +115 -0
- package/dist/commands/lint/checks/FE/008.js.map +1 -0
- package/dist/commands/lint/checks/FE/009.d.ts +6 -0
- package/dist/commands/lint/checks/FE/009.js +83 -0
- package/dist/commands/lint/checks/FE/009.js.map +1 -0
- package/dist/commands/lint/checks/FE/010.d.ts +6 -0
- package/dist/commands/lint/checks/FE/010.js +105 -0
- package/dist/commands/lint/checks/FE/010.js.map +1 -0
- package/dist/commands/lint/checks/FE/011.d.ts +6 -0
- package/dist/commands/lint/checks/FE/011.js +43 -0
- package/dist/commands/lint/checks/FE/011.js.map +1 -0
- package/dist/commands/lint/checks/FE/012.d.ts +6 -0
- package/dist/commands/lint/checks/FE/012.js +150 -0
- package/dist/commands/lint/checks/FE/012.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +3436 -0
- package/dist/index.js.map +1 -0
- package/dude.stack.json +13 -0
- package/package.json +43 -0
- package/templates/aws-ecs/backend/Dockerfile.prod +34 -0
- package/templates/aws-ecs/docs/docs/deploy.md.hbs +54 -0
- package/templates/aws-ecs/frontend/Dockerfile.prod +28 -0
- package/templates/aws-ecs/frontend/nginx.conf +21 -0
- package/templates/aws-ecs/iac/README.md.hbs +75 -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 +102 -0
- package/templates/aws-ecs/iac/terraform/bootstrap/outputs.tf +19 -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 +43 -0
- package/templates/aws-ecs/iac/terraform/main.tf.hbs +949 -0
- package/templates/aws-ecs/iac/terraform/outputs.tf.hbs +71 -0
- package/templates/aws-ecs/iac/terraform/providers.tf +19 -0
- package/templates/aws-ecs/iac/terraform/variables.tf.hbs +113 -0
- package/templates/aws-ecs/iac/terraform/versions.tf +19 -0
- package/templates/base/.claude/agents/issue-fixer.md +190 -0
- package/templates/base/.claude/agents/security-fixer.md +250 -0
- package/templates/base/.claude/rules/BE/001.md +19 -0
- package/templates/base/.claude/rules/BE/002.md +21 -0
- package/templates/base/.claude/rules/BE/003.md +19 -0
- package/templates/base/.claude/rules/BE/004.md +16 -0
- package/templates/base/.claude/rules/BE/005.md +23 -0
- package/templates/base/.claude/rules/BE/006.md +13 -0
- package/templates/base/.claude/rules/BE/007.md +24 -0
- package/templates/base/.claude/rules/BE/008.md +19 -0
- package/templates/base/.claude/rules/BE/009.md +18 -0
- package/templates/base/.claude/rules/BE/010.md +15 -0
- package/templates/base/.claude/rules/BE/011.md +17 -0
- package/templates/base/.claude/rules/BE/012.md +18 -0
- package/templates/base/.claude/rules/BE/013.md +22 -0
- package/templates/base/.claude/rules/BE/014.md +15 -0
- package/templates/base/.claude/rules/E2E/001.md +13 -0
- package/templates/base/.claude/rules/E2E/002.md +14 -0
- package/templates/base/.claude/rules/E2E/003.md +10 -0
- package/templates/base/.claude/rules/E2E/004.md +23 -0
- package/templates/base/.claude/rules/E2E/005.md +15 -0
- package/templates/base/.claude/rules/E2E/006.md +13 -0
- package/templates/base/.claude/rules/E2E/007.md +18 -0
- package/templates/base/.claude/rules/FE/001.md +22 -0
- package/templates/base/.claude/rules/FE/002.md +39 -0
- package/templates/base/.claude/rules/FE/003.md +19 -0
- package/templates/base/.claude/rules/FE/004.md +21 -0
- package/templates/base/.claude/rules/FE/005.md +33 -0
- package/templates/base/.claude/rules/FE/006.md +24 -0
- package/templates/base/.claude/rules/FE/007.md +19 -0
- package/templates/base/.claude/rules/FE/008.md +26 -0
- package/templates/base/.claude/rules/FE/009.md +30 -0
- package/templates/base/.claude/rules/FE/010.md +25 -0
- package/templates/base/.claude/rules/FE/011.md +22 -0
- package/templates/base/.claude/rules/FE/012.md +32 -0
- package/templates/base/.claude/settings.json +53 -0
- package/templates/base/.claude/skills/clean-branches/SKILL.md +100 -0
- package/templates/base/.claude/skills/create/SKILL.md +47 -0
- package/templates/base/.claude/skills/create-jira-issue/SKILL.md +174 -0
- package/templates/base/.claude/skills/create-page/SKILL.md +195 -0
- package/templates/base/.claude/skills/create-route/SKILL.md +156 -0
- package/templates/base/.claude/skills/draft-email/SKILL.md +104 -0
- package/templates/base/.claude/skills/fix-issues/SKILL.md +238 -0
- package/templates/base/.claude/skills/security-scan/SKILL.md +297 -0
- package/templates/base/.claude/skills/verify-security-fixes/SKILL.md +180 -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/.worktreeinclude +2 -0
- package/templates/base/CLAUDE.md.hbs +391 -0
- package/templates/base/README.md.hbs +188 -0
- package/templates/base/_env.example.hbs +39 -0
- package/templates/base/_env.hbs +35 -0
- package/templates/base/_gitignore +31 -0
- package/templates/base/_npmrc +2 -0
- package/templates/base/backend/Dockerfile +16 -0
- package/templates/base/backend/README.md.hbs +90 -0
- package/templates/base/backend/_dockerignore +12 -0
- package/templates/base/backend/apps/__init__.py +0 -0
- package/templates/base/backend/apps/core/__init__.py +0 -0
- package/templates/base/backend/apps/core/apps.py +6 -0
- package/templates/base/backend/apps/core/serializers.py +7 -0
- package/templates/base/backend/apps/core/tests/__init__.py +0 -0
- package/templates/base/backend/apps/core/tests/test_views.py +16 -0
- package/templates/base/backend/apps/core/urls.py +9 -0
- package/templates/base/backend/apps/core/views.py +24 -0
- package/templates/base/backend/apps/users/__init__.py +0 -0
- package/templates/base/backend/apps/users/admin.py +6 -0
- package/templates/base/backend/apps/users/apps.py +6 -0
- package/templates/base/backend/apps/users/migrations/0001_initial.py +41 -0
- package/templates/base/backend/apps/users/migrations/__init__.py +0 -0
- package/templates/base/backend/apps/users/models.py +15 -0
- package/templates/base/backend/apps/users/serializers.py +16 -0
- package/templates/base/backend/apps/users/services.py +25 -0
- package/templates/base/backend/apps/users/tests/__init__.py +0 -0
- package/templates/base/backend/apps/users/tests/test_models.py +30 -0
- package/templates/base/backend/apps/users/tests/test_views.py +55 -0
- package/templates/base/backend/apps/users/urls.py +10 -0
- package/templates/base/backend/apps/users/views.py +20 -0
- package/templates/base/backend/config/__init__.py.hbs +5 -0
- package/templates/base/backend/config/asgi.py +9 -0
- package/templates/base/backend/config/settings/__init__.py +0 -0
- package/templates/base/backend/config/settings/base.py.hbs +197 -0
- package/templates/base/backend/config/settings/local.py +31 -0
- package/templates/base/backend/config/settings/production.py.hbs +37 -0
- package/templates/base/backend/config/urls.py.hbs +22 -0
- package/templates/base/backend/config/wsgi.py +9 -0
- package/templates/base/backend/manage.py +23 -0
- package/templates/base/backend/pyproject.toml.hbs +48 -0
- package/templates/base/backend/start.sh +5 -0
- package/templates/base/docker-compose.yml.hbs +196 -0
- package/templates/base/docs/docs/api.md +21 -0
- package/templates/base/docs/docs/dude.md.hbs +305 -0
- package/templates/base/docs/docs/index.md.hbs +92 -0
- package/templates/base/docs/docs/mkdocs.md +231 -0
- package/templates/base/docs/mkdocs.yml.hbs +52 -0
- package/templates/base/e2e/.prettierrc.json +1 -0
- package/templates/base/e2e/_prettierignore +3 -0
- package/templates/base/e2e/cucumber.js +21 -0
- package/templates/base/e2e/eslint.config.mjs +14 -0
- package/templates/base/e2e/features/example.feature +13 -0
- package/templates/base/e2e/package.json.hbs +30 -0
- package/templates/base/e2e/pages/ExamplePage.ts +16 -0
- package/templates/base/e2e/playwright.config.ts +27 -0
- package/templates/base/e2e/steps/common.steps.ts +32 -0
- package/templates/base/e2e/steps/example.steps.ts +4 -0
- package/templates/base/e2e/support/hooks.ts +72 -0
- package/templates/base/e2e/support/world.ts +24 -0
- package/templates/base/e2e/tsconfig.json +22 -0
- package/templates/base/frontend/.prettierrc.json +6 -0
- package/templates/base/frontend/Dockerfile +10 -0
- package/templates/base/frontend/_dockerignore +4 -0
- package/templates/base/frontend/eslint.config.js +32 -0
- package/templates/base/frontend/index.html.hbs +13 -0
- package/templates/base/frontend/package.json.hbs +38 -0
- package/templates/base/frontend/public/favicon.svg +5 -0
- package/templates/base/frontend/src/$components/Layout/$assets/logo.svg +5 -0
- package/templates/base/frontend/src/$components/Layout/constants.tsx +8 -0
- package/templates/base/frontend/src/$components/Layout/index.tsx +80 -0
- package/templates/base/frontend/src/$components/Layout/types.tsx +9 -0
- package/templates/base/frontend/src/$components/index.tsx +1 -0
- package/templates/base/frontend/src/$hooks/index.tsx +2 -0
- package/templates/base/frontend/src/$hooks/useCounterStore/index.tsx +28 -0
- package/templates/base/frontend/src/$hooks/useCounterStore/types.tsx +7 -0
- package/templates/base/frontend/src/$hooks/usePageTitle/index.tsx +21 -0
- package/templates/base/frontend/src/$types/vite-env.d.ts +10 -0
- package/templates/base/frontend/src/App.tsx +36 -0
- package/templates/base/frontend/src/main.tsx +10 -0
- package/templates/base/frontend/src/openapi/utils/openapi.yaml.hbs +129 -0
- package/templates/base/frontend/src/pages/constants.tsx +31 -0
- package/templates/base/frontend/src/pages/index.tsx +192 -0
- package/templates/base/frontend/src/pages/types.tsx +6 -0
- package/templates/base/frontend/src/pages/users/[id]/index.tsx +53 -0
- package/templates/base/frontend/src/pages/users/index.tsx +57 -0
- package/templates/base/frontend/src/pages/users/types.tsx +4 -0
- package/templates/base/frontend/src/styles.module.css +7 -0
- package/templates/base/frontend/src/utils/constants.tsx +11 -0
- package/templates/base/frontend/src/utils/formatters/index.tsx +49 -0
- package/templates/base/frontend/src/utils/functions.tsx +70 -0
- package/templates/base/frontend/src/utils/index.tsx +3 -0
- package/templates/base/frontend/tsconfig.json +24 -0
- package/templates/base/frontend/vite.config.ts +20 -0
- package/templates/base/package.json.hbs +11 -0
- package/templates/base/security/baseline.json +4 -0
- package/templates/celery/backend/apps/core/tasks.py +14 -0
- package/templates/celery/backend/apps/core/tests/test_tasks.py +6 -0
- package/templates/celery/backend/config/celery.py.hbs +26 -0
- package/templates/celerybeat/backend/apps/core/tasks_scheduled.py +18 -0
- package/templates/celerybeat/backend/apps/core/tests/test_tasks_scheduled.py +6 -0
- package/templates/postgres/.dude/commands/reset.ts +83 -0
- package/templates/s3/backend/apps/files/__init__.py +0 -0
- package/templates/s3/backend/apps/files/admin.py +9 -0
- package/templates/s3/backend/apps/files/apps.py +6 -0
- package/templates/s3/backend/apps/files/migrations/0001_initial.py +29 -0
- package/templates/s3/backend/apps/files/migrations/__init__.py +0 -0
- package/templates/s3/backend/apps/files/models.py +21 -0
- package/templates/s3/backend/apps/files/serializers.py +27 -0
- package/templates/s3/backend/apps/files/services.py +19 -0
- package/templates/s3/backend/apps/files/tests/__init__.py +0 -0
- package/templates/s3/backend/apps/files/tests/test_models.py +10 -0
- package/templates/s3/backend/apps/files/tests/test_views.py +32 -0
- package/templates/s3/backend/apps/files/urls.py +10 -0
- package/templates/s3/backend/apps/files/views.py +29 -0
- package/templates/s3/frontend/src/openapi/utils/openapi.yaml.hbs +253 -0
|
@@ -0,0 +1,949 @@
|
|
|
1
|
+
# ECS Fargate deployment for {{projectName}}: two containers — the Django
|
|
2
|
+
# backend (gunicorn) and the nginx-served React frontend — behind one
|
|
3
|
+
# internet-facing ALB that routes by path (`/api/*`, `/admin/*`,
|
|
4
|
+
# `/django-static/*` → backend; everything else → frontend), plus an RDS
|
|
5
|
+
# PostgreSQL instance. Images are pulled from the two shared ECR repositories
|
|
6
|
+
# created by `dude iac bootstrap`.
|
|
7
|
+
#
|
|
8
|
+
# Why ECS (and not EKS): the scaffold is two stateless containers and one small
|
|
9
|
+
# database — a Kubernetes control plane would cost more per month than the
|
|
10
|
+
# workload itself. Fargate runs the containers serverlessly.
|
|
11
|
+
#
|
|
12
|
+
# NOTE: production always runs PostgreSQL (RDS), even when local development
|
|
13
|
+
# was scaffolded on SQLite — a file database cannot survive Fargate's ephemeral
|
|
14
|
+
# task storage, so there is no SQLite option here by design.
|
|
15
|
+
|
|
16
|
+
locals {
|
|
17
|
+
name = "${var.project_name}-${var.environment}"
|
|
18
|
+
backend_port = 8000
|
|
19
|
+
frontend_port = 80
|
|
20
|
+
with_domain = var.domain_name != "" && var.route53_zone_name != ""
|
|
21
|
+
|
|
22
|
+
# Host + URL the app is served on — the custom domain when configured,
|
|
23
|
+
# otherwise the ALB's DNS name. Fed to Django as ALLOWED_HOSTS / CSRF origins.
|
|
24
|
+
app_host = local.with_domain ? var.domain_name : aws_lb.this.dns_name
|
|
25
|
+
app_url = local.with_domain ? "https://${var.domain_name}" : "http://${aws_lb.this.dns_name}"
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
data "aws_availability_zones" "available" {
|
|
29
|
+
state = "available"
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
# ── Network ──────────────────────────────────────────────────────────────────
|
|
33
|
+
# A minimal VPC with two public subnets. Tasks run in the public subnets with a
|
|
34
|
+
# public IP (for image pulls / egress) but their security groups only admit
|
|
35
|
+
# traffic from the ALB — this avoids the ~$65/month a NAT gateway pair would
|
|
36
|
+
# cost a dev environment. Move the tasks to private subnets + NAT if your
|
|
37
|
+
# compliance posture requires it.
|
|
38
|
+
|
|
39
|
+
resource "aws_vpc" "this" {
|
|
40
|
+
cidr_block = "10.0.0.0/16"
|
|
41
|
+
enable_dns_support = true
|
|
42
|
+
enable_dns_hostnames = true
|
|
43
|
+
|
|
44
|
+
tags = { Name = local.name }
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
resource "aws_internet_gateway" "this" {
|
|
48
|
+
vpc_id = aws_vpc.this.id
|
|
49
|
+
|
|
50
|
+
tags = { Name = local.name }
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
resource "aws_subnet" "public" {
|
|
54
|
+
count = 2
|
|
55
|
+
vpc_id = aws_vpc.this.id
|
|
56
|
+
cidr_block = cidrsubnet(aws_vpc.this.cidr_block, 8, count.index)
|
|
57
|
+
availability_zone = data.aws_availability_zones.available.names[count.index]
|
|
58
|
+
map_public_ip_on_launch = true
|
|
59
|
+
|
|
60
|
+
tags = { Name = "${local.name}-public-${count.index}" }
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
resource "aws_route_table" "public" {
|
|
64
|
+
vpc_id = aws_vpc.this.id
|
|
65
|
+
|
|
66
|
+
route {
|
|
67
|
+
cidr_block = "0.0.0.0/0"
|
|
68
|
+
gateway_id = aws_internet_gateway.this.id
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
tags = { Name = "${local.name}-public" }
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
resource "aws_route_table_association" "public" {
|
|
75
|
+
count = 2
|
|
76
|
+
subnet_id = aws_subnet.public[count.index].id
|
|
77
|
+
route_table_id = aws_route_table.public.id
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
# ── Security groups ──────────────────────────────────────────────────────────
|
|
81
|
+
|
|
82
|
+
resource "aws_security_group" "alb" {
|
|
83
|
+
name_prefix = "${local.name}-alb-"
|
|
84
|
+
description = "ALB - admits HTTP/HTTPS from allowed_cidrs"
|
|
85
|
+
vpc_id = aws_vpc.this.id
|
|
86
|
+
|
|
87
|
+
ingress {
|
|
88
|
+
description = "HTTP"
|
|
89
|
+
from_port = 80
|
|
90
|
+
to_port = 80
|
|
91
|
+
protocol = "tcp"
|
|
92
|
+
cidr_blocks = var.allowed_cidrs
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
ingress {
|
|
96
|
+
description = "HTTPS"
|
|
97
|
+
from_port = 443
|
|
98
|
+
to_port = 443
|
|
99
|
+
protocol = "tcp"
|
|
100
|
+
cidr_blocks = var.allowed_cidrs
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
egress {
|
|
104
|
+
from_port = 0
|
|
105
|
+
to_port = 0
|
|
106
|
+
protocol = "-1"
|
|
107
|
+
cidr_blocks = ["0.0.0.0/0"]
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
lifecycle {
|
|
111
|
+
create_before_destroy = true
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
# Backend workloads: the Django API service, the one-off migrate task{{#if withCelery}} and the
|
|
116
|
+
# Celery worker{{#if withCeleryBeat}}/beat{{/if}} services{{/if}}. They share one security group — the RDS{{#if withRedis}}
|
|
117
|
+
# and ElastiCache groups{{else}} group{{/if}} below admit exactly this group.
|
|
118
|
+
resource "aws_security_group" "backend" {
|
|
119
|
+
name_prefix = "${local.name}-be-"
|
|
120
|
+
description = "Django backend tasks - only reachable from the ALB"
|
|
121
|
+
vpc_id = aws_vpc.this.id
|
|
122
|
+
|
|
123
|
+
ingress {
|
|
124
|
+
description = "HTTP from the ALB"
|
|
125
|
+
from_port = local.backend_port
|
|
126
|
+
to_port = local.backend_port
|
|
127
|
+
protocol = "tcp"
|
|
128
|
+
security_groups = [aws_security_group.alb.id]
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
egress {
|
|
132
|
+
from_port = 0
|
|
133
|
+
to_port = 0
|
|
134
|
+
protocol = "-1"
|
|
135
|
+
cidr_blocks = ["0.0.0.0/0"]
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
lifecycle {
|
|
139
|
+
create_before_destroy = true
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
resource "aws_security_group" "frontend" {
|
|
144
|
+
name_prefix = "${local.name}-fe-"
|
|
145
|
+
description = "Frontend (nginx) tasks - only reachable from the ALB"
|
|
146
|
+
vpc_id = aws_vpc.this.id
|
|
147
|
+
|
|
148
|
+
ingress {
|
|
149
|
+
description = "HTTP from the ALB"
|
|
150
|
+
from_port = local.frontend_port
|
|
151
|
+
to_port = local.frontend_port
|
|
152
|
+
protocol = "tcp"
|
|
153
|
+
security_groups = [aws_security_group.alb.id]
|
|
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 ECR repositories are shared across environments and owned by the
|
|
170
|
+
# bootstrap config (one image per service, promoted dev → staging → prod by
|
|
171
|
+
# tag) — read them by name.
|
|
172
|
+
|
|
173
|
+
data "aws_ecr_repository" "backend" {
|
|
174
|
+
name = "${var.project_name}-backend"
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
data "aws_ecr_repository" "frontend" {
|
|
178
|
+
name = "${var.project_name}-frontend"
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
resource "aws_cloudwatch_log_group" "backend" {
|
|
182
|
+
name = "/ecs/${local.name}-backend"
|
|
183
|
+
retention_in_days = var.log_retention_days
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
resource "aws_cloudwatch_log_group" "frontend" {
|
|
187
|
+
name = "/ecs/${local.name}-frontend"
|
|
188
|
+
retention_in_days = var.log_retention_days
|
|
189
|
+
}
|
|
190
|
+
{{#if withCelery}}
|
|
191
|
+
|
|
192
|
+
resource "aws_cloudwatch_log_group" "worker" {
|
|
193
|
+
name = "/ecs/${local.name}-worker"
|
|
194
|
+
retention_in_days = var.log_retention_days
|
|
195
|
+
}
|
|
196
|
+
{{/if}}
|
|
197
|
+
{{#if withCeleryBeat}}
|
|
198
|
+
|
|
199
|
+
resource "aws_cloudwatch_log_group" "beat" {
|
|
200
|
+
name = "/ecs/${local.name}-beat"
|
|
201
|
+
retention_in_days = var.log_retention_days
|
|
202
|
+
}
|
|
203
|
+
{{/if}}
|
|
204
|
+
|
|
205
|
+
# ── Database (RDS PostgreSQL) ────────────────────────────────────────────────
|
|
206
|
+
# Always provisioned: production runs Postgres even when local dev chose
|
|
207
|
+
# SQLite (Fargate task storage is ephemeral). Single-AZ, small instance —
|
|
208
|
+
# bump db_instance_class / add multi_az for production-grade durability.
|
|
209
|
+
|
|
210
|
+
resource "random_password" "db" {
|
|
211
|
+
length = 32
|
|
212
|
+
# URL-safe: the password is embedded in DATABASE_URL.
|
|
213
|
+
special = false
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
resource "aws_db_subnet_group" "this" {
|
|
217
|
+
name = local.name
|
|
218
|
+
subnet_ids = aws_subnet.public[*].id
|
|
219
|
+
|
|
220
|
+
tags = { Name = local.name }
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
resource "aws_security_group" "db" {
|
|
224
|
+
name_prefix = "${local.name}-db-"
|
|
225
|
+
description = "RDS PostgreSQL - only reachable from the backend tasks"
|
|
226
|
+
vpc_id = aws_vpc.this.id
|
|
227
|
+
|
|
228
|
+
ingress {
|
|
229
|
+
description = "PostgreSQL from the backend service"
|
|
230
|
+
from_port = 5432
|
|
231
|
+
to_port = 5432
|
|
232
|
+
protocol = "tcp"
|
|
233
|
+
security_groups = [aws_security_group.backend.id]
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
egress {
|
|
237
|
+
from_port = 0
|
|
238
|
+
to_port = 0
|
|
239
|
+
protocol = "-1"
|
|
240
|
+
cidr_blocks = ["0.0.0.0/0"]
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
lifecycle {
|
|
244
|
+
create_before_destroy = true
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
resource "aws_db_instance" "this" {
|
|
249
|
+
identifier = "${local.name}-db"
|
|
250
|
+
engine = "postgres"
|
|
251
|
+
engine_version = "16"
|
|
252
|
+
instance_class = var.db_instance_class
|
|
253
|
+
|
|
254
|
+
allocated_storage = var.db_allocated_storage
|
|
255
|
+
storage_type = "gp3"
|
|
256
|
+
|
|
257
|
+
db_name = "app"
|
|
258
|
+
username = "app"
|
|
259
|
+
password = random_password.db.result
|
|
260
|
+
|
|
261
|
+
db_subnet_group_name = aws_db_subnet_group.this.name
|
|
262
|
+
vpc_security_group_ids = [aws_security_group.db.id]
|
|
263
|
+
publicly_accessible = false
|
|
264
|
+
multi_az = false
|
|
265
|
+
|
|
266
|
+
skip_final_snapshot = true
|
|
267
|
+
apply_immediately = true
|
|
268
|
+
|
|
269
|
+
tags = { Name = "${local.name}-db" }
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
# ── Secrets ──────────────────────────────────────────────────────────────────
|
|
273
|
+
# DATABASE_URL and the Django SECRET_KEY live in Secrets Manager and are
|
|
274
|
+
# injected into the backend task via `secrets` (never plain env vars, never in
|
|
275
|
+
# state-committed tfvars). recovery_window_in_days = 0 so `dude iac destroy`
|
|
276
|
+
# followed by a re-apply doesn't collide with a secret pending deletion.
|
|
277
|
+
|
|
278
|
+
resource "aws_secretsmanager_secret" "database_url" {
|
|
279
|
+
name = "${local.name}/database-url"
|
|
280
|
+
recovery_window_in_days = 0
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
resource "aws_secretsmanager_secret_version" "database_url" {
|
|
284
|
+
secret_id = aws_secretsmanager_secret.database_url.id
|
|
285
|
+
secret_string = "postgres://${aws_db_instance.this.username}:${random_password.db.result}@${aws_db_instance.this.address}:5432/${aws_db_instance.this.db_name}"
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
resource "random_password" "django_secret_key" {
|
|
289
|
+
length = 50
|
|
290
|
+
special = false
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
resource "aws_secretsmanager_secret" "django_secret_key" {
|
|
294
|
+
name = "${local.name}/django-secret-key"
|
|
295
|
+
recovery_window_in_days = 0
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
resource "aws_secretsmanager_secret_version" "django_secret_key" {
|
|
299
|
+
secret_id = aws_secretsmanager_secret.django_secret_key.id
|
|
300
|
+
secret_string = random_password.django_secret_key.result
|
|
301
|
+
}
|
|
302
|
+
{{#if withS3}}
|
|
303
|
+
|
|
304
|
+
# ── Media storage (S3) ───────────────────────────────────────────────────────
|
|
305
|
+
# The `files` app stores uploads here via django-storages/boto3. No endpoint
|
|
306
|
+
# URL is configured in AWS (that's a MinIO-only, local-dev setting) — boto3
|
|
307
|
+
# talks to the real S3 API. force_destroy lets `dude iac destroy` remove the
|
|
308
|
+
# bucket even when it still holds uploads.
|
|
309
|
+
|
|
310
|
+
resource "aws_s3_bucket" "media" {
|
|
311
|
+
bucket = "${local.name}-media"
|
|
312
|
+
force_destroy = true
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
resource "aws_s3_bucket_public_access_block" "media" {
|
|
316
|
+
bucket = aws_s3_bucket.media.id
|
|
317
|
+
block_public_acls = true
|
|
318
|
+
block_public_policy = true
|
|
319
|
+
ignore_public_acls = true
|
|
320
|
+
restrict_public_buckets = true
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
resource "aws_s3_bucket_cors_configuration" "media" {
|
|
324
|
+
bucket = aws_s3_bucket.media.id
|
|
325
|
+
|
|
326
|
+
cors_rule {
|
|
327
|
+
allowed_headers = ["*"]
|
|
328
|
+
allowed_methods = ["GET", "PUT", "POST", "HEAD"]
|
|
329
|
+
allowed_origins = [local.app_url]
|
|
330
|
+
expose_headers = ["ETag"]
|
|
331
|
+
max_age_seconds = 3000
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
{{/if}}
|
|
335
|
+
{{#if withRedis}}
|
|
336
|
+
|
|
337
|
+
# ── Redis (ElastiCache) ──────────────────────────────────────────────────────
|
|
338
|
+
# Single-node broker/result backend for Celery. Only reachable from the
|
|
339
|
+
# backend security group (API service + worker{{#if withCeleryBeat}}/beat{{/if}} tasks).
|
|
340
|
+
|
|
341
|
+
resource "aws_security_group" "redis" {
|
|
342
|
+
name_prefix = "${local.name}-redis-"
|
|
343
|
+
description = "ElastiCache Redis - only reachable from the backend tasks"
|
|
344
|
+
vpc_id = aws_vpc.this.id
|
|
345
|
+
|
|
346
|
+
ingress {
|
|
347
|
+
description = "Redis from the backend/worker tasks"
|
|
348
|
+
from_port = 6379
|
|
349
|
+
to_port = 6379
|
|
350
|
+
protocol = "tcp"
|
|
351
|
+
security_groups = [aws_security_group.backend.id]
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
egress {
|
|
355
|
+
from_port = 0
|
|
356
|
+
to_port = 0
|
|
357
|
+
protocol = "-1"
|
|
358
|
+
cidr_blocks = ["0.0.0.0/0"]
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
lifecycle {
|
|
362
|
+
create_before_destroy = true
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
resource "aws_elasticache_subnet_group" "redis" {
|
|
367
|
+
name = "${local.name}-redis"
|
|
368
|
+
subnet_ids = aws_subnet.public[*].id
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
resource "aws_elasticache_cluster" "redis" {
|
|
372
|
+
cluster_id = "${local.name}-redis"
|
|
373
|
+
engine = "redis"
|
|
374
|
+
node_type = "cache.t4g.micro"
|
|
375
|
+
num_cache_nodes = 1
|
|
376
|
+
port = 6379
|
|
377
|
+
subnet_group_name = aws_elasticache_subnet_group.redis.name
|
|
378
|
+
security_group_ids = [aws_security_group.redis.id]
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
locals {
|
|
382
|
+
redis_url = "redis://${aws_elasticache_cluster.redis.cache_nodes[0].address}:6379/0"
|
|
383
|
+
}
|
|
384
|
+
{{/if}}
|
|
385
|
+
|
|
386
|
+
# ── IAM ──────────────────────────────────────────────────────────────────────
|
|
387
|
+
|
|
388
|
+
data "aws_iam_policy_document" "task_assume" {
|
|
389
|
+
statement {
|
|
390
|
+
actions = ["sts:AssumeRole"]
|
|
391
|
+
|
|
392
|
+
principals {
|
|
393
|
+
type = "Service"
|
|
394
|
+
identifiers = ["ecs-tasks.amazonaws.com"]
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
resource "aws_iam_role" "execution" {
|
|
400
|
+
name = "${local.name}-ecs-execution"
|
|
401
|
+
assume_role_policy = data.aws_iam_policy_document.task_assume.json
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
resource "aws_iam_role_policy_attachment" "execution" {
|
|
405
|
+
role = aws_iam_role.execution.name
|
|
406
|
+
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
# The execution role (not the task role) resolves `secrets` entries at task
|
|
410
|
+
# start, so it needs read access to exactly the two secrets injected above.
|
|
411
|
+
resource "aws_iam_role_policy" "execution_secrets" {
|
|
412
|
+
name = "read-app-secrets"
|
|
413
|
+
role = aws_iam_role.execution.id
|
|
414
|
+
|
|
415
|
+
policy = jsonencode({
|
|
416
|
+
Version = "2012-10-17"
|
|
417
|
+
Statement = [{
|
|
418
|
+
Effect = "Allow"
|
|
419
|
+
Action = ["secretsmanager:GetSecretValue"]
|
|
420
|
+
Resource = [
|
|
421
|
+
aws_secretsmanager_secret.database_url.arn,
|
|
422
|
+
aws_secretsmanager_secret.django_secret_key.arn,
|
|
423
|
+
]
|
|
424
|
+
}]
|
|
425
|
+
})
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
resource "aws_iam_role" "task" {
|
|
429
|
+
name = "${local.name}-ecs-task"
|
|
430
|
+
assume_role_policy = data.aws_iam_policy_document.task_assume.json
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
# ECS Exec — lets `aws ecs execute-command` open a shell in a running task.
|
|
434
|
+
resource "aws_iam_role_policy" "task_ecs_exec" {
|
|
435
|
+
name = "ecs-exec"
|
|
436
|
+
role = aws_iam_role.task.id
|
|
437
|
+
|
|
438
|
+
policy = jsonencode({
|
|
439
|
+
Version = "2012-10-17"
|
|
440
|
+
Statement = [{
|
|
441
|
+
Effect = "Allow"
|
|
442
|
+
Action = [
|
|
443
|
+
"ssmmessages:CreateControlChannel",
|
|
444
|
+
"ssmmessages:CreateDataChannel",
|
|
445
|
+
"ssmmessages:OpenControlChannel",
|
|
446
|
+
"ssmmessages:OpenDataChannel",
|
|
447
|
+
]
|
|
448
|
+
Resource = "*"
|
|
449
|
+
}]
|
|
450
|
+
})
|
|
451
|
+
}
|
|
452
|
+
{{#if withS3}}
|
|
453
|
+
|
|
454
|
+
# Read/write on the media bucket for the running containers (boto3 uses the
|
|
455
|
+
# task role's credentials automatically — no access keys in the environment).
|
|
456
|
+
resource "aws_iam_role_policy" "task_media_s3" {
|
|
457
|
+
name = "media-s3"
|
|
458
|
+
role = aws_iam_role.task.id
|
|
459
|
+
|
|
460
|
+
policy = jsonencode({
|
|
461
|
+
Version = "2012-10-17"
|
|
462
|
+
Statement = [
|
|
463
|
+
{
|
|
464
|
+
Effect = "Allow"
|
|
465
|
+
Action = ["s3:ListBucket", "s3:GetBucketLocation"]
|
|
466
|
+
Resource = aws_s3_bucket.media.arn
|
|
467
|
+
},
|
|
468
|
+
{
|
|
469
|
+
Effect = "Allow"
|
|
470
|
+
Action = ["s3:GetObject", "s3:PutObject", "s3:DeleteObject"]
|
|
471
|
+
Resource = "${aws_s3_bucket.media.arn}/*"
|
|
472
|
+
},
|
|
473
|
+
]
|
|
474
|
+
})
|
|
475
|
+
}
|
|
476
|
+
{{/if}}
|
|
477
|
+
|
|
478
|
+
# ── Load balancer ────────────────────────────────────────────────────────────
|
|
479
|
+
# One ALB, path-routed: `/api/*`, `/admin/*` and `/django-static/*` go to the
|
|
480
|
+
# backend target group; everything else falls through to the frontend (the
|
|
481
|
+
# static SPA bundle served by nginx).
|
|
482
|
+
|
|
483
|
+
resource "aws_lb" "this" {
|
|
484
|
+
name = local.name
|
|
485
|
+
load_balancer_type = "application"
|
|
486
|
+
security_groups = [aws_security_group.alb.id]
|
|
487
|
+
subnets = aws_subnet.public[*].id
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
resource "aws_lb_target_group" "frontend" {
|
|
491
|
+
name = "${local.name}-fe"
|
|
492
|
+
port = local.frontend_port
|
|
493
|
+
protocol = "HTTP"
|
|
494
|
+
target_type = "ip"
|
|
495
|
+
vpc_id = aws_vpc.this.id
|
|
496
|
+
|
|
497
|
+
# nginx serves the SPA's index.html at `/` — a 200 means the bundle is up.
|
|
498
|
+
health_check {
|
|
499
|
+
path = "/"
|
|
500
|
+
matcher = "200"
|
|
501
|
+
interval = 30
|
|
502
|
+
timeout = 5
|
|
503
|
+
healthy_threshold = 2
|
|
504
|
+
unhealthy_threshold = 3
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
deregistration_delay = 30
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
resource "aws_lb_target_group" "backend" {
|
|
511
|
+
name = "${local.name}-be"
|
|
512
|
+
port = local.backend_port
|
|
513
|
+
protocol = "HTTP"
|
|
514
|
+
target_type = "ip"
|
|
515
|
+
vpc_id = aws_vpc.this.id
|
|
516
|
+
|
|
517
|
+
# The scaffold's liveness route (no DB required, always 200 when the
|
|
518
|
+
# gunicorn workers are serving).
|
|
519
|
+
health_check {
|
|
520
|
+
path = "/api/health/"
|
|
521
|
+
matcher = "200"
|
|
522
|
+
interval = 30
|
|
523
|
+
timeout = 5
|
|
524
|
+
healthy_threshold = 2
|
|
525
|
+
unhealthy_threshold = 3
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
deregistration_delay = 30
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
# Plain HTTP when no domain is configured…
|
|
532
|
+
resource "aws_lb_listener" "http" {
|
|
533
|
+
count = local.with_domain ? 0 : 1
|
|
534
|
+
load_balancer_arn = aws_lb.this.arn
|
|
535
|
+
port = 80
|
|
536
|
+
protocol = "HTTP"
|
|
537
|
+
|
|
538
|
+
default_action {
|
|
539
|
+
type = "forward"
|
|
540
|
+
target_group_arn = aws_lb_target_group.frontend.arn
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
# …or HTTPS with an ACM certificate + HTTP→HTTPS redirect when one is.
|
|
545
|
+
data "aws_route53_zone" "this" {
|
|
546
|
+
count = local.with_domain ? 1 : 0
|
|
547
|
+
name = var.route53_zone_name
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
resource "aws_acm_certificate" "this" {
|
|
551
|
+
count = local.with_domain ? 1 : 0
|
|
552
|
+
domain_name = var.domain_name
|
|
553
|
+
validation_method = "DNS"
|
|
554
|
+
|
|
555
|
+
lifecycle {
|
|
556
|
+
create_before_destroy = true
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
resource "aws_route53_record" "cert_validation" {
|
|
561
|
+
for_each = local.with_domain ? {
|
|
562
|
+
for dvo in aws_acm_certificate.this[0].domain_validation_options : dvo.domain_name => {
|
|
563
|
+
name = dvo.resource_record_name
|
|
564
|
+
record = dvo.resource_record_value
|
|
565
|
+
type = dvo.resource_record_type
|
|
566
|
+
}
|
|
567
|
+
} : {}
|
|
568
|
+
|
|
569
|
+
zone_id = data.aws_route53_zone.this[0].zone_id
|
|
570
|
+
name = each.value.name
|
|
571
|
+
type = each.value.type
|
|
572
|
+
ttl = 60
|
|
573
|
+
records = [each.value.record]
|
|
574
|
+
allow_overwrite = true
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
resource "aws_acm_certificate_validation" "this" {
|
|
578
|
+
count = local.with_domain ? 1 : 0
|
|
579
|
+
certificate_arn = aws_acm_certificate.this[0].arn
|
|
580
|
+
validation_record_fqdns = [for r in aws_route53_record.cert_validation : r.fqdn]
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
resource "aws_lb_listener" "https" {
|
|
584
|
+
count = local.with_domain ? 1 : 0
|
|
585
|
+
load_balancer_arn = aws_lb.this.arn
|
|
586
|
+
port = 443
|
|
587
|
+
protocol = "HTTPS"
|
|
588
|
+
ssl_policy = "ELBSecurityPolicy-TLS13-1-2-2021-06"
|
|
589
|
+
certificate_arn = aws_acm_certificate_validation.this[0].certificate_arn
|
|
590
|
+
|
|
591
|
+
default_action {
|
|
592
|
+
type = "forward"
|
|
593
|
+
target_group_arn = aws_lb_target_group.frontend.arn
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
resource "aws_lb_listener" "http_redirect" {
|
|
598
|
+
count = local.with_domain ? 1 : 0
|
|
599
|
+
load_balancer_arn = aws_lb.this.arn
|
|
600
|
+
port = 80
|
|
601
|
+
protocol = "HTTP"
|
|
602
|
+
|
|
603
|
+
default_action {
|
|
604
|
+
type = "redirect"
|
|
605
|
+
|
|
606
|
+
redirect {
|
|
607
|
+
port = "443"
|
|
608
|
+
protocol = "HTTPS"
|
|
609
|
+
status_code = "HTTP_301"
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
# Path routing to the backend, attached to whichever listener actually
|
|
615
|
+
# forwards traffic (HTTP without a domain, HTTPS with one — Terraform's
|
|
616
|
+
# conditional only evaluates the selected branch).
|
|
617
|
+
resource "aws_lb_listener_rule" "backend" {
|
|
618
|
+
listener_arn = local.with_domain ? aws_lb_listener.https[0].arn : aws_lb_listener.http[0].arn
|
|
619
|
+
priority = 10
|
|
620
|
+
|
|
621
|
+
action {
|
|
622
|
+
type = "forward"
|
|
623
|
+
target_group_arn = aws_lb_target_group.backend.arn
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
condition {
|
|
627
|
+
path_pattern {
|
|
628
|
+
values = ["/api/*", "/admin/*", "/django-static/*"]
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
resource "aws_route53_record" "app" {
|
|
634
|
+
count = local.with_domain ? 1 : 0
|
|
635
|
+
zone_id = data.aws_route53_zone.this[0].zone_id
|
|
636
|
+
name = var.domain_name
|
|
637
|
+
type = "A"
|
|
638
|
+
|
|
639
|
+
alias {
|
|
640
|
+
name = aws_lb.this.dns_name
|
|
641
|
+
zone_id = aws_lb.this.zone_id
|
|
642
|
+
evaluate_target_health = true
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
# ── ECS (Fargate) ────────────────────────────────────────────────────────────
|
|
647
|
+
|
|
648
|
+
resource "aws_ecs_cluster" "this" {
|
|
649
|
+
name = local.name
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
# Environment + secrets shared by every backend-image workload (API service,
|
|
653
|
+
# migrate task{{#if withCelery}}, worker{{#if withCeleryBeat}}, beat{{/if}}{{/if}}).
|
|
654
|
+
locals {
|
|
655
|
+
backend_environment = [
|
|
656
|
+
{ name = "DJANGO_SETTINGS_MODULE", value = "config.settings.production" },
|
|
657
|
+
{ name = "DJANGO_ALLOWED_HOSTS", value = local.app_host },
|
|
658
|
+
{ name = "DJANGO_CSRF_TRUSTED_ORIGINS", value = local.app_url },
|
|
659
|
+
{ name = "APP_TITLE", value = var.project_name },
|
|
660
|
+
{{#if withS3}}
|
|
661
|
+
{ name = "AWS_STORAGE_BUCKET_NAME", value = aws_s3_bucket.media.bucket },
|
|
662
|
+
{ name = "AWS_S3_REGION_NAME", value = var.region },
|
|
663
|
+
{{/if}}
|
|
664
|
+
{{#if withRedis}}
|
|
665
|
+
{ name = "CELERY_BROKER_URL", value = local.redis_url },
|
|
666
|
+
{ name = "CELERY_RESULT_BACKEND", value = local.redis_url },
|
|
667
|
+
{{/if}}
|
|
668
|
+
]
|
|
669
|
+
|
|
670
|
+
backend_secrets = [
|
|
671
|
+
{ name = "DATABASE_URL", valueFrom = aws_secretsmanager_secret.database_url.arn },
|
|
672
|
+
{ name = "DJANGO_SECRET_KEY", valueFrom = aws_secretsmanager_secret.django_secret_key.arn },
|
|
673
|
+
]
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
resource "aws_ecs_task_definition" "backend" {
|
|
677
|
+
family = "${local.name}-backend"
|
|
678
|
+
requires_compatibilities = ["FARGATE"]
|
|
679
|
+
network_mode = "awsvpc"
|
|
680
|
+
cpu = var.backend_cpu
|
|
681
|
+
memory = var.backend_memory
|
|
682
|
+
execution_role_arn = aws_iam_role.execution.arn
|
|
683
|
+
task_role_arn = aws_iam_role.task.arn
|
|
684
|
+
|
|
685
|
+
container_definitions = jsonencode([
|
|
686
|
+
{
|
|
687
|
+
name = "backend"
|
|
688
|
+
image = "${data.aws_ecr_repository.backend.repository_url}:${var.image_tag}"
|
|
689
|
+
essential = true
|
|
690
|
+
|
|
691
|
+
portMappings = [
|
|
692
|
+
{ containerPort = local.backend_port, protocol = "tcp" }
|
|
693
|
+
]
|
|
694
|
+
|
|
695
|
+
environment = local.backend_environment
|
|
696
|
+
secrets = local.backend_secrets
|
|
697
|
+
|
|
698
|
+
logConfiguration = {
|
|
699
|
+
logDriver = "awslogs"
|
|
700
|
+
options = {
|
|
701
|
+
awslogs-group = aws_cloudwatch_log_group.backend.name
|
|
702
|
+
awslogs-region = var.region
|
|
703
|
+
awslogs-stream-prefix = "backend"
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
])
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
resource "aws_ecs_service" "backend" {
|
|
711
|
+
name = "${local.name}-backend"
|
|
712
|
+
cluster = aws_ecs_cluster.this.id
|
|
713
|
+
task_definition = aws_ecs_task_definition.backend.arn
|
|
714
|
+
desired_count = var.backend_desired_count
|
|
715
|
+
launch_type = "FARGATE"
|
|
716
|
+
|
|
717
|
+
# Shell into a running task: aws ecs execute-command --command /bin/bash …
|
|
718
|
+
enable_execute_command = true
|
|
719
|
+
|
|
720
|
+
network_configuration {
|
|
721
|
+
subnets = aws_subnet.public[*].id
|
|
722
|
+
security_groups = [aws_security_group.backend.id]
|
|
723
|
+
assign_public_ip = true
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
load_balancer {
|
|
727
|
+
target_group_arn = aws_lb_target_group.backend.arn
|
|
728
|
+
container_name = "backend"
|
|
729
|
+
container_port = local.backend_port
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
health_check_grace_period_seconds = 60
|
|
733
|
+
|
|
734
|
+
# Roll back automatically when a deployment's tasks keep failing to start.
|
|
735
|
+
deployment_circuit_breaker {
|
|
736
|
+
enable = true
|
|
737
|
+
rollback = true
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
depends_on = [aws_lb_listener.http, aws_lb_listener.https, aws_lb_listener_rule.backend]
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
resource "aws_ecs_task_definition" "frontend" {
|
|
744
|
+
family = "${local.name}-frontend"
|
|
745
|
+
requires_compatibilities = ["FARGATE"]
|
|
746
|
+
network_mode = "awsvpc"
|
|
747
|
+
cpu = var.frontend_cpu
|
|
748
|
+
memory = var.frontend_memory
|
|
749
|
+
execution_role_arn = aws_iam_role.execution.arn
|
|
750
|
+
task_role_arn = aws_iam_role.task.arn
|
|
751
|
+
|
|
752
|
+
container_definitions = jsonencode([
|
|
753
|
+
{
|
|
754
|
+
name = "frontend"
|
|
755
|
+
image = "${data.aws_ecr_repository.frontend.repository_url}:${var.image_tag}"
|
|
756
|
+
essential = true
|
|
757
|
+
|
|
758
|
+
portMappings = [
|
|
759
|
+
{ containerPort = local.frontend_port, protocol = "tcp" }
|
|
760
|
+
]
|
|
761
|
+
|
|
762
|
+
logConfiguration = {
|
|
763
|
+
logDriver = "awslogs"
|
|
764
|
+
options = {
|
|
765
|
+
awslogs-group = aws_cloudwatch_log_group.frontend.name
|
|
766
|
+
awslogs-region = var.region
|
|
767
|
+
awslogs-stream-prefix = "frontend"
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
])
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
resource "aws_ecs_service" "frontend" {
|
|
775
|
+
name = "${local.name}-frontend"
|
|
776
|
+
cluster = aws_ecs_cluster.this.id
|
|
777
|
+
task_definition = aws_ecs_task_definition.frontend.arn
|
|
778
|
+
desired_count = var.frontend_desired_count
|
|
779
|
+
launch_type = "FARGATE"
|
|
780
|
+
|
|
781
|
+
enable_execute_command = true
|
|
782
|
+
|
|
783
|
+
network_configuration {
|
|
784
|
+
subnets = aws_subnet.public[*].id
|
|
785
|
+
security_groups = [aws_security_group.frontend.id]
|
|
786
|
+
assign_public_ip = true
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
load_balancer {
|
|
790
|
+
target_group_arn = aws_lb_target_group.frontend.arn
|
|
791
|
+
container_name = "frontend"
|
|
792
|
+
container_port = local.frontend_port
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
health_check_grace_period_seconds = 60
|
|
796
|
+
|
|
797
|
+
deployment_circuit_breaker {
|
|
798
|
+
enable = true
|
|
799
|
+
rollback = true
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
depends_on = [aws_lb_listener.http, aws_lb_listener.https]
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
# One-off migration task — `dude iac migrate --env <env>` runs this with
|
|
806
|
+
# `aws ecs run-task` after each deploy. Same image + env + secrets as the
|
|
807
|
+
# backend, but the command is the migrate management command (the image has
|
|
808
|
+
# no ENTRYPOINT, so this fully replaces the gunicorn CMD).
|
|
809
|
+
resource "aws_ecs_task_definition" "migrate" {
|
|
810
|
+
family = "${local.name}-migrate"
|
|
811
|
+
requires_compatibilities = ["FARGATE"]
|
|
812
|
+
network_mode = "awsvpc"
|
|
813
|
+
cpu = var.backend_cpu
|
|
814
|
+
memory = var.backend_memory
|
|
815
|
+
execution_role_arn = aws_iam_role.execution.arn
|
|
816
|
+
task_role_arn = aws_iam_role.task.arn
|
|
817
|
+
|
|
818
|
+
container_definitions = jsonencode([
|
|
819
|
+
{
|
|
820
|
+
name = "migrate"
|
|
821
|
+
image = "${data.aws_ecr_repository.backend.repository_url}:${var.image_tag}"
|
|
822
|
+
essential = true
|
|
823
|
+
command = ["/app/.venv/bin/python", "manage.py", "migrate", "--noinput"]
|
|
824
|
+
|
|
825
|
+
environment = local.backend_environment
|
|
826
|
+
secrets = local.backend_secrets
|
|
827
|
+
|
|
828
|
+
logConfiguration = {
|
|
829
|
+
logDriver = "awslogs"
|
|
830
|
+
options = {
|
|
831
|
+
awslogs-group = aws_cloudwatch_log_group.backend.name
|
|
832
|
+
awslogs-region = var.region
|
|
833
|
+
awslogs-stream-prefix = "migrate"
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
])
|
|
838
|
+
}
|
|
839
|
+
{{#if withCelery}}
|
|
840
|
+
|
|
841
|
+
# Celery worker — same backend image, no ALB exposure.
|
|
842
|
+
resource "aws_ecs_task_definition" "worker" {
|
|
843
|
+
family = "${local.name}-worker"
|
|
844
|
+
requires_compatibilities = ["FARGATE"]
|
|
845
|
+
network_mode = "awsvpc"
|
|
846
|
+
cpu = var.worker_cpu
|
|
847
|
+
memory = var.worker_memory
|
|
848
|
+
execution_role_arn = aws_iam_role.execution.arn
|
|
849
|
+
task_role_arn = aws_iam_role.task.arn
|
|
850
|
+
|
|
851
|
+
container_definitions = jsonencode([
|
|
852
|
+
{
|
|
853
|
+
name = "worker"
|
|
854
|
+
image = "${data.aws_ecr_repository.backend.repository_url}:${var.image_tag}"
|
|
855
|
+
essential = true
|
|
856
|
+
command = ["/app/.venv/bin/celery", "-A", "config", "worker", "--loglevel=info"]
|
|
857
|
+
|
|
858
|
+
environment = local.backend_environment
|
|
859
|
+
secrets = local.backend_secrets
|
|
860
|
+
|
|
861
|
+
logConfiguration = {
|
|
862
|
+
logDriver = "awslogs"
|
|
863
|
+
options = {
|
|
864
|
+
awslogs-group = aws_cloudwatch_log_group.worker.name
|
|
865
|
+
awslogs-region = var.region
|
|
866
|
+
awslogs-stream-prefix = "worker"
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
])
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
resource "aws_ecs_service" "worker" {
|
|
874
|
+
name = "${local.name}-worker"
|
|
875
|
+
cluster = aws_ecs_cluster.this.id
|
|
876
|
+
task_definition = aws_ecs_task_definition.worker.arn
|
|
877
|
+
desired_count = var.worker_desired_count
|
|
878
|
+
launch_type = "FARGATE"
|
|
879
|
+
|
|
880
|
+
enable_execute_command = true
|
|
881
|
+
|
|
882
|
+
network_configuration {
|
|
883
|
+
subnets = aws_subnet.public[*].id
|
|
884
|
+
security_groups = [aws_security_group.backend.id]
|
|
885
|
+
assign_public_ip = true
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
deployment_circuit_breaker {
|
|
889
|
+
enable = true
|
|
890
|
+
rollback = true
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
{{/if}}
|
|
894
|
+
{{#if withCeleryBeat}}
|
|
895
|
+
|
|
896
|
+
# Celery beat — the scheduler. Exactly one task: beat must be a singleton or
|
|
897
|
+
# every periodic job fires once per replica.
|
|
898
|
+
resource "aws_ecs_task_definition" "beat" {
|
|
899
|
+
family = "${local.name}-beat"
|
|
900
|
+
requires_compatibilities = ["FARGATE"]
|
|
901
|
+
network_mode = "awsvpc"
|
|
902
|
+
cpu = 256
|
|
903
|
+
memory = 512
|
|
904
|
+
execution_role_arn = aws_iam_role.execution.arn
|
|
905
|
+
task_role_arn = aws_iam_role.task.arn
|
|
906
|
+
|
|
907
|
+
container_definitions = jsonencode([
|
|
908
|
+
{
|
|
909
|
+
name = "beat"
|
|
910
|
+
image = "${data.aws_ecr_repository.backend.repository_url}:${var.image_tag}"
|
|
911
|
+
essential = true
|
|
912
|
+
command = ["/app/.venv/bin/celery", "-A", "config", "beat", "--loglevel=info"]
|
|
913
|
+
|
|
914
|
+
environment = local.backend_environment
|
|
915
|
+
secrets = local.backend_secrets
|
|
916
|
+
|
|
917
|
+
logConfiguration = {
|
|
918
|
+
logDriver = "awslogs"
|
|
919
|
+
options = {
|
|
920
|
+
awslogs-group = aws_cloudwatch_log_group.beat.name
|
|
921
|
+
awslogs-region = var.region
|
|
922
|
+
awslogs-stream-prefix = "beat"
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
])
|
|
927
|
+
}
|
|
928
|
+
|
|
929
|
+
resource "aws_ecs_service" "beat" {
|
|
930
|
+
name = "${local.name}-beat"
|
|
931
|
+
cluster = aws_ecs_cluster.this.id
|
|
932
|
+
task_definition = aws_ecs_task_definition.beat.arn
|
|
933
|
+
desired_count = 1
|
|
934
|
+
launch_type = "FARGATE"
|
|
935
|
+
|
|
936
|
+
enable_execute_command = true
|
|
937
|
+
|
|
938
|
+
network_configuration {
|
|
939
|
+
subnets = aws_subnet.public[*].id
|
|
940
|
+
security_groups = [aws_security_group.backend.id]
|
|
941
|
+
assign_public_ip = true
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
deployment_circuit_breaker {
|
|
945
|
+
enable = true
|
|
946
|
+
rollback = true
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
{{/if}}
|