@cubocicloide/stack-react-fastapi 13.4.2 → 13.5.0
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/dist/index.js +2654 -329
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/templates/azure-aca/backend/Dockerfile.prod.hbs +29 -0
- package/templates/azure-aca/docs/docs/deploy.md.hbs +127 -0
- package/templates/azure-aca/frontend/Dockerfile.prod +36 -0
- package/templates/azure-aca/frontend/nginx.conf.template +47 -0
- package/templates/azure-aca/iac/README.md.hbs +286 -0
- package/templates/azure-aca/iac/_gitignore +19 -0
- package/templates/azure-aca/iac/runner/Dockerfile +51 -0
- package/templates/azure-aca/iac/terraform/bootstrap/main.tf.hbs +123 -0
- package/templates/azure-aca/iac/terraform/bootstrap/outputs.tf +24 -0
- package/templates/azure-aca/iac/terraform/bootstrap/variables.tf.hbs +23 -0
- package/templates/azure-aca/iac/terraform/environments/dev/backend.hcl.hbs +25 -0
- package/templates/azure-aca/iac/terraform/environments/dev/terraform.tfvars.hbs +69 -0
- package/templates/azure-aca/iac/terraform/main.tf.hbs +593 -0
- package/templates/azure-aca/iac/terraform/outputs.tf.hbs +72 -0
- package/templates/azure-aca/iac/terraform/providers.tf +16 -0
- package/templates/azure-aca/iac/terraform/variables.tf.hbs +200 -0
- package/templates/azure-aca/iac/terraform/versions.tf +21 -0
- package/templates/azure-aks/backend/Dockerfile.prod.hbs +29 -0
- package/templates/azure-aks/docs/docs/deploy.md.hbs +273 -0
- package/templates/azure-aks/frontend/Dockerfile.prod +32 -0
- package/templates/azure-aks/frontend/nginx.conf +20 -0
- package/templates/azure-aks/iac/README.md.hbs +472 -0
- package/templates/azure-aks/iac/_gitignore +29 -0
- package/templates/azure-aks/iac/helm/app/.helmignore +7 -0
- package/templates/azure-aks/iac/helm/app/Chart.yaml.hbs +6 -0
- package/templates/azure-aks/iac/helm/app/templates/NOTES.txt +32 -0
- package/templates/azure-aks/iac/helm/app/templates/backend-deployment.yaml +44 -0
- package/templates/azure-aks/iac/helm/app/templates/backend-service.yaml +16 -0
- package/templates/azure-aks/iac/helm/app/templates/celery-beat-deployment.yaml +34 -0
- package/templates/azure-aks/iac/helm/app/templates/celery-worker-deployment.yaml +31 -0
- package/templates/azure-aks/iac/helm/app/templates/configmap.yaml +15 -0
- package/templates/azure-aks/iac/helm/app/templates/flower-deployment.yaml +28 -0
- package/templates/azure-aks/iac/helm/app/templates/flower-service.yaml +19 -0
- package/templates/azure-aks/iac/helm/app/templates/frontend-deployment.yaml +34 -0
- package/templates/azure-aks/iac/helm/app/templates/frontend-service.yaml +16 -0
- package/templates/azure-aks/iac/helm/app/templates/helpers.tpl +90 -0
- package/templates/azure-aks/iac/helm/app/templates/hpa.yaml +23 -0
- package/templates/azure-aks/iac/helm/app/templates/ingress.yaml +35 -0
- package/templates/azure-aks/iac/helm/app/templates/migration-job.yaml +38 -0
- package/templates/azure-aks/iac/helm/app/templates/redis-deployment.yaml +33 -0
- package/templates/azure-aks/iac/helm/app/templates/redis-service.yaml +18 -0
- package/templates/azure-aks/iac/helm/app/templates/secret.yaml +17 -0
- package/templates/azure-aks/iac/helm/app/templates/serviceaccount.yaml +12 -0
- package/templates/azure-aks/iac/helm/app/values-dev.yaml.hbs +36 -0
- package/templates/azure-aks/iac/helm/app/values.yaml.hbs +147 -0
- package/templates/azure-aks/iac/runner/Dockerfile +61 -0
- package/templates/azure-aks/iac/terraform/bootstrap/main.tf.hbs +123 -0
- package/templates/azure-aks/iac/terraform/bootstrap/outputs.tf +24 -0
- package/templates/azure-aks/iac/terraform/bootstrap/variables.tf.hbs +23 -0
- package/templates/azure-aks/iac/terraform/environments/dev/backend.hcl.hbs +25 -0
- package/templates/azure-aks/iac/terraform/environments/dev/terraform.tfvars.hbs +65 -0
- package/templates/azure-aks/iac/terraform/main.tf.hbs +307 -0
- package/templates/azure-aks/iac/terraform/outputs.tf.hbs +57 -0
- package/templates/azure-aks/iac/terraform/providers.tf +35 -0
- package/templates/azure-aks/iac/terraform/variables.tf.hbs +157 -0
- package/templates/azure-aks/iac/terraform/versions.tf +29 -0
- package/templates/base/CLAUDE.md.hbs +7 -7
- package/templates/base/docs/docs/dude.md.hbs +1 -1
- package/templates/base/docs/docs/index.md.hbs +1 -1
- package/templates/base/docs/mkdocs.yml.hbs +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cubocicloide/stack-react-fastapi",
|
|
3
|
-
"version": "13.
|
|
3
|
+
"version": "13.5.0",
|
|
4
4
|
"description": "React (Vite) + FastAPI stack plugin for the dude CLI.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"openapi-typescript": "^7.13.0",
|
|
21
21
|
"pathe": "^1.1.2",
|
|
22
22
|
"yaml": "^2.6.0",
|
|
23
|
-
"@cubocicloide/dude": "0.
|
|
23
|
+
"@cubocicloide/dude": "0.18.0"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
26
|
"@types/node": "^26.2.0",
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# Production image for the backend: no --reload, no dev dependencies, app code
|
|
2
|
+
# baked in (no bind mounts in Kubernetes). Migrations run as a Helm pre-upgrade
|
|
3
|
+
# Job (`alembic upgrade head`), not on container start.
|
|
4
|
+
#
|
|
5
|
+
# docker build -f Dockerfile.prod -t <ecr>/<proj>-backend:<tag> .
|
|
6
|
+
|
|
7
|
+
FROM python:3.13-slim
|
|
8
|
+
WORKDIR /app
|
|
9
|
+
|
|
10
|
+
RUN pip install --no-cache-dir uv
|
|
11
|
+
|
|
12
|
+
# Run as an unprivileged user (trivy DS002). The user is created *before* the
|
|
13
|
+
# virtualenv is built so every layer is written with the right ownership — a
|
|
14
|
+
# trailing `chown -R /app` would instead duplicate the whole .venv into an
|
|
15
|
+
# extra layer.
|
|
16
|
+
RUN useradd -m -u 1000 appuser && chown appuser:appuser /app
|
|
17
|
+
USER appuser
|
|
18
|
+
|
|
19
|
+
COPY --chown=appuser:appuser pyproject.toml .
|
|
20
|
+
RUN uv sync --no-cache --no-dev
|
|
21
|
+
|
|
22
|
+
COPY --chown=appuser:appuser app ./app
|
|
23
|
+
{{#if withPostgres}}
|
|
24
|
+
COPY --chown=appuser:appuser alembic ./alembic
|
|
25
|
+
COPY --chown=appuser:appuser alembic.ini ./alembic.ini
|
|
26
|
+
{{/if}}
|
|
27
|
+
|
|
28
|
+
EXPOSE 8000
|
|
29
|
+
CMD ["uv", "run", "uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
# Deploy — Azure Container Apps (Terraform)
|
|
2
|
+
|
|
3
|
+
This project was scaffolded with the **Azure Container Apps** IaC target.
|
|
4
|
+
Infrastructure and the application are both described with Terraform — there is
|
|
5
|
+
no Kubernetes cluster, no kubectl and no Helm chart. Every command is scoped per
|
|
6
|
+
environment via `--env`, which is **required** (no default); run a command
|
|
7
|
+
without it to list the environments discovered under
|
|
8
|
+
`iac/terraform/environments/`.
|
|
9
|
+
|
|
10
|
+
!!! success "HTTPS with no domain"
|
|
11
|
+
Container Apps serves each externally-exposed app on
|
|
12
|
+
`https://<app>.<environment>.<region>.azurecontainerapps.io` with a
|
|
13
|
+
**certificate Microsoft issues and renews**. No domain to buy, no DNS to
|
|
14
|
+
delegate, no certificate authority to configure.
|
|
15
|
+
|
|
16
|
+
!!! info "The IaC runner (Docker)"
|
|
17
|
+
`dude iac *` runs terraform inside a pinned container built from
|
|
18
|
+
`iac/runner/Dockerfile` (yours to edit) — so you only need **Docker** and the
|
|
19
|
+
`az` CLI, not a Terraform install. Your `~/.azure` is mounted in, so your
|
|
20
|
+
login carries over. `dude iac login` stays on the host (it needs a browser).
|
|
21
|
+
Set `DUDE_IAC_RUNNER=host` to use your local tools instead.
|
|
22
|
+
|
|
23
|
+
## What gets provisioned
|
|
24
|
+
|
|
25
|
+
| Layer | Resource | Notes |
|
|
26
|
+
|-------|----------|-------|
|
|
27
|
+
| Resource group | `{{projectName}}-<env>-rg` | everything for one environment lives in it, so teardown is total |
|
|
28
|
+
| Network | Virtual network + a `/23` subnet delegated to Container Apps{{#if withPostgres}} + a delegated database subnet{{/if}} | VNet-injected so the apps can reach a private database |
|
|
29
|
+
| Logging | Log Analytics workspace | required by the environment; what `dude iac logs` reads |
|
|
30
|
+
| Platform | Container Apps environment | Consumption plan: billed per vCPU-second of running replicas |
|
|
31
|
+
| Registry | Azure Container Registry | **shared** across envs, created once by `dude iac bootstrap` |
|
|
32
|
+
| Identity | User-assigned managed identity + `AcrPull` | lets apps pull images with no registry password stored anywhere |
|
|
33
|
+
| `frontend` | container app, **public HTTPS** | nginx: serves the SPA, proxies `/api` to the backend |
|
|
34
|
+
| `backend` | container app, internal ingress | never reachable from the internet directly |
|
|
35
|
+
{{#if withCelery}}| `worker` | container app, no ingress | Celery worker; fixed replicas |
|
|
36
|
+
{{/if}}{{#if withCeleryBeat}}| `beat` | container app, no ingress | Celery Beat; exactly one replica, always |
|
|
37
|
+
{{/if}}{{#if withRedis}}| `redis` | container app, internal TCP | Celery broker + result backend |
|
|
38
|
+
{{/if}}{{#if withPostgres}}| Database | PostgreSQL Flexible Server | private (VNet-integrated); password generated by Terraform and injected as a secret |
|
|
39
|
+
| Migrations | Container Apps job | manually triggered; `dude iac deploy`/`ship` starts it and waits |
|
|
40
|
+
{{/if}}| State backend | Storage account + `tfstate` container | the blob lease **is** the state lock — no lock table |
|
|
41
|
+
|
|
42
|
+
!!! warning "Some regions are closed to new subscriptions"
|
|
43
|
+
Azure caps capacity per region and often refuses a new subscription outright
|
|
44
|
+
— West Europe is the common case, failing with `RequestDisallowedByAzure`.
|
|
45
|
+
The scaffold defaults to **North Europe**, which is reliably open.
|
|
46
|
+
|
|
47
|
+
## Deploy, in order
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
# 1. Sign in and confirm which subscription this environment uses
|
|
51
|
+
dude iac login --env dev
|
|
52
|
+
|
|
53
|
+
# 2. One-time per project: shared resource group, Terraform state storage
|
|
54
|
+
# account and container registry. The prefix makes those names globally
|
|
55
|
+
# unique — use your org slug, lowercase letters/digits only.
|
|
56
|
+
dude iac bootstrap --state-prefix <yourorg> --env dev --yes
|
|
57
|
+
|
|
58
|
+
# 3. Initialise Terraform against the remote backend
|
|
59
|
+
dude iac init --env dev
|
|
60
|
+
|
|
61
|
+
# 4. Build, push, provision{{#if withPostgres}} and migrate{{/if}} — in one step
|
|
62
|
+
dude iac ship --env dev
|
|
63
|
+
|
|
64
|
+
# 5. Find the URL
|
|
65
|
+
dude iac output --env dev # app_url
|
|
66
|
+
dude iac status --env dev # every app, its state and revision
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
> **`ship` before `apply`, on a first deploy.** Container Apps validates that an
|
|
70
|
+
> image exists when it creates an app — unlike a Kubernetes Deployment, which is
|
|
71
|
+
> accepted and then sits in ImagePullBackOff. Applying before anything has been
|
|
72
|
+
> published therefore fails outright, so `ship` builds and pushes first and then
|
|
73
|
+
> applies. `dude iac apply` checks for this and says so, rather than letting
|
|
74
|
+
> Azure return an error that never mentions the real cause. Afterwards the two
|
|
75
|
+
> separate: `apply` for infrastructure, `ship`/`deploy` for code.
|
|
76
|
+
|
|
77
|
+
## Watching it run
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
dude iac status --env dev # all apps at a glance
|
|
81
|
+
dude iac logs --env dev --service backend --follow # stream one app
|
|
82
|
+
dude iac logs --env dev --service worker # …or any other
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
The services available to `--service` depend on how the project was scaffolded,
|
|
86
|
+
so `logs` reads the deployed set from Azure and lists them if you name one that
|
|
87
|
+
does not exist.
|
|
88
|
+
|
|
89
|
+
## Image tags
|
|
90
|
+
|
|
91
|
+
`dude iac deploy`/`ship` writes `image_tag` into the environment's
|
|
92
|
+
`terraform.tfvars` and applies. The deployed version is therefore a **committed,
|
|
93
|
+
reviewable fact** — not an imperative change the next `terraform apply` would
|
|
94
|
+
silently roll back. Commit that line after a release.
|
|
95
|
+
|
|
96
|
+
| Working tree | Tag |
|
|
97
|
+
|--------------|-----|
|
|
98
|
+
| clean | `<git-short-sha>` |
|
|
99
|
+
| uncommitted changes | `<git-short-sha>-dirty-<hash-of-the-diff>` |
|
|
100
|
+
|
|
101
|
+
To promote an image between environments rather than rebuilding it:
|
|
102
|
+
`dude iac deploy --env staging --tag <sha>`.
|
|
103
|
+
|
|
104
|
+
## Scaling, and scaling to zero
|
|
105
|
+
|
|
106
|
+
Container Apps bills per vCPU-second and GiB-second of *running* replicas. Set a
|
|
107
|
+
service's `min_replicas` to `0` in the environment's `terraform.tfvars` and it
|
|
108
|
+
costs nothing while idle, at the cost of a cold start on the next request. The
|
|
109
|
+
defaults keep the frontend and backend at 1; in a dev environment
|
|
110
|
+
`backend_min_replicas = 0` is often the right trade.
|
|
111
|
+
|
|
112
|
+
The allowed cpu/memory pairs are fixed by the platform: `0.25`/`0.5Gi`,
|
|
113
|
+
`0.5`/`1Gi`, `0.75`/`1.5Gi`, `1`/`2Gi`, and so on. A mismatched pair fails the
|
|
114
|
+
apply.
|
|
115
|
+
|
|
116
|
+
## Tear down
|
|
117
|
+
|
|
118
|
+
```bash
|
|
119
|
+
dude iac destroy --env dev
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Destroys the environment's whole resource group, then the shared bootstrap —
|
|
123
|
+
but only when no other environment still uses it, judged from each sibling's
|
|
124
|
+
remote Terraform state rather than its folder on disk.
|
|
125
|
+
|
|
126
|
+
See `iac/README.md` for the full command reference, the troubleshooting table,
|
|
127
|
+
and how the apps reach each other.
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# Production image for the frontend: build the Vite app, serve the static bundle
|
|
2
|
+
# with nginx, and proxy /api to the backend. Used by the Container App (port
|
|
3
|
+
# 8080). The dev Dockerfile (Vite dev server) is kept for `dude up`.
|
|
4
|
+
#
|
|
5
|
+
# The config is a TEMPLATE, not a finished conf: nginx's entrypoint runs envsubst
|
|
6
|
+
# over /etc/nginx/templates/ at start-up, which is how BACKEND_URL — known only
|
|
7
|
+
# once Terraform has created the backend app — gets in without rebuilding.
|
|
8
|
+
#
|
|
9
|
+
# VITE_* values are baked in at build time — pass them as build args:
|
|
10
|
+
# docker build -f Dockerfile.prod --build-arg VITE_APP_TITLE="My App" -t <ecr>/<proj>-frontend:<tag> .
|
|
11
|
+
|
|
12
|
+
FROM node:22-alpine AS build
|
|
13
|
+
WORKDIR /app
|
|
14
|
+
|
|
15
|
+
RUN npm install -g pnpm@9
|
|
16
|
+
|
|
17
|
+
COPY package.json pnpm-lock.yaml* ./
|
|
18
|
+
RUN pnpm install --frozen-lockfile || pnpm install
|
|
19
|
+
|
|
20
|
+
ARG VITE_APP_TITLE=app
|
|
21
|
+
ARG VITE_REACT_QUERY_DEVTOOLS=false
|
|
22
|
+
ENV VITE_APP_TITLE=$VITE_APP_TITLE
|
|
23
|
+
ENV VITE_REACT_QUERY_DEVTOOLS=$VITE_REACT_QUERY_DEVTOOLS
|
|
24
|
+
|
|
25
|
+
COPY . .
|
|
26
|
+
RUN pnpm build
|
|
27
|
+
|
|
28
|
+
# nginx-unprivileged is the stock nginx image rebuilt to run as UID 101 with no
|
|
29
|
+
# root master process (trivy DS002). Because it cannot bind a privileged port,
|
|
30
|
+
# it listens on 8080 — see nginx.conf and `frontend.port` in the Helm values.
|
|
31
|
+
FROM nginxinc/nginx-unprivileged:1.27-alpine
|
|
32
|
+
COPY --from=build /app/dist /usr/share/nginx/html
|
|
33
|
+
COPY nginx.conf.template /etc/nginx/templates/default.conf.template
|
|
34
|
+
USER nginx
|
|
35
|
+
EXPOSE 8080
|
|
36
|
+
CMD ["nginx", "-g", "daemon off;"]
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# SPA routing + API proxy.
|
|
2
|
+
#
|
|
3
|
+
# On Container Apps there is no shared ingress doing path-based routing: the
|
|
4
|
+
# frontend is the one externally reachable app, so it proxies /api to the
|
|
5
|
+
# backend itself. BACKEND_URL is injected by Terraform as an environment
|
|
6
|
+
# variable and substituted into this file at container start — the stock nginx
|
|
7
|
+
# entrypoint runs envsubst over /etc/nginx/templates/*.template. Only real
|
|
8
|
+
# environment variables are substituted, so nginx's own $uri/$host survive.
|
|
9
|
+
server {
|
|
10
|
+
# 8080, not 80: the image runs unprivileged and cannot bind a port < 1024.
|
|
11
|
+
listen 8080;
|
|
12
|
+
server_name _;
|
|
13
|
+
root /usr/share/nginx/html;
|
|
14
|
+
index index.html;
|
|
15
|
+
|
|
16
|
+
location /api {
|
|
17
|
+
proxy_pass ${BACKEND_URL};
|
|
18
|
+
proxy_http_version 1.1;
|
|
19
|
+
|
|
20
|
+
# Host MUST stay the upstream's own hostname ($proxy_host, nginx's
|
|
21
|
+
# default). Container Apps routes internal traffic by Host header, so
|
|
22
|
+
# forwarding the browser's Host here makes the platform unable to tell
|
|
23
|
+
# which app the request is for and answer 404 from its own edge — which
|
|
24
|
+
# looks exactly like a missing route in the backend.
|
|
25
|
+
proxy_set_header Host $proxy_host;
|
|
26
|
+
|
|
27
|
+
# The original host and scheme travel in the X-Forwarded-* headers
|
|
28
|
+
# instead; the external edge terminates TLS, so without X-Forwarded-Proto
|
|
29
|
+
# the backend builds http:// links in redirects and in the OpenAPI
|
|
30
|
+
# server URL.
|
|
31
|
+
proxy_set_header X-Forwarded-Host $host;
|
|
32
|
+
proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
|
|
33
|
+
proxy_set_header X-Real-IP $remote_addr;
|
|
34
|
+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
35
|
+
proxy_read_timeout 120s;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
location / {
|
|
39
|
+
try_files $uri $uri/ /index.html;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
# Long-cache hashed assets.
|
|
43
|
+
location /assets/ {
|
|
44
|
+
expires 1y;
|
|
45
|
+
add_header Cache-Control "public, immutable";
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
# Infrastructure as Code — {{projectName}} on Azure Container Apps
|
|
2
|
+
|
|
3
|
+
Deploys {{projectName}} to **Azure Container Apps** with **Terraform**.
|
|
4
|
+
Everything is environment-scoped and driven by `dude iac …`, which works
|
|
5
|
+
identically on your laptop and in CI/CD.
|
|
6
|
+
|
|
7
|
+
**If you just want it deployed, follow [Part 1](#part-1-first-deploy-start-to-finish)
|
|
8
|
+
top to bottom.** The rest of this file is reference.
|
|
9
|
+
|
|
10
|
+
There is no Kubernetes here: no cluster, no kubectl, no Helm chart. Terraform
|
|
11
|
+
describes the container apps directly, and the platform runs them.
|
|
12
|
+
|
|
13
|
+
```
|
|
14
|
+
iac/
|
|
15
|
+
├── terraform/
|
|
16
|
+
│ ├── versions.tf # providers + azurerm backend (partial)
|
|
17
|
+
│ ├── providers.tf # azurerm
|
|
18
|
+
│ ├── variables.tf # all inputs (sane defaults)
|
|
19
|
+
│ ├── main.tf # resource group + VNet + Container Apps env + apps{{#if withPostgres}} + PostgreSQL{{/if}}
|
|
20
|
+
│ ├── outputs.tf # app URL, registry URLs{{#if withPostgres}}, database{{/if}}
|
|
21
|
+
│ ├── bootstrap/ # one-time shared infra: state storage account + container registry
|
|
22
|
+
│ └── environments/
|
|
23
|
+
│ └── dev/
|
|
24
|
+
│ ├── backend.hcl # remote-state location for this env
|
|
25
|
+
│ └── terraform.tfvars # this env's sizing/knobs + the deployed image tag
|
|
26
|
+
└── runner/
|
|
27
|
+
└── Dockerfile # the toolchain `dude iac` runs in — yours to edit
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
## Part 1 — first deploy, start to finish
|
|
33
|
+
|
|
34
|
+
### Step 0 — install two things
|
|
35
|
+
|
|
36
|
+
| Tool | Why | Install |
|
|
37
|
+
| ---- | --- | ------- |
|
|
38
|
+
| **Docker** | builds the application images; also runs terraform for you | [docs.docker.com](https://docs.docker.com/get-docker/) |
|
|
39
|
+
| **Azure CLI** (`az`) | signing in needs a browser, which a container doesn't have | `brew install azure-cli` · [other platforms](https://learn.microsoft.com/cli/azure/install-azure-cli) |
|
|
40
|
+
|
|
41
|
+
You do **not** need terraform on your machine.
|
|
42
|
+
|
|
43
|
+
### Step 1 — have the right access in Azure
|
|
44
|
+
|
|
45
|
+
An Azure subscription where your account can **create resource groups**
|
|
46
|
+
(*Contributor*) and **assign roles** (*User Access Administrator*). *Owner*
|
|
47
|
+
covers both. The role assignment matters because the container apps are granted
|
|
48
|
+
`AcrPull` on the registry — that is what lets them pull images without storing a
|
|
49
|
+
registry password on each app.
|
|
50
|
+
|
|
51
|
+
On a brand-new subscription, register the resource providers this scaffold uses
|
|
52
|
+
(free, once per subscription, a couple of minutes to propagate):
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
for p in App OperationalInsights Network Storage ContainerRegistry \
|
|
56
|
+
DBforPostgreSQL ManagedIdentity Authorization; do
|
|
57
|
+
az provider register --namespace Microsoft.$p
|
|
58
|
+
done
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
> Container Apps needs **no VM quota**, which is what makes this target work on
|
|
62
|
+
> restricted subscriptions where a Kubernetes cluster cannot be created at all.
|
|
63
|
+
|
|
64
|
+
### Step 2 — sign in for this environment
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
dude iac login --env dev
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Runs `az login` if needed, then prints the subscription `dev` will deploy into
|
|
71
|
+
and the id to pin it with. Pin it — an unpinned environment silently follows
|
|
72
|
+
whatever `az account set` was run last:
|
|
73
|
+
|
|
74
|
+
```hcl
|
|
75
|
+
# iac/terraform/environments/dev/terraform.tfvars
|
|
76
|
+
subscription_id = "00000000-0000-0000-0000-000000000000"
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### Step 3 — create the shared resources (once per project)
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
dude iac bootstrap --state-prefix <yourorg> --location northeurope --env dev --yes
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Creates, in `{{projectName}}-shared-rg`: a **storage account** + `tfstate`
|
|
86
|
+
container holding the Terraform state for every environment, and an **Azure
|
|
87
|
+
Container Registry** holding the images for every environment. Then it writes
|
|
88
|
+
their names into `environments/dev/{backend.hcl,terraform.tfvars}` for you.
|
|
89
|
+
|
|
90
|
+
`--state-prefix` makes those names globally unique across Azure; they allow only
|
|
91
|
+
lowercase letters and digits. Run this **once per project**, not per environment.
|
|
92
|
+
|
|
93
|
+
### Step 4 — initialise Terraform
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
dude iac init --env dev
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### Step 5 — build, push and provision, in one step
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
dude iac ship --env dev
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
**Not `apply` first.** Container Apps validates that an image exists when it
|
|
106
|
+
creates an app — unlike Kubernetes, which accepts the app and leaves it in
|
|
107
|
+
ImagePullBackOff — so applying before anything is published fails outright.
|
|
108
|
+
`ship` builds both images, pushes them, records the tag and *then* applies, which
|
|
109
|
+
is the order that works on a first deploy. `dude iac apply` checks this for you
|
|
110
|
+
and says so rather than letting Azure produce an error that never mentions the
|
|
111
|
+
real problem.
|
|
112
|
+
|
|
113
|
+
Use `dude iac plan --env dev` to review the infrastructure first if you like; it
|
|
114
|
+
does not need the images.
|
|
115
|
+
|
|
116
|
+
Expect a few minutes.{{#if withPostgres}} The PostgreSQL Flexible Server is most
|
|
117
|
+
of it.{{/if}} What you get: a resource group, a VNet, a Log Analytics workspace,
|
|
118
|
+
a Container Apps environment, and one container app per service — frontend,
|
|
119
|
+
backend{{#if withCelery}}, Celery worker{{/if}}{{#if withCeleryBeat}}, Celery
|
|
120
|
+
beat{{/if}}{{#if withRedis}}, Redis{{/if}}{{#if withPostgres}} — plus a private
|
|
121
|
+
PostgreSQL Flexible Server, a migration job, and the migrations themselves run
|
|
122
|
+
and waited for{{/if}}.
|
|
123
|
+
|
|
124
|
+
After the first deploy, the two halves separate: `dude iac apply` for
|
|
125
|
+
infrastructure changes, `dude iac ship` (or `deploy`) to release new code.
|
|
126
|
+
|
|
127
|
+
### Step 6 — open it
|
|
128
|
+
|
|
129
|
+
```bash
|
|
130
|
+
dude iac output --env dev # look for app_url
|
|
131
|
+
dude iac status --env dev # every app, its state and revision
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
```
|
|
135
|
+
https://{{projectName}}-dev-frontend.<random>.northeurope.azurecontainerapps.io/
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
**HTTPS, with a certificate Microsoft issues and renews** — no domain to buy, no
|
|
139
|
+
DNS to delegate, no certificate authority to configure. The API is served under
|
|
140
|
+
`/api`, proxied by the frontend to the backend over the environment's internal
|
|
141
|
+
network.
|
|
142
|
+
|
|
143
|
+
If something is not up:
|
|
144
|
+
|
|
145
|
+
```bash
|
|
146
|
+
dude iac logs --env dev --service backend --follow
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
---
|
|
150
|
+
|
|
151
|
+
## How the pieces fit
|
|
152
|
+
|
|
153
|
+
| Component | Exposure | Notes |
|
|
154
|
+
| --------- | -------- | ----- |
|
|
155
|
+
| `frontend` | **public HTTPS** | nginx: serves the SPA and proxies `/api` to the backend |
|
|
156
|
+
| `backend` | internal only | reachable from inside the environment, never from the internet |
|
|
157
|
+
{{#if withCelery}}| `worker` | none | pulls from the broker; nothing connects to it |
|
|
158
|
+
{{/if}}{{#if withCeleryBeat}}| `beat` | none | exactly one replica — a second would fire every periodic task twice |
|
|
159
|
+
{{/if}}{{#if withRedis}}| `redis` | internal TCP | Celery broker + result backend |
|
|
160
|
+
{{/if}}{{#if withPostgres}}| PostgreSQL | private (VNet) | no public endpoint; `psql` from your laptop will not connect |
|
|
161
|
+
| migration job | n/a | manually triggered, runs `alembic upgrade head` |
|
|
162
|
+
{{/if}}
|
|
163
|
+
|
|
164
|
+
Apps in one Container Apps environment resolve each other **by app name**, which
|
|
165
|
+
is why the frontend proxies to `http://{{projectName}}-<env>-backend` with no
|
|
166
|
+
FQDN or service-discovery wiring.
|
|
167
|
+
|
|
168
|
+
### Why the frontend proxies instead of the SPA calling the API directly
|
|
169
|
+
|
|
170
|
+
There is no shared ingress doing path-based routing here — each app gets its own
|
|
171
|
+
hostname. Exposing the backend publicly too would mean a second URL baked into
|
|
172
|
+
the frontend bundle at build time, plus CORS. Proxying keeps one origin, so the
|
|
173
|
+
browser sees a single site and the backend stays off the internet.
|
|
174
|
+
|
|
175
|
+
## Image tags, and why they are committed
|
|
176
|
+
|
|
177
|
+
`dude iac deploy`/`ship` writes `image_tag` into the environment's
|
|
178
|
+
`terraform.tfvars` and applies. That makes the deployed version a **reviewed,
|
|
179
|
+
committed fact** rather than something imperative that the next `terraform
|
|
180
|
+
apply` would silently roll back.
|
|
181
|
+
|
|
182
|
+
| Working tree | Tag |
|
|
183
|
+
|--------------|-----|
|
|
184
|
+
| clean | `<git-short-sha>` |
|
|
185
|
+
| uncommitted changes | `<git-short-sha>-dirty-<hash-of-the-diff>` |
|
|
186
|
+
|
|
187
|
+
Commit the `image_tag` change after a release. Pushing a tag that already exists
|
|
188
|
+
**overwrites** it — a Basic-tier registry does not enforce tag immutability — so
|
|
189
|
+
`push`/`ship` warn before doing it.
|
|
190
|
+
|
|
191
|
+
## Cost, and scaling to zero
|
|
192
|
+
|
|
193
|
+
Container Apps bills per vCPU-second and GiB-second of *running* replicas. Set a
|
|
194
|
+
service's `min_replicas` to `0` and it costs nothing while idle, at the price of
|
|
195
|
+
a cold start on the next request. The defaults keep the frontend and backend at
|
|
196
|
+
1 — a visitor should not wait for a container to boot — while everything else is
|
|
197
|
+
sized down. In a dev environment you may well want `backend_min_replicas = 0`.
|
|
198
|
+
|
|
199
|
+
{{#if withPostgres}}The database is the opposite: a Flexible Server bills while it
|
|
200
|
+
exists, whether or not anything connects. Stop it from the portal, or destroy the
|
|
201
|
+
environment, when you are not using it.
|
|
202
|
+
|
|
203
|
+
{{/if}}## Add a new environment
|
|
204
|
+
|
|
205
|
+
```bash
|
|
206
|
+
dude iac new-env --env prod # copies dev by default; --from <env> to pick another
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
Scaffolds `environments/prod/{backend.hcl,terraform.tfvars}` with
|
|
210
|
+
`environment = "prod"`, a per-env state `key`, and `image_tag` reset to `latest`
|
|
211
|
+
(a new environment must not come up serving whatever the source env last
|
|
212
|
+
deployed). The storage account, state container **and registry** are shared, so
|
|
213
|
+
you do not bootstrap again.
|
|
214
|
+
|
|
215
|
+
```bash
|
|
216
|
+
dude iac init --env prod && dude iac apply --env prod --yes && dude iac ship --env prod
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
## Local vs CI/CD
|
|
220
|
+
|
|
221
|
+
The same commands run in CI — state is remote and auth is the Azure CLI/SDK
|
|
222
|
+
chain. Authenticate a pipeline with a service principal or workload identity
|
|
223
|
+
federation by exporting `ARM_CLIENT_ID`, `ARM_TENANT_ID`, `ARM_SUBSCRIPTION_ID`
|
|
224
|
+
and either `ARM_CLIENT_SECRET` or OIDC. Pass an explicit `--tag $CI_COMMIT_SHA`
|
|
225
|
+
so the pipeline controls provenance.
|
|
226
|
+
|
|
227
|
+
## Tear down
|
|
228
|
+
|
|
229
|
+
```bash
|
|
230
|
+
dude iac destroy --env dev
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
Destroys the environment's whole resource group, re-checks that it is really
|
|
234
|
+
gone (a delete still in flight, or a resource lock, otherwise leaves it quietly
|
|
235
|
+
billing), then tears down the shared bootstrap — but **only if no other
|
|
236
|
+
environment still uses it**, judged from each sibling's remote Terraform state
|
|
237
|
+
rather than its folder on disk.
|
|
238
|
+
|
|
239
|
+
| Flag | Effect |
|
|
240
|
+
| ---------------- | ------------------------------------------------------------------- |
|
|
241
|
+
| `--keep-backend` | Keep the shared state storage account **+ registry**. |
|
|
242
|
+
| `--skip-tf` | Skip the per-env destroy — only run the bootstrap teardown. |
|
|
243
|
+
|
|
244
|
+
## Commands
|
|
245
|
+
|
|
246
|
+
| Command | Wraps |
|
|
247
|
+
|---------|-------|
|
|
248
|
+
| `dude iac login` | `az login` (when needed) + `az account show` for the env's subscription |
|
|
249
|
+
| `dude iac new-env` | scaffolds `environments/<name>/` from an existing env |
|
|
250
|
+
| `dude iac bootstrap` | `terraform init/apply` in `bootstrap/`, patches `backend.hcl` + `terraform.tfvars` |
|
|
251
|
+
| `dude iac init` | `terraform init` (azurerm backend per env) |
|
|
252
|
+
| `dude iac plan` | `terraform plan` |
|
|
253
|
+
| `dude iac apply` | `terraform apply` (`--yes` to auto-approve) |
|
|
254
|
+
| `dude iac destroy` | `terraform destroy` + bootstrap teardown (`--yes`, `--keep-backend`, `--skip-tf`) |
|
|
255
|
+
| `dude iac output` | `terraform output` (`--json`) |
|
|
256
|
+
| `dude iac fmt` | `terraform fmt -recursive` |
|
|
257
|
+
| `dude iac validate` | `terraform validate` |
|
|
258
|
+
| `dude iac build` | `docker build` BE+FE prod images (`linux/amd64`) |
|
|
259
|
+
| `dude iac push` | registry token login + `docker push` BE+FE |
|
|
260
|
+
| `dude iac deploy` | record `image_tag` + `terraform apply`{{#if withPostgres}} + run migrations{{/if}} |
|
|
261
|
+
| `dude iac ship` | `build` + `push` + `deploy` |
|
|
262
|
+
| `dude iac status` | every container app: state, revision, public URL |
|
|
263
|
+
| `dude iac logs` | `az containerapp logs show` (`--service`, `--tail`, `--follow`) |
|
|
264
|
+
{{#if withPostgres}}| `dude iac migrate` | starts the migration job and waits for it |
|
|
265
|
+
{{/if}}| `dude iac shell` | interactive shell in the IaC runner container (terraform, az) |
|
|
266
|
+
|
|
267
|
+
All commands take `--env <name>` (**required**) and `--subscription <id>`.
|
|
268
|
+
`build`/`push`/`deploy`/`ship` take `--tag`; `build`/`ship` also `--platform`
|
|
269
|
+
(default `linux/amd64`). `bootstrap` accepts `--state-prefix` (required) and
|
|
270
|
+
`--location` (default `northeurope`).
|
|
271
|
+
|
|
272
|
+
## Troubleshooting
|
|
273
|
+
|
|
274
|
+
| Symptom | Cause / fix |
|
|
275
|
+
| ------- | ----------- |
|
|
276
|
+
| `apply` fails on `azurerm_role_assignment.acr_pull` with *AuthorizationFailed* | Your account can't assign roles. Ask for **User Access Administrator** (or Owner). |
|
|
277
|
+
| `apply` fails: *RequestDisallowedByAzure … region is currently not accepting new customers* | That region is capacity-closed to your subscription. Use another: `--location` **and** `location` in the env tfvars. |
|
|
278
|
+
| `apply` fails: subnet too small | The Container Apps subnet must be at least a `/23`. Widen `aca_subnet_cidr`. |
|
|
279
|
+
| `bootstrap` fails: storage account name taken | The name is global across Azure. Re-run with a different `--state-prefix`. |
|
|
280
|
+
| `init` fails: *ListBlobs … 404* | A brand-new storage account's blob endpoint takes a few minutes. `bootstrap` waits for it; if you hit this anyway, re-run `init` a minute later. |
|
|
281
|
+
| `apply` fails: *InvalidParameterValueInContainerTemplate … MANIFEST_UNKNOWN* | The configured `image_tag` is not in the registry. Container Apps refuses to create an app it cannot pull. Run `dude iac ship --env <env>`, which builds and pushes first. |
|
|
282
|
+
| Apps stuck, logs show image pull errors | The `AcrPull` role assignment may not have propagated yet (a few minutes). |
|
|
283
|
+
| `502` from the public URL | The backend is not up. `dude iac logs --env <env> --service backend`. |
|
|
284
|
+
{{#if withPostgres}}| `psql` from your laptop can't connect | By design: the database has no public endpoint and lives inside the VNet. |
|
|
285
|
+
{{/if}}| `destroy` removes the environment but leaves the shared resource group | The bootstrap's Terraform state is **local and gitignored**, so it exists only where `dude iac bootstrap` ran. A fresh clone or a re-scaffold starts empty and has nothing to destroy. Remove them directly: `az group delete --name <project>-shared-rg --yes`. Keep that state file (or the machine) if you want Terraform to own the teardown. |
|
|
286
|
+
| `exec format error` in an app | An arm64 image on x86 compute. Rebuild without overriding `--platform`. |
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# Terraform
|
|
2
|
+
**/.terraform/*
|
|
3
|
+
*.tfstate
|
|
4
|
+
*.tfstate.*
|
|
5
|
+
crash.log
|
|
6
|
+
crash.*.log
|
|
7
|
+
*.tfvars.local
|
|
8
|
+
override.tf
|
|
9
|
+
override.tf.json
|
|
10
|
+
*_override.tf
|
|
11
|
+
*_override.tf.json
|
|
12
|
+
.terraformrc
|
|
13
|
+
terraform.rc
|
|
14
|
+
.terraform.lock.hcl
|
|
15
|
+
|
|
16
|
+
# Azure
|
|
17
|
+
# `az ... --file` output — never a committed file.
|
|
18
|
+
kubeconfig
|
|
19
|
+
*.kubeconfig
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# IaC runner image — the toolchain `dude iac *` runs inside.
|
|
2
|
+
#
|
|
3
|
+
# `dude iac` builds this image on first use and runs terraform inside it, so you
|
|
4
|
+
# don't have to install (or version-match) it on your machine. The image is
|
|
5
|
+
# tagged by a hash of THIS file: edit it (bump a version, add a tool) and the
|
|
6
|
+
# next `dude iac` command rebuilds automatically.
|
|
7
|
+
#
|
|
8
|
+
# Lighter than the Kubernetes target's runner on purpose: Container Apps has no
|
|
9
|
+
# cluster, so there is no kubectl, helm or k9s to carry. Terraform and the Azure
|
|
10
|
+
# CLI are the whole toolchain.
|
|
11
|
+
#
|
|
12
|
+
# Your Azure credentials are NOT baked in — `dude iac` mounts ~/.azure at run
|
|
13
|
+
# time, so `az login` on your machine carries into the container. That mount is
|
|
14
|
+
# read-WRITE on purpose: the Azure CLI rewrites its token cache whenever it
|
|
15
|
+
# refreshes an access token, and a read-only mount breaks authentication as soon
|
|
16
|
+
# as the cached token expires. `dude iac login` runs on the host (it needs a
|
|
17
|
+
# browser); everything else runs in here.
|
|
18
|
+
#
|
|
19
|
+
# TARGETARCH is set automatically by Docker BuildKit (amd64 or arm64).
|
|
20
|
+
|
|
21
|
+
FROM debian:bookworm-slim
|
|
22
|
+
ARG TARGETARCH
|
|
23
|
+
ENV DEBIAN_FRONTEND=noninteractive
|
|
24
|
+
|
|
25
|
+
ARG TF_VERSION=1.10.5
|
|
26
|
+
|
|
27
|
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
|
28
|
+
curl unzip ca-certificates bash less git gnupg lsb-release \
|
|
29
|
+
&& rm -rf /var/lib/apt/lists/*
|
|
30
|
+
|
|
31
|
+
# terraform
|
|
32
|
+
RUN curl -fsSL --retry 5 --retry-delay 2 --retry-connrefused \
|
|
33
|
+
"https://releases.hashicorp.com/terraform/${TF_VERSION}/terraform_${TF_VERSION}_linux_${TARGETARCH}.zip" -o /tmp/tf.zip \
|
|
34
|
+
&& unzip -q /tmp/tf.zip -d /usr/local/bin && rm /tmp/tf.zip
|
|
35
|
+
|
|
36
|
+
# azure cli — installed from Microsoft's apt repository rather than pip, because
|
|
37
|
+
# the deb ships arm64 builds and keeps its own Python out of the way.
|
|
38
|
+
RUN install -m 0755 -d /etc/apt/keyrings \
|
|
39
|
+
&& curl -fsSL --retry 5 --retry-delay 2 --retry-connrefused https://packages.microsoft.com/keys/microsoft.asc \
|
|
40
|
+
| gpg --dearmor -o /etc/apt/keyrings/microsoft.gpg \
|
|
41
|
+
&& chmod a+r /etc/apt/keyrings/microsoft.gpg \
|
|
42
|
+
&& echo "deb [arch=${TARGETARCH} signed-by=/etc/apt/keyrings/microsoft.gpg] https://packages.microsoft.com/repos/azure-cli/ $(lsb_release -cs) main" \
|
|
43
|
+
> /etc/apt/sources.list.d/azure-cli.list \
|
|
44
|
+
&& apt-get update && apt-get install -y --no-install-recommends azure-cli \
|
|
45
|
+
&& rm -rf /var/lib/apt/lists/*
|
|
46
|
+
|
|
47
|
+
# The `containerapp` extension is what `dude iac status/logs/migrate` drive.
|
|
48
|
+
RUN az extension add --name containerapp --only-show-errors \
|
|
49
|
+
&& az config set extension.use_dynamic_install=yes_without_prompt --only-show-errors
|
|
50
|
+
|
|
51
|
+
WORKDIR /work
|