跳到內容
AggregaAggregaAggregaAggrega
選擇語言: 繁體中文
本頁為機器翻譯。閱讀英文原文。

搭配 PostgreSQL 的網頁應用程式,一次一步地建立起來

一個把筆記保存在 PostgreSQL 中的小型網頁應用程式,分四個步驟逐步成長:先從你機器上的資料庫開始,接著加入 Redis 快取,再搬到 AWS 並把資料庫放在 Amazon RDS 上,最後加上備份。每個步驟都是一個完整的獨立工作區,位於 1/ 到 4/ 目錄,且每一個都能直接建置。

每個 aggregate.yaml 都以一行註解開頭,指出它所屬的步驟,release.yaml 或 manifest.yaml 在該步驟有更動時也是如此。把檔案與前一步驟的比較,就能確切看出該步驟增加了什麼:

Terminal window
diff 1/aggregate.yaml 2/aggregate.yaml

應用程式是 hub.aggrega.ai/tools/quickstart-web:1.1.0,採用 Apache-2.0 授權。它只從環境讀取設定:PostgreSQL 用 PGHOST、PGPORT、PGUSER、PGPASSWORD 和 PGDATABASE,快取用 REDIS_HOST 和 REDIS_PORT,而且只有在設定了 REDIS_HOST 時才會使用快取。它監聽連接埠 8080,而每個關於筆記的回答都會在 source 欄位中說明該筆記是從哪裡讀取的,所以從外部就能看出快取。

此頁面上的輸出區塊複製自真實執行,依據閱讀這些頁面上的輸出。

你需要什麼

  • aggrega CLI,每個步驟都需要。
  • Docker 與 Compose,用於在你機器上執行的 1/ 和 2/。
  • 3/ 和 4/ 不需要其他東西:它們建置 Terraform 檔案就停止。每個步驟的第一次建置會從 git 存放庫取得 RDS 和 ElastiCache 模組。規劃並套用這些檔案是另一件事,要在預留位置被取代之後,以你自己的 AWS 帳戶與認證進行。

用一個命令執行

你可以用一個命令執行第一個步驟,或是讓你的 AI 助理為你撰寫並建置。

先為資料庫選一個密碼並匯出。在 Linux 或 macOS 上:

Terminal window
export QUICKSTART_DB_PASSWORD=choose-a-password
curl -fsSL https://aggrega.ai/quickstart-web.sh | sh

在 Windows 的 PowerShell 中:

Terminal window
$env:QUICKSTART_DB_PASSWORD = 'choose-a-password'
irm https://aggrega.ai/quickstart-web.ps1 | iex

密碼必須匯出,因為 Docker Compose 會從你的 shell 讀取它,而資料庫會保留它啟動時使用的第一個密碼,所以第二個步驟需要同一個密碼。沒有它時,指令碼會拒絕啟動。

指令碼會說明它要做什麼,並在繼續之前詢問。它會檢查能否找到 aggrega,以及 Docker 是否有回應並具備 Compose。它把範例寫入資料夾 quickstart-web,或在該資料夾已存在時直接使用。接著它建置第一個步驟,並以 Docker Compose 啟動。指令碼最後會印出應用程式的頁面 http://127.0.0.1:8080/。

若要執行其他步驟,請指定它:2、3 或 4;步驟 3 和 4 只建置。在 Linux 或 macOS 上,把它放在 sh -s -- 之後,並在那裡加上 --yes,即可不經詢問直接進行:

Terminal window
curl -fsSL https://aggrega.ai/quickstart-web.sh | sh -s -- 2

在 PowerShell 中,先把它設定在 QUICKSTART_STEP。它會在該視窗中一直保持設定,供之後每次執行使用,直到你以 Remove-Item Env:QUICKSTART_STEP 移除為止:

Terminal window
$env:QUICKSTART_STEP=2; irm https://aggrega.ai/quickstart-web.ps1 | iex

草擬範例

如果你想一步一步建置並執行,請把範例寫入以它命名的新資料夾:

Terminal window
aggrega draft --example quickstart-web

它接著會印出要接著執行的命令。第一個會切換到新資料夾:

Terminal window
cd quickstart-web

