Skip to content

Commit 86b4c1e

Browse files
authored
Merge pull request #783 from j5ik2o/fix/lambda-rmu-on-localstack
fix: Lambda RMU on LocalStack with DynamoDB Streams
2 parents ca6f91d + 8452498 commit 86b4c1e

31 files changed

Lines changed: 615 additions & 61 deletions

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ jobs:
5858
- uses: pnpm/action-setup@v3
5959
with:
6060
version: 8
61+
- uses: unfor19/install-aws-cli-action@v1
62+
with:
63+
version: "2"
6164
- name: Use Node.js ${{ matrix.node-version }}
6265
uses: actions/setup-node@v4
6366
with:

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ yarn-error.log
1919
.turbo/
2020
.turbo/*
2121

22+
# Local developer overrides (see common.env.default)
23+
common.env
24+

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "references/okite-ai"]
2+
path = references/okite-ai
3+
url = git@github.com:j5ik2o/okite-ai.git

README.ja.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
- [x] Read API Server(GraphQL)
2020
- [x] Read Model Updater on Local
2121
- [x] Docker Compose Support
22-
- [ ] Read Model Updater on AWS Lambda
22+
- [x] Read Model Updater on AWS Lambda(LocalStack 向けビルド/デプロイと `common.env`。詳細は [ビルドとテスト](./docs/BUILD_AND_TEST.ja.md)
2323
- [ ] Deployment to AWS
2424

2525
## 概要

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Please refer to [here](https://github.com/j5ik2o/cqrs-es-example) for implementa
2222
- [x] Read API Server(GraphQL)
2323
- [x] Read Model Updater on Local
2424
- [x] Docker Compose Support
25-
- [ ] Read Model Updater on AWS Lambda
25+
- [x] Read Model Updater on AWS Lambda (LocalStack: build/deploy scripts and `common.env`; see [Build and Test](./docs/BUILD_AND_TEST.md))
2626
- [ ] Deployment to AWS
2727

2828
## Component Composition

common.env.default

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copy to common.env and adjust. common.env is gitignored.
2+
# Host-side AWS CLI uses this URL (Compose maps LocalStack to host port 34566).
3+
LOCALSTACK_ENDPOINT_URL=http://localhost:34566
4+
5+
AWS_REGION=ap-northeast-1
6+
AWS_DEFAULT_REGION=ap-northeast-1
7+
AWS_ACCESS_KEY_ID=x
8+
AWS_SECRET_ACCESS_KEY=x
9+
10+
STREAM_JOURNAL_TABLE_NAME=journal
11+
STREAM_MAX_ITEM_COUNT=32
12+
13+
# Lambda container reaches MySQL on the Compose network (see docker-compose-databases.yml).
14+
DATABASE_URL_LAMBDA=mysql://ceer:ceer@mysql-local:3306/ceer
15+
16+
FUNCTION_NAME=read-model-updater-rmu
17+
# LocalStack 2.x では nodejs20.x が無い場合があるため既定は nodejs18.x
18+
LAMBDA_RUNTIME=nodejs18.x
19+
COMPOSE_PROJECT=cqrs-es-example-js
20+
21+
# 1 = after deploy, stop container read-model-updater-1 to avoid double stream processing
22+
STOP_LOCAL_RMU_AFTER_LAMBDA_DEPLOY=1
23+
24+
# docker-compose-up.sh: build/deploy Lambda zip when non-zero
25+
DOCKER_COMPOSE_UP_BUILD_LAMBDA=1
26+
DOCKER_COMPOSE_UP_DEPLOY_LAMBDA=1
27+
28+
# Override if your compose project name differs (affects LAMBDA_DOCKER_NETWORK default in compose).
29+
# LAMBDA_DOCKER_NETWORK=cqrs-es-example-js_default

docs/BUILD_AND_TEST.ja.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,25 @@ docker composeで起動したアプリケーションに対してE2Eテストを
2626
$ pnpm docker-build
2727
$ pnpm docker-compose-up
2828
$ pnpm verify-group-chat # E2E
29-
```
29+
```
30+
31+
## Read Model Updater Lambda(LocalStack)
32+
33+
Rust 版と同様に、Read Model Updater を LocalStack 上の Lambda(DynamoDB ストリーム → Lambda → MySQL)として動かす手順です。
34+
35+
1. リポジトリルートに `common.env.default``common.env` としてコピーするか、`docker-compose-up.sh` / `docker-compose-e2e-test.sh` に任せて自動生成する。`common.env``.gitignore` されます。
36+
2. ホストから AWS CLI で LocalStack に接続するときは **`http://localhost:34566`** を使います(Compose がコンテナの `4566` をホストの `34566` に公開)。Compose 内のサービスからは `http://localstack:4566` です。
37+
3. デプロイ用 zip(Docker `linux/amd64`、Prisma の Lambda 用バイナリを含む)のビルド:
38+
39+
```shell
40+
$ pnpm build-read-model-updater-lambda
41+
```
42+
43+
成果物は `dist/lambda/read-model-updater/function.zip` です。
44+
45+
4. `pnpm docker-compose-up` および `pnpm docker-compose-e2e-test` は、必要に応じて zip をビルドし、LocalStack 待機後に `deploy-read-model-updater-localstack.sh` を実行し、既定でコンテナ `read-model-updater-1` を停止します(ストリームの二重処理を防ぐため)。`DOCKER_COMPOSE_UP_DEPLOY_LAMBDA=0``DOCKER_COMPOSE_UP_BUILD_LAMBDA=0` でスキップできます。E2E 用コンテナは `profile: e2e` とし、スタック起動と Lambda デプロイの後に `verify-group-chat.sh` が走るようにしています。
46+
5. Compose 起動後に手動デプロイする場合:
47+
48+
```shell
49+
$ pnpm deploy-read-model-updater-localstack
50+
```

docs/BUILD_AND_TEST.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,24 @@ $ pnpm docker-build
2727
$ pnpm docker-compose-up
2828
$ pnpm verify-group-chat # E2E
2929
```
30+
31+
## Read Model Updater Lambda (LocalStack)
32+
33+
This mirrors the Rust example: run the Read Model Updater as an AWS Lambda on LocalStack (DynamoDB stream → Lambda → MySQL).
34+
35+
1. Copy `common.env.default` to `common.env` at the repo root, or let `docker-compose-up.sh` / `docker-compose-e2e-test.sh` create it. `common.env` is gitignored.
36+
2. From the host, point the AWS CLI at **`http://localhost:34566`** (Compose maps container `4566` to host `34566`). Inside Compose, services use `http://localstack:4566`.
37+
3. Build the deployment zip (Docker `linux/amd64`, Prisma engines for Lambda):
38+
39+
```shell
40+
$ pnpm build-read-model-updater-lambda
41+
```
42+
43+
Output: `dist/lambda/read-model-updater/function.zip`.
44+
45+
4. `pnpm docker-compose-up` and `pnpm docker-compose-e2e-test` can build the zip (unless `DOCKER_COMPOSE_UP_BUILD_LAMBDA=0`), wait for LocalStack, run `deploy-read-model-updater-localstack.sh`, and by default stop the `read-model-updater-1` container to avoid double-processing the stream. Set `DOCKER_COMPOSE_UP_DEPLOY_LAMBDA=0` to skip deploy. The e2e script brings stacks up, deploys Lambda, then runs the `e2e-test` container (`profile: e2e`) so GraphQL is ready before `verify-group-chat.sh` runs.
46+
5. To deploy manually after Compose is up:
47+
48+
```shell
49+
$ pnpm deploy-read-model-updater-localstack
50+
```

mise.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[tools]
2+
node = "20"
3+
python = "3"
4+
"npm:@fission-ai/openspec" = "1.2.0"
5+
"npm:happy-coder" = "0.13.0"

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
"docker-compose-up": "./tools/scripts/docker-compose-up.sh",
1212
"docker-compose-up-db": "./tools/scripts/docker-compose-up.sh -d",
1313
"docker-compose-down": "./tools/scripts/docker-compose-down.sh",
14+
"build-read-model-updater-lambda": "./tools/scripts/build-read-model-updater-lambda.sh",
15+
"deploy-read-model-updater-localstack": "./tools/scripts/deploy-read-model-updater-localstack.sh",
1416
"create-group-chat": "./tools/scripts/curl-create-group-chat.sh",
1517
"verify-group-chat": "./tools/e2e-test/verify-group-chat.sh",
1618
"build": "turbo build",

0 commit comments

Comments
 (0)