建置

以下命令從這個範例的目錄啟動第一個步驟。密碼由你自行選擇:Docker Compose 會在堆疊啟動時從你的 shell 讀取它,所以沒有任何檔案保存它。

Terminal window
cd 1
aggrega build --release release.yaml --auto-pull
export QUICKSTART_DB_PASSWORD=choose-a-password
docker compose -p quickstart-web -f dist/compose/docker-compose.yaml up -d --wait
# then open http://127.0.0.1:8080/ in your browser

建置會寫出一個檔案 dist/compose/docker-compose.yaml,而 docker compose 會在專案名稱 quickstart-web 之下啟動它,並在兩個服務都啟動後回傳。下面每一節都往前再走一步。

搭配 AI 助理

以 Aggrega 的工具設定好的 AI 助理可以做同樣的事:把這個提示貼給它。

請只使用 Aggrega 工具,且不要自行建立任何資料夾。如果工作區資料夾中還沒有 `quickstart-web` 資料夾,請呼叫 `draft` 工具一次,將 `example` 設為 `quickstart-web`,且不帶 `dir`:它會把範例寫入新的 `quickstart-web` 資料夾,含一個 README 與步驟資料夾 `1` 到 `4`。接著呼叫 `build` 工具,將 `workspace_root` 設為 `quickstart-web/1`、`release` 設為 `release.yaml`、`auto_pull` 設為 true、`write` 設為 true。告訴我建置寫出了哪些檔案;它們位於 `quickstart-web/1/dist/`。

網頁應用程式與它的 PostgreSQL——1/

這個 Aggregate 宣告兩個元件:web,型別為 core/compute/server,以及 db,型別為 core/datastore/postgres。應用程式在一條邊上以它所讀取的名稱要求資料庫:

uses:
database:
db:
target: db
inject:
env:
- in: PGHOST
from: host
- in: PGPORT
from: port
- in: PGUSER
from: user
- in: PGDATABASE
from: database

密碼透過兩端各自的 core/secret-value 附加項目傳到兩端,其來源是 Compose 變數參照 ${QUICKSTART_DB_PASSWORD}。Release 指名映像檔(以其多平台索引的摘要固定)、它監聽的連接埠 port: 8080,以及 published_port: 8080,也就是在你機器上為它發布的連接埠;沒有 published_port 時,那裡不會發布任何東西。dist/compose/docker-compose.yaml 中的 web 服務:

web:
image: hub.aggrega.ai/tools/quickstart-web:1.1.0@sha256:72d42d6db51d34d1268108eca1989e1467ccb2c66d15ed3e50ca97e09003c8fb
environment:
PGDATABASE: appdb
PGHOST: db
PGPASSWORD: ${QUICKSTART_DB_PASSWORD}
PGPORT: "5432"
PGUSER: appuser
ports:
- 127.0.0.1:8080:8080
[… omitted: the aggrega.ai/ labels every service carries]
networks:
- compose
depends_on:
- db
deploy:
resources:
limits:
cpus: "0.1"
memory: "134217728"
reservations:
cpus: "0.1"
memory: "134217728"

PGHOST 是 db,也就是資料庫在 Compose 網路上的服務名稱,而連接埠只發布在你機器的回送位址上。

在同一台機器上用瀏覽器開啟 http://127.0.0.1:8080/,即可看到應用程式的頁面。它會指出應用程式所連接的資料庫與快取,並讓你新增筆記並讀取。每次讀取都會說明它來自哪裡,「from PostgreSQL」或「from the cache」。在還沒有連接快取時,頁面會顯示「No cache is wired to the app, so every read comes from PostgreSQL.」

在終端機中,詢問應用程式它連接到什麼,然後儲存一則筆記並讀回來:

Terminal window
curl -s http://localhost:8080/
curl -s -X POST --data 'hello from the quickstart' http://localhost:8080/notes
curl -s http://localhost:8080/notes/1
{
"cache": "not wired",
"database": "postgresql at db:5432/appdb",
"service": "quickstart-web"
}
{
"id": 1,
"text": "hello from the quickstart",
"created_at": "2026-11-01T17:22:34.261989Z",
"source": "postgresql"
}
{
"id": 1,
"text": "hello from the quickstart",
"created_at": "2026-11-01T17:22:34.261989Z",
"source": "postgresql"
}

加入快取——2/

這個 Aggregate 加入一個型別為 core/datastore/redis 的 cache 元件,以及應用程式上的另一條邊:

cache:
cache:
target: cache
inject:
env:
- in: REDIS_HOST
from: host
- in: REDIS_PORT
from: port

Manifest 把新的型別對應到 compose/redis,而 Release 放置新的元件。在同一個專案名稱之下建置並啟動它,且仍匯出同一個密碼:

Terminal window
cd ../2
aggrega build --release release.yaml --auto-pull
docker compose -p quickstart-web -f dist/compose/docker-compose.yaml up -d --wait

Docker Compose 讓資料庫繼續執行,以兩個新變數重新建立應用程式,並啟動快取。資料庫是用你在 1/ 匯出的密碼建立的,並會保留它,所以這個步驟請使用相同的值。在 1/ 儲存的筆記仍然在;請讀兩次:

Terminal window
curl -s http://localhost:8080/
curl -s http://localhost:8080/notes/1
curl -s http://localhost:8080/notes/1
{
"cache": "cache:6379",
"database": "postgresql at db:5432/appdb",
"service": "quickstart-web"
}
{
"id": 1,
"text": "hello from the quickstart",
"created_at": "2026-11-01T17:22:34.261989Z",
"source": "postgresql"
}
{
"id": 1,
"text": "hello from the quickstart",
"created_at": "2026-11-01T17:22:34.261989Z",
"source": "cache"
}

第一次讀取來自 PostgreSQL 並填入快取;第二次來自快取,快取會把筆記保留五分鐘。在頁面上,把筆記讀兩次會先顯示「from PostgreSQL」,再顯示「from the cache」。

完成堆疊的使用後,移除它,連同資料庫的資料:

Terminal window
docker compose -p quickstart-web -f dist/compose/docker-compose.yaml down -v

同一個資料庫,在 AWS 上受管理——3/

同樣的三個元件,在 AWS 上,透過 Terraform。Manifest 把伺服器對應到 terraform/server-ecs,這是 Fargate 上的 Amazon ECS 服務;把 PostgreSQL 對應到 terraform/postgres-rds-terraform-aws-modules,它 vendor 了 terraform-aws-modules 專案已發布的 RDS 模組;把 Redis 對應到 terraform/redis-elasticache,它 vendor 了其 ElastiCache 模組。Aggregate 中的邊不會改變。

這個 Aggregate 移除的是兩個密碼附加項目:RDS 會自己產生資料庫的密碼,所以沒有任何建置輸入帶著密碼。在 AWS 上,RDS 把密碼保存在 AWS Secrets Manager 中,而快速入門應用程式只從 PGPASSWORD 讀取它的密碼,所以在你提供之前無法登入。

Release 在 infra.overrides 之下指名你的帳戶中已經在執行的東西:ECS 叢集、工作所附加的子網路與安全群組、ECS 用來下載映像檔並寫入記錄的執行角色,以及 RDS 與 ElastiCache 的子網路群組。它也把快取的 engine_version 設為 7.1,因為 ElastiCache 最高只執行到 Redis OSS 7.1。這些值每一個都是預留位置:123456789012 是 AWS 文件在範例中使用的帳戶編號,而 subnet-… 和 sg-… ID、子網路群組、叢集與角色,代表你自己帳戶中的資源。建置之所以接受它們,是因為它從不查詢它們;請在規劃之前先替換。

Terminal window
cd ../3
aggrega build --release release.yaml --auto-pull

根設定 dist/terraform/aggrega.modules.tf:

# Root configuration for this boundary. Each component rendered here is one
# module block below, and the directory that block points at holds that
# component's own files. One configuration means one dependency graph, which is
# what lets a reference or a depends_on reach from one component to another.
module "cache" {
source = "./modules/cache"
create_security_group = false
create_subnet_group = false
engine = "redis"
engine_version = "7.1"
node_type = "cache.t3.small"
port = 6379
replication_group_id = "cache"
security_group_ids = ["sg-0123456789abcdef1"]
subnet_group_name = "example-private-cache-subnets"
transit_encryption_enabled = false
}
module "db" {
source = "./modules/db"
allocated_storage = 10
backup_retention_period = 7
create_db_subnet_group = false
db_name = "appdb"
db_subnet_group_name = "example-private-db-subnets"
engine = "postgres"
engine_version = "16"
family = "postgres16"
identifier = "db"
instance_class = "db.t3.small"
manage_master_user_password = true
multi_az = false
port = "5432"
skip_final_snapshot = false
storage_type = "gp3"
username = "appuser"
vpc_security_group_ids = ["sg-0123456789abcdef0"]
}
module "web" {
source = "./modules/web"
assign_public_ip = false
cluster = "example-cluster"
cpu = "256"
desired_count = 1
environment = {"PGDATABASE":"appdb","PGHOST":"${module.db.db_instance_address}","PGPORT":"5432","PGUSER":"appuser","REDIS_HOST":"${module.cache.replication_group_primary_endpoint_address}","REDIS_PORT":"6379"}
execution_role_arn = "arn:aws:iam::123456789012:role/example-ecs-task-execution"
image = "hub.aggrega.ai/tools/quickstart-web:1.1.0@sha256:72d42d6db51d34d1268108eca1989e1467ccb2c66d15ed3e50ca97e09003c8fb"
memory = "512"
name = "web"
port = 8080
security_groups = ["sg-0fedcba9876543210"]
subnet_ids = ["subnet-0123456789abcdef0","subnet-0fedcba9876543210"]
}

應用程式收到與 Compose 上相同的名稱。PGHOST 和 REDIS_HOST 現在是對 RDS 與 ElastiCache 模組自己輸出的參照,Terraform 會在套用設定時解析它們,因為在執行個體與快取建立之前,這兩個位址都不存在;編譯器把兩者傳遞過去而不讀取它們。在 dist/terraform 的副本上,tofu init -backend=false 會載入根設定與 vendored 模組,並且只取得提供者,接著 tofu validate 會印出:

Success! The configuration is valid.

加入備份——4/

在沒有任何設定另行指定時,RDS 會把執行個體的自動備份保留 7 天,備份時段由它自行選擇——就是上方的 backup_retention_period = 7。Release 把 core/backup 附加到資料庫:

db:
features:
backup:
type: core/backup
infra:
retention_days: 14
schedule: "30 2 * * *"

而 Manifest 把該 Feature 繫結到 terraform/backup-native,它自己不渲染任何東西:備份是執行個體的一項設定,所以資料庫 Recipe 讀取附加項目,並把它寫在執行個體的模組區塊上。RDS 每天進行一次自動備份,所以排程會在該時間開啟一個 30 分鐘的時段,以 UTC 計。Aggregate 不會改變。

Terminal window
cd ../4
aggrega build --release release.yaml --auto-pull

根設定與 3/ 的根設定只有 db 模組區塊不同,對它執行 tofu validate 同樣會印出 Success! The configuration is valid.:

backup_retention_period = 7
backup_retention_period = 14
backup_window = "02:30-03:00"

建置不是部署

3/ 與 4/ 會在 dist/ 底下寫入檔案後就停止。建置從不執行 Terraform 或 OpenTofu,從不向 AWS 進行身分驗證,也不查詢這些 Releases 所指名的任何識別碼。規劃與套用是你在輸出上自行進行的步驟,使用你自己的認證,並在替換預留位置之後進行。

檢查每個步驟

在文件存放庫的簽出副本中,有一個共用的建置輔助程式,是 aggrega draft --example 寫出的副本所不含的;此目錄中的 ./check.sh 會建置這四個步驟,並檢查每個步驟是否寫出本頁所示的內容:兩個 Compose 檔案及其服務與變數,以及兩個 Terraform 根設定及其模組區塊、參照與備份設定。它為每個步驟印出一行,只要有任何步驟不同,就以非零的結束代碼結束。dist/、.aggrega/ 與 *.lock 檔案是產生出來的,不會提交;它們會由已提交的檔案重新產生。