@aws/nx-plugin-mcp 1.10.0 → 1.12.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/bin/aws-nx-mcp.js +70 -60
- package/docs/guides/fastapi.mdx +230 -3
- package/docs/guides/react-website-auth.mdx +7 -0
- package/docs/guides/react-website.mdx +55 -0
- package/docs/guides/trpc.mdx +5 -0
- package/docs/guides/ts-verified-access.mdx +103 -0
- package/generators.json +8 -0
- package/package.json +1 -1
- package/src/py/api/schema.json +11 -4
- package/src/py/fast-api/schema.json +11 -4
- package/src/ts/react-website/app/schema.json +11 -0
- package/src/ts/verified-access/schema.json +27 -0
package/bin/aws-nx-mcp.js
CHANGED
|
@@ -25,12 +25,14 @@ var __exportAll = (all, flags) => {
|
|
|
25
25
|
return flags & 2 ? target : Object.preventExtensions(target);
|
|
26
26
|
};
|
|
27
27
|
var __copyProps = (to, from, except, desc) => {
|
|
28
|
-
if (from && typeof from === "object" || typeof from === "function")
|
|
29
|
-
key
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
28
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
29
|
+
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
30
|
+
desc = __getOwnPropDesc(from, key);
|
|
31
|
+
__defProp(to, key, {
|
|
32
|
+
get: desc && desc.get && !desc.configurable ? desc.get : () => from[key],
|
|
33
|
+
enumerable: !desc || desc.enumerable
|
|
34
|
+
});
|
|
35
|
+
}
|
|
34
36
|
}
|
|
35
37
|
return to;
|
|
36
38
|
};
|
|
@@ -22613,67 +22615,67 @@ const NX_VERSION = {
|
|
|
22613
22615
|
"deepmerge-ts": "8.0.2",
|
|
22614
22616
|
mysql2: "3.24.6",
|
|
22615
22617
|
effect: "3.22.5",
|
|
22616
|
-
"@aws-sdk/client-dynamodb": "3.
|
|
22617
|
-
"@aws-sdk/client-api-gateway": "3.
|
|
22618
|
-
"@aws-sdk/client-iam": "3.
|
|
22619
|
-
"@aws-sdk/client-bedrock-agentcore": "3.
|
|
22620
|
-
"@aws-sdk/client-bedrock-runtime": "3.
|
|
22621
|
-
"@aws-sdk/client-greengrassv2": "3.
|
|
22622
|
-
"@aws-sdk/client-s3": "3.
|
|
22623
|
-
"@aws-sdk/client-sts": "3.
|
|
22624
|
-
"@aws-sdk/client-cognito-identity-provider": "3.
|
|
22625
|
-
"@aws-sdk/credential-providers": "3.
|
|
22626
|
-
"@aws-sdk/credential-provider-cognito-identity": "3.972.
|
|
22627
|
-
"@aws-sdk/client-secrets-manager": "3.
|
|
22628
|
-
"@aws-sdk/rds-signer": "3.
|
|
22618
|
+
"@aws-sdk/client-dynamodb": "3.1179.0",
|
|
22619
|
+
"@aws-sdk/client-api-gateway": "3.1179.0",
|
|
22620
|
+
"@aws-sdk/client-iam": "3.1179.0",
|
|
22621
|
+
"@aws-sdk/client-bedrock-agentcore": "3.1179.0",
|
|
22622
|
+
"@aws-sdk/client-bedrock-runtime": "3.1179.0",
|
|
22623
|
+
"@aws-sdk/client-greengrassv2": "3.1179.0",
|
|
22624
|
+
"@aws-sdk/client-s3": "3.1179.0",
|
|
22625
|
+
"@aws-sdk/client-sts": "3.1179.0",
|
|
22626
|
+
"@aws-sdk/client-cognito-identity-provider": "3.1179.0",
|
|
22627
|
+
"@aws-sdk/credential-providers": "3.1179.0",
|
|
22628
|
+
"@aws-sdk/credential-provider-cognito-identity": "3.972.85",
|
|
22629
|
+
"@aws-sdk/client-secrets-manager": "3.1179.0",
|
|
22630
|
+
"@aws-sdk/rds-signer": "3.1179.0",
|
|
22629
22631
|
"@smithy/server-apigateway": "0.3.5",
|
|
22630
22632
|
"@smithy/server-node": "0.3.5",
|
|
22631
|
-
"@aws-lambda-powertools/logger": "2.
|
|
22632
|
-
"@aws-lambda-powertools/metrics": "2.
|
|
22633
|
-
"@aws-lambda-powertools/parameters": "2.
|
|
22634
|
-
"@aws-lambda-powertools/tracer": "2.
|
|
22635
|
-
"@aws-lambda-powertools/parser": "2.
|
|
22636
|
-
"@aws-sdk/client-appconfigdata": "3.
|
|
22633
|
+
"@aws-lambda-powertools/logger": "2.37.0",
|
|
22634
|
+
"@aws-lambda-powertools/metrics": "2.37.0",
|
|
22635
|
+
"@aws-lambda-powertools/parameters": "2.37.0",
|
|
22636
|
+
"@aws-lambda-powertools/tracer": "2.37.0",
|
|
22637
|
+
"@aws-lambda-powertools/parser": "2.37.0",
|
|
22638
|
+
"@aws-sdk/client-appconfigdata": "3.1179.0",
|
|
22637
22639
|
"@middy/core": "7.9.2",
|
|
22638
|
-
"@nxlv/python": "23.
|
|
22640
|
+
"@nxlv/python": "23.6.0",
|
|
22639
22641
|
"@nx-extend/terraform": "10.4.1",
|
|
22640
|
-
nx: "24.0
|
|
22641
|
-
"@nx/devkit": "24.0
|
|
22642
|
-
"@nx/js": "24.0
|
|
22643
|
-
"@nx/react": "24.0
|
|
22644
|
-
"@nx/vite": "24.0
|
|
22645
|
-
"@nx/vitest": "24.0
|
|
22646
|
-
"@nx/workspace": "24.0
|
|
22647
|
-
"create-nx-workspace": "24.0
|
|
22642
|
+
nx: "24.1.0",
|
|
22643
|
+
"@nx/devkit": "24.1.0",
|
|
22644
|
+
"@nx/js": "24.1.0",
|
|
22645
|
+
"@nx/react": "24.1.0",
|
|
22646
|
+
"@nx/vite": "24.1.0",
|
|
22647
|
+
"@nx/vitest": "24.1.0",
|
|
22648
|
+
"@nx/workspace": "24.1.0",
|
|
22649
|
+
"create-nx-workspace": "24.1.0",
|
|
22648
22650
|
"@swc-node/register": "1.12.1",
|
|
22649
22651
|
"@swc/core": "1.16.22",
|
|
22650
22652
|
"@modelcontextprotocol/sdk": "1.32.2",
|
|
22651
22653
|
"@modelcontextprotocol/client": "2.4.0",
|
|
22652
|
-
"@modelcontextprotocol/inspector": "2.
|
|
22654
|
+
"@modelcontextprotocol/inspector": "2.17.0",
|
|
22653
22655
|
"@ag-ui/a2ui-toolkit": "0.0.5",
|
|
22654
22656
|
"@ag-ui/aws-strands": "1.0.2",
|
|
22655
22657
|
"@ag-ui/client": "1.0.5",
|
|
22656
22658
|
"@ag-ui/core": "1.0.5",
|
|
22657
22659
|
"@ag-ui/encoder": "1.0.5",
|
|
22658
22660
|
"agent-chat-cli": "0.3.0",
|
|
22659
|
-
"@copilotkit/react-core": "1.90.
|
|
22661
|
+
"@copilotkit/react-core": "1.90.2",
|
|
22660
22662
|
rxjs: "7.8.2",
|
|
22661
|
-
"@strands-agents/sdk": "1.
|
|
22663
|
+
"@strands-agents/sdk": "1.27.0",
|
|
22662
22664
|
"@tanstack/react-router": "1.170.46",
|
|
22663
|
-
"@tanstack/router-plugin": "1.
|
|
22664
|
-
"@tanstack/router-generator": "1.
|
|
22665
|
+
"@tanstack/router-plugin": "1.170.0",
|
|
22666
|
+
"@tanstack/router-generator": "1.169.0",
|
|
22665
22667
|
"@tanstack/virtual-file-routes": "1.162.0",
|
|
22666
22668
|
"@tanstack/router-utils": "1.163.0",
|
|
22667
|
-
"@cloudscape-design/board-components": "3.0.
|
|
22668
|
-
"@cloudscape-design/chat-components": "1.0.
|
|
22669
|
-
"@cloudscape-design/components": "3.0.
|
|
22669
|
+
"@cloudscape-design/board-components": "3.0.252",
|
|
22670
|
+
"@cloudscape-design/chat-components": "1.0.197",
|
|
22671
|
+
"@cloudscape-design/components": "3.0.1428",
|
|
22670
22672
|
"@cloudscape-design/global-styles": "1.0.72",
|
|
22671
|
-
"@tanstack/react-query": "5.108.
|
|
22672
|
-
"@tanstack/react-query-devtools": "5.108.
|
|
22673
|
-
"@trpc/tanstack-react-query": "11.
|
|
22674
|
-
"@trpc/client": "11.
|
|
22675
|
-
"@trpc/server": "11.
|
|
22676
|
-
"@types/node": "26.6.
|
|
22673
|
+
"@tanstack/react-query": "5.108.4",
|
|
22674
|
+
"@tanstack/react-query-devtools": "5.108.4",
|
|
22675
|
+
"@trpc/tanstack-react-query": "11.32.2",
|
|
22676
|
+
"@trpc/client": "11.32.2",
|
|
22677
|
+
"@trpc/server": "11.32.2",
|
|
22678
|
+
"@types/node": "26.6.11",
|
|
22677
22679
|
"@types/aws-lambda": "8.10.164",
|
|
22678
22680
|
"@types/cors": "2.8.19",
|
|
22679
22681
|
"@types/js-yaml": "4.0.9",
|
|
@@ -22684,10 +22686,10 @@ const NX_VERSION = {
|
|
|
22684
22686
|
"@smithy/node-config-provider": "4.6.2",
|
|
22685
22687
|
"@smithy/node-http-handler": "4.14.2",
|
|
22686
22688
|
"@smithy/types": "4.20.0",
|
|
22687
|
-
"@vitest/coverage-v8": "5.1.
|
|
22688
|
-
"@vitest/ui": "5.1.
|
|
22689
|
+
"@vitest/coverage-v8": "5.1.2",
|
|
22690
|
+
"@vitest/ui": "5.1.2",
|
|
22689
22691
|
"@astrojs/react": "6.0.6",
|
|
22690
|
-
"@astrojs/starlight": "0.43.
|
|
22692
|
+
"@astrojs/starlight": "0.43.2",
|
|
22691
22693
|
astro: "7.4.13",
|
|
22692
22694
|
cookie: "2.0.2",
|
|
22693
22695
|
aws4fetch: "1.0.20",
|
|
@@ -22705,7 +22707,7 @@ const NX_VERSION = {
|
|
|
22705
22707
|
esbuild: "0.28.2",
|
|
22706
22708
|
"event-source-polyfill": "1.0.31",
|
|
22707
22709
|
"@types/event-source-polyfill": "1.0.5",
|
|
22708
|
-
"@biomejs/biome": "2.6.
|
|
22710
|
+
"@biomejs/biome": "2.6.9",
|
|
22709
22711
|
"@prisma/adapter-mariadb": "7.10.1",
|
|
22710
22712
|
"@prisma/adapter-pg": "7.10.1",
|
|
22711
22713
|
"@prisma/client": "7.10.1",
|
|
@@ -22718,7 +22720,7 @@ const NX_VERSION = {
|
|
|
22718
22720
|
"@types/fs-extra": "11.0.4",
|
|
22719
22721
|
"js-yaml": "4.3.2",
|
|
22720
22722
|
mariadb: "3.5.5",
|
|
22721
|
-
mise: "2026.11.
|
|
22723
|
+
mise: "2026.11.15",
|
|
22722
22724
|
npm: "12.2.3",
|
|
22723
22725
|
"npm-check-updates": "23.1.1",
|
|
22724
22726
|
"oidc-client-ts": "3.5.0",
|
|
@@ -22727,25 +22729,25 @@ const NX_VERSION = {
|
|
|
22727
22729
|
"react-oidc-context": "3.3.1",
|
|
22728
22730
|
react: "19.3.1",
|
|
22729
22731
|
"react-dom": "19.3.1",
|
|
22730
|
-
rolldown: "1.3.
|
|
22732
|
+
rolldown: "1.3.4",
|
|
22731
22733
|
"rolldown-plugin-dts": "0.30.2",
|
|
22732
22734
|
shx: "0.4.0",
|
|
22733
22735
|
"simple-git": "4.1.0",
|
|
22734
22736
|
"source-map-support": "0.5.21",
|
|
22735
|
-
"starlight-blog": "0.
|
|
22737
|
+
"starlight-blog": "0.31.0",
|
|
22736
22738
|
tailwindcss: "4.3.5",
|
|
22737
22739
|
"@tailwindcss/vite": "4.3.5",
|
|
22738
22740
|
tsx: "4.23.15",
|
|
22739
|
-
"lucide-react": "1.
|
|
22741
|
+
"lucide-react": "1.84.0",
|
|
22740
22742
|
"radix-ui": "1.8.1",
|
|
22741
|
-
shadcn: "4.23.
|
|
22743
|
+
shadcn: "4.23.1",
|
|
22742
22744
|
"tw-animate-css": "1.4.0",
|
|
22743
|
-
vite: "8.
|
|
22745
|
+
vite: "8.4.0",
|
|
22744
22746
|
typescript: "6.0.3",
|
|
22745
|
-
vitest: "5.1.
|
|
22747
|
+
vitest: "5.1.2",
|
|
22746
22748
|
jsdom: "29.1.1",
|
|
22747
22749
|
zod: "4.6.7",
|
|
22748
|
-
ws: "8.23.
|
|
22750
|
+
ws: "8.23.1"
|
|
22749
22751
|
}.nx;
|
|
22750
22752
|
// ../nx-plugin/src/utils/commands.ts
|
|
22751
22753
|
/**
|
|
@@ -56273,6 +56275,14 @@ const generatorsJsonEntries = {
|
|
|
56273
56275
|
"guidePages": ["react-website-auth"],
|
|
56274
56276
|
"hidden": true
|
|
56275
56277
|
},
|
|
56278
|
+
"ts#verified-access": {
|
|
56279
|
+
"factory": "./src/ts/verified-access/generator",
|
|
56280
|
+
"schema": "./src/ts/verified-access/schema.json",
|
|
56281
|
+
"description": "Put an AWS Verified Access door in front of a private REST API",
|
|
56282
|
+
"metric": "g90",
|
|
56283
|
+
"experimental": true,
|
|
56284
|
+
"guidePages": ["ts-verified-access"]
|
|
56285
|
+
},
|
|
56276
56286
|
"ts#react-website#runtime-config": {
|
|
56277
56287
|
"factory": "./src/ts/react-website/runtime-config/generator",
|
|
56278
56288
|
"schema": "./src/ts/react-website/runtime-config/schema.json",
|
package/docs/guides/fastapi.mdx
CHANGED
|
@@ -21,7 +21,15 @@ import { PY_VERSIONS } from '../../../../../../packages/nx-plugin/src/utils/vers
|
|
|
21
21
|
|
|
22
22
|
[FastAPI](https://fastapi.tiangolo.com/) is a framework for building APIs in Python.
|
|
23
23
|
|
|
24
|
-
The FastAPI generator creates a new FastAPI with AWS CDK or Terraform infrastructure setup. The generated backend
|
|
24
|
+
The FastAPI generator creates a new FastAPI with AWS CDK or Terraform infrastructure setup. The generated backend is exposed via an AWS API Gateway API. It sets up [AWS Lambda Powertools](https://docs.powertools.aws.dev/lambda/python/latest/) for observability, including structured logging and CloudWatch metrics.
|
|
25
|
+
|
|
26
|
+
<OptionFilter not when={{ infra: 'http-ecs' }} description="Lambda hosting">
|
|
27
|
+
The backend uses AWS Lambda for serverless deployment, and AWS X-Ray tracing is set up too.
|
|
28
|
+
</OptionFilter>
|
|
29
|
+
|
|
30
|
+
<OptionFilter when={{ infra: 'http-ecs' }} description="ECS hosting">
|
|
31
|
+
Your API runs as a long-lived uvicorn server in a container on Amazon ECS, behind an API Gateway HTTP API, rather than in a Lambda function. See <a href="#running-on-amazon-ecs">Running on Amazon ECS</a> for what that changes, including why X-Ray tracing produces no traces there.
|
|
32
|
+
</OptionFilter>
|
|
25
33
|
|
|
26
34
|
## Usage
|
|
27
35
|
|
|
@@ -43,7 +51,21 @@ Select `rest-lambda` (default) as your `infra` if you intend to build any stream
|
|
|
43
51
|
|
|
44
52
|
:::tip[Integration Pattern]
|
|
45
53
|
The `integrationPattern` option defaults to `isolated`, which creates one Lambda per FastAPI operation. Select `shared` if you would prefer a single shared Lambda handler for the whole API, with optional per-operation overrides.
|
|
54
|
+
|
|
55
|
+
With `infra` set to `http-ecs` the option is always `shared`, since one container service answers every operation.
|
|
56
|
+
:::
|
|
57
|
+
|
|
58
|
+
<OptionFilter when={{ infra: 'http-ecs' }} description="ECS hosting is experimental">
|
|
59
|
+
:::caution[Experimental]
|
|
60
|
+
`http-ecs` is experimental. Its generated output may change between releases without a migration to carry an existing workspace across, so expect to reconcile those changes yourself when you upgrade.
|
|
61
|
+
|
|
62
|
+
It needs the CDK IaC provider: Terraform support is not implemented yet. If the workspace resolves to Terraform, the generator fails with a message naming the fix.
|
|
63
|
+
|
|
64
|
+
`integrationPattern` is always `shared` here. Passing `--integrationPattern=isolated` alongside `http-ecs` does not fail: the generator warns that the option is ignored and uses `shared`. Running the generator interactively, it asks for `integrationPattern` before `infra`, with `isolated` preselected, so that warning is what you see if you leave the preselection alone and then choose `http-ecs`. In the command builder on this page, picking `http-ecs` disables `isolated` and clears it if it was selected, so the command it writes never carries the flag.
|
|
65
|
+
|
|
66
|
+
The `architecture` option sets whether the container runs on `ARM64` or `X86_64`, and the Python wheels in its bundle follow it. Leave it out to use the architecture of the machine you run the generator on. See <Link path="/guides/py-container#cpu-architecture">CPU Architecture</Link> in the container guide.
|
|
46
67
|
:::
|
|
68
|
+
</OptionFilter>
|
|
47
69
|
|
|
48
70
|
## Generator Output
|
|
49
71
|
|
|
@@ -63,6 +85,20 @@ The generator will create the following project structure in the `<directory>/<a
|
|
|
63
85
|
|
|
64
86
|
</FileTree>
|
|
65
87
|
|
|
88
|
+
<OptionFilter when={{ infra: 'http-ecs' }} description="Files that differ when the API runs on ECS">
|
|
89
|
+
With `infra` set to `http-ecs` the API runs as its own HTTP server rather than behind the Lambda Web Adapter, so `run.sh` is not generated. In its place you get a container folder inside your module, named after your API:
|
|
90
|
+
|
|
91
|
+
<FileTree>
|
|
92
|
+
- \<module_name>
|
|
93
|
+
- \<api_name>_container
|
|
94
|
+
- main.py Entry point: adds `GET /health`, writes out the metrics the API collects, and starts uvicorn on `HOST` and `PORT`. The bundle, the image and the `serve` target all start from this file
|
|
95
|
+
- Dockerfile Container image definition
|
|
96
|
+
- .dockerignore Files left out of the image build context
|
|
97
|
+
</FileTree>
|
|
98
|
+
|
|
99
|
+
Your `init.py`, `main.py` and the operations you add to them are the same for every `infra` value.
|
|
100
|
+
</OptionFilter>
|
|
101
|
+
|
|
66
102
|
### Infrastructure
|
|
67
103
|
|
|
68
104
|
<Snippet name="shared-constructs" />
|
|
@@ -101,8 +137,18 @@ The generator sets up several features automatically:
|
|
|
101
137
|
2. Error handling middleware
|
|
102
138
|
3. Request/response correlation
|
|
103
139
|
4. Metrics collection
|
|
104
|
-
|
|
105
|
-
|
|
140
|
+
|
|
141
|
+
<OptionFilter not when={{ infra: 'http-ecs' }} description="Streaming, which needs a REST API">
|
|
142
|
+
Type-safe streaming is set up too, for the `rest-lambda` infrastructure only.
|
|
143
|
+
</OptionFilter>
|
|
144
|
+
|
|
145
|
+
<OptionFilter not when={{ infra: 'http-ecs' }} description="How the Lambda variants serve the app">
|
|
146
|
+
The app is served in a Lambda function by the [Lambda Web Adapter](https://github.com/awslabs/aws-lambda-web-adapter) running uvicorn.
|
|
147
|
+
</OptionFilter>
|
|
148
|
+
|
|
149
|
+
<OptionFilter when={{ infra: 'http-ecs' }} description="How the ECS variant serves the app">
|
|
150
|
+
The app is served by uvicorn in the container, with no Lambda Web Adapter. See <a href="#running-on-amazon-ecs">Running on Amazon ECS</a>.
|
|
151
|
+
</OptionFilter>
|
|
106
152
|
|
|
107
153
|
### Observability with AWS Lambda Powertools
|
|
108
154
|
|
|
@@ -140,6 +186,12 @@ def read_item(item_id: int):
|
|
|
140
186
|
return {"item_id": item_id}
|
|
141
187
|
```
|
|
142
188
|
|
|
189
|
+
<OptionFilter when={{ infra: 'http-ecs' }} description="X-Ray produces no traces on ECS">
|
|
190
|
+
:::caution[No traces on ECS]
|
|
191
|
+
The decorator and the subsegment above run without error in a container, but produce no traces. Powertools turns its tracer off when it is not running in Lambda. See <a href="#observability">Observability</a> for what you do get, and what to add if you need traces.
|
|
192
|
+
:::
|
|
193
|
+
</OptionFilter>
|
|
194
|
+
|
|
143
195
|
#### Metrics
|
|
144
196
|
|
|
145
197
|
CloudWatch metrics are collected automatically for each request. You can add custom metrics:
|
|
@@ -187,9 +239,17 @@ It's recommended to specify response models for your API operations for better c
|
|
|
187
239
|
|
|
188
240
|
### Accessing the Calling User
|
|
189
241
|
|
|
242
|
+
<OptionFilter when={{ infra: 'http-ecs' }} description="Identity on ECS — the authorizer context does not arrive">
|
|
243
|
+
On ECS there is no Lambda Web Adapter and no API Gateway request context header: API Gateway does not forward the authorizer's claims to a private load balancer integration. The authorizer still decides whether a request reaches the container at all, but your handlers cannot read the caller from the request as written below.
|
|
244
|
+
|
|
245
|
+
To read the caller's identity in an operation, map the authorizer's context onto a request header in your API construct, and read that header from the FastAPI `Request`.
|
|
246
|
+
</OptionFilter>
|
|
247
|
+
|
|
248
|
+
<OptionFilter not when={{ infra: 'http-ecs' }} description="Identity on the Lambda variants">
|
|
190
249
|
When your API is protected by authentication, your route handlers often need to know who is calling. The generated FastAPI runs inside AWS Lambda via the [Lambda Web Adapter](https://github.com/awslabs/aws-lambda-web-adapter), which forwards the API Gateway request context as JSON on the `x-amzn-request-context` header. You can read it from the FastAPI `Request` to extract the caller's identity.
|
|
191
250
|
|
|
192
251
|
As an example, let's add a `/me` endpoint that returns details about the calling user. We'll implement the extraction as a [FastAPI dependency](https://fastapi.tiangolo.com/tutorial/dependencies/) so it can be reused across routes. The shape of the request context — and therefore how you extract the identity — depends on both your selected `auth` method and whether you deployed a REST or HTTP API.
|
|
252
|
+
</OptionFilter>
|
|
193
253
|
|
|
194
254
|
<OptionFilter when={{ auth: 'iam' }} description="Identity extraction for IAM-authenticated APIs">
|
|
195
255
|
For `IAM` authentication, we look up the caller in Cognito using the sub extracted from the API Gateway request context, which requires `boto3`. Add it to your API project:
|
|
@@ -444,6 +504,7 @@ The FastAPI generator creates CDK or Terraform infrastructure as code based on y
|
|
|
444
504
|
|
|
445
505
|
<Infrastructure>
|
|
446
506
|
<Fragment slot="cdk">
|
|
507
|
+
<OptionFilter not when={{ infra: 'http-ecs' }} description="CDK usage for the Lambda variants">
|
|
447
508
|
The CDK construct for deploying your API in the `common/constructs` folder. You can use this in a CDK application:
|
|
448
509
|
|
|
449
510
|
```ts {6-8}
|
|
@@ -467,6 +528,47 @@ This sets up:
|
|
|
467
528
|
4. CloudWatch log group
|
|
468
529
|
5. X-Ray tracing configuration
|
|
469
530
|
6. CloudWatch metrics namespace
|
|
531
|
+
</OptionFilter>
|
|
532
|
+
|
|
533
|
+
<OptionFilter when={{ infra: 'http-ecs' }} description="CDK usage when the API runs on ECS — pass the VPC">
|
|
534
|
+
`defaultIntegrations` takes the VPC your service runs in, and builds everything inside it: the ECS service, the internal load balancer in front of it, and the VPC link API Gateway reaches that load balancer through. The construct never creates a VPC, so you create or import one in your stack.
|
|
535
|
+
|
|
536
|
+
```ts {10,13-15}
|
|
537
|
+
import { MyApi } from '@my-scope/common-constructs';
|
|
538
|
+
import { Stack } from 'aws-cdk-lib';
|
|
539
|
+
import { Vpc } from 'aws-cdk-lib/aws-ec2';
|
|
540
|
+
import { Construct } from 'constructs';
|
|
541
|
+
|
|
542
|
+
export class ExampleStack extends Stack {
|
|
543
|
+
constructor(scope: Construct, id: string) {
|
|
544
|
+
super(scope, id);
|
|
545
|
+
|
|
546
|
+
const vpc = new Vpc(this, 'Vpc', { natGateways: 1 });
|
|
547
|
+
|
|
548
|
+
const api = new MyApi(this, 'MyApi', {
|
|
549
|
+
integrations: MyApi.defaultIntegrations(this, { vpc }).build(),
|
|
550
|
+
});
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
```
|
|
554
|
+
|
|
555
|
+
To use an existing VPC, look it up with `Vpc.fromLookup(this, 'Vpc', { vpcId: '<vpc-id>' })` and pass it as `vpc`.
|
|
556
|
+
|
|
557
|
+
Pass `service` to change the container service itself — its `cpu`, `memoryLimitMiB`, `desiredCount`, `environment`, or `grantTaskRole` to give the task role a permission:
|
|
558
|
+
|
|
559
|
+
```ts
|
|
560
|
+
MyApi.defaultIntegrations(this, {
|
|
561
|
+
vpc,
|
|
562
|
+
service: {
|
|
563
|
+
cpu: 512,
|
|
564
|
+
memoryLimitMiB: 1024,
|
|
565
|
+
grantTaskRole: (taskRole) => myTable.grantReadWriteData(taskRole),
|
|
566
|
+
},
|
|
567
|
+
}).build();
|
|
568
|
+
```
|
|
569
|
+
|
|
570
|
+
This sets up an AWS API Gateway HTTP API with authentication based on your chosen `auth` method, an Amazon ECS service running your container, an internal load balancer in front of it, and the VPC link API Gateway reaches that load balancer through.
|
|
571
|
+
</OptionFilter>
|
|
470
572
|
|
|
471
573
|
<Snippet name="api/cors-configuration-cdk-note" />
|
|
472
574
|
|
|
@@ -502,6 +604,12 @@ Authorizer results are cached for 5 minutes, keyed on the `Authorization` header
|
|
|
502
604
|
</OptionFilter>
|
|
503
605
|
</Fragment>
|
|
504
606
|
<Fragment slot="terraform">
|
|
607
|
+
<OptionFilter when={{ infra: 'http-ecs' }} description="Terraform is not implemented for ECS hosting">
|
|
608
|
+
:::caution[CDK only]
|
|
609
|
+
`http-ecs` is not implemented for Terraform. The generator fails with a message saying so. Choose `rest-lambda` or `http-lambda` for a Terraform workspace.
|
|
610
|
+
:::
|
|
611
|
+
</OptionFilter>
|
|
612
|
+
|
|
505
613
|
The Terraform modules for deploying your API are in the `common/terraform` folder. You can use this in a Terraform configuration.
|
|
506
614
|
|
|
507
615
|
The API module stages its Lambda deployment zip in a shared S3 asset bucket — see the <Link path="/guides/terraform-project">Terraform infrastructure guide</Link> for details. Instantiate the `core/asset-bucket` module once per deployment and pass its `bucket_name` output into every API / Lambda module via the `asset_bucket_name` input:
|
|
@@ -730,16 +838,135 @@ The generator configures a local development server that you can run with:
|
|
|
730
838
|
|
|
731
839
|
<NxCommands commands={['serve <project-name>']} />
|
|
732
840
|
|
|
841
|
+
<OptionFilter not when={{ infra: 'http-ecs' }} description="Local server for the Lambda variants">
|
|
733
842
|
This starts a local FastAPI development server with:
|
|
734
843
|
|
|
735
844
|
- Auto-reload on code changes
|
|
736
845
|
- Interactive API documentation at `/docs` or `/redoc`
|
|
737
846
|
- OpenAPI schema at `/openapi.json`
|
|
847
|
+
</OptionFilter>
|
|
848
|
+
|
|
849
|
+
<OptionFilter when={{ infra: 'http-ecs' }} description="Local server when the API runs on ECS">
|
|
850
|
+
This starts the container's entry point, the same file the image runs, with:
|
|
851
|
+
|
|
852
|
+
- `GET /health`, answering as it does for the load balancer
|
|
853
|
+
- Interactive API documentation at `/docs` or `/redoc`
|
|
854
|
+
- OpenAPI schema at `/openapi.json`
|
|
855
|
+
|
|
856
|
+
The server binds `127.0.0.1`, so it answers only on your own machine. It reloads nothing: restart it to pick up a code change. To reach it from another machine, a VM or a container, set `HOST=0.0.0.0` on the `serve` target — which makes the API reachable by anything that can reach your machine, unauthenticated.
|
|
857
|
+
</OptionFilter>
|
|
738
858
|
|
|
739
859
|
:::note[Project Names]
|
|
740
860
|
Python project names use underscores, so an API generated with `--name=MyApi` is addressed as `my_api` (or by its fully qualified name, `<scope>.my_api`). The port the server listens on is assigned per project — read it from the `serve` target in your project's `project.json`.
|
|
741
861
|
:::
|
|
742
862
|
|
|
863
|
+
<OptionFilter when={{ infra: 'http-ecs' }} description="How ECS hosting works, what it costs and what it limits">
|
|
864
|
+
## Running on Amazon ECS
|
|
865
|
+
|
|
866
|
+
With `infra` set to `http-ecs`, your API runs as a long-lived uvicorn server in a container rather than in a Lambda function behind the Lambda Web Adapter. A request takes this path:
|
|
867
|
+
|
|
868
|
+
1. A caller signs a request to the API Gateway HTTP API, which is the only public entry point. Every route carries the authorizer for the `auth` you chose, so an unauthenticated request is rejected at the edge.
|
|
869
|
+
2. API Gateway forwards the request through a VPC link, which is its private path into your VPC.
|
|
870
|
+
3. The VPC link reaches an internal load balancer. The load balancer has no public address, and its only inbound rule allows the VPC link's security group on the listener port.
|
|
871
|
+
4. The load balancer forwards the request to a task. The tasks' only inbound rule allows the load balancer's security group on the container port, so nothing else in the VPC can open a connection to them.
|
|
872
|
+
|
|
873
|
+
The tasks run in private subnets and never get a public IP address. Nothing in this path allows an address range inbound.
|
|
874
|
+
|
|
875
|
+
By default the service, the load balancer and the VPC link go in the VPC's `PRIVATE_WITH_EGRESS` subnets, which reach AWS through a NAT gateway. Passing `vpcSubnets: { subnetType: SubnetType.PRIVATE_ISOLATED }` to `defaultIntegrations` drops the NAT gateway and its hourly charge, but isolated subnets have no route out, so the VPC then needs four VPC endpoints for the tasks to start at all: interface endpoints for `ecr.api` and `ecr.dkr` to pull the image, an S3 gateway endpoint because the image layers are S3 objects, and an interface endpoint for `logs` so the `awslogs` driver can write. Without them a task cannot pull its image and ECS rolls the deployment back on the circuit breaker without saying why.
|
|
876
|
+
|
|
877
|
+
The VPC link, the load balancer, its listener, the target group and the bucket the load balancer writes its access logs to are created by one construct, `core/api/ecs-http-endpoint.ts` in `common/constructs`, which every API hosted on ECS shares. Your API's own construct names its container service and routes every operation to that endpoint.
|
|
878
|
+
|
|
879
|
+
### What Runs in the Container
|
|
880
|
+
|
|
881
|
+
`<module_name>/<api_name>_container/main.py` imports the app from your `main.py`, so every operation you write is served exactly as it is on Lambda. It adds two things of its own:
|
|
882
|
+
|
|
883
|
+
- `GET /health`, which the load balancer's target group and the container health check call. It is declared `include_in_schema=False`, so it stays out of your OpenAPI spec and therefore out of the API Gateway routes and the generated clients. It is also left out of the request metrics, so the target group's call every 15 seconds does not count as an API request.
|
|
884
|
+
- A middleware that writes out the metrics the API's own middleware collects. On Lambda the handler does that at the end of each invocation; this server lives across requests, so it flushes them itself.
|
|
885
|
+
|
|
886
|
+
The API Gateway routes still come from your OpenAPI spec, so an operation you add is deployed once `nx build` has regenerated the spec.
|
|
887
|
+
|
|
888
|
+
### Observability
|
|
889
|
+
|
|
890
|
+
Logs and metrics work as they do on Lambda: the container writes to its own CloudWatch log group, and metrics are written in the embedded metric format for CloudWatch Logs to extract.
|
|
891
|
+
|
|
892
|
+
One thing about metrics is different here, and it is worth knowing before you read a dashboard or add a metric of your own. `metrics`, the object the <a href="#metrics">Metrics</a> section above tells you to import from `.init`, is a Powertools `Metrics`, and every `Metrics` object in a process shares one metric store. On Lambda one invocation owns that store from start to finish. A uvicorn server handles requests at the same time, and both the API's own per-request metrics and any you add go to that one store, so two requests in flight together can have their metrics written out in one document and attributed to whichever route was added first. The other request's document can come out with no `route` dimension at all, holding only the metrics added after the first flush, and Powertools logs `PowertoolsUserWarning: Dimension 'route' has already been added` when it happens. Under light traffic, where requests rarely overlap, you will not see it.
|
|
893
|
+
|
|
894
|
+
So treat `RequestCount` and `Success` per route as approximate on ECS. If you need a metric attributed to exactly one request, add it to an `EphemeralMetrics` you create in the request and flush before returning, which keeps a store of its own:
|
|
895
|
+
|
|
896
|
+
```python
|
|
897
|
+
from aws_lambda_powertools.metrics import EphemeralMetrics, MetricUnit
|
|
898
|
+
|
|
899
|
+
@app.get("/items/{item_id}")
|
|
900
|
+
def read_item(item_id: int):
|
|
901
|
+
request_metrics = EphemeralMetrics()
|
|
902
|
+
request_metrics.add_metric(name="ItemViewed", unit=MetricUnit.Count, value=1)
|
|
903
|
+
request_metrics.flush_metrics()
|
|
904
|
+
return {"item_id": item_id}
|
|
905
|
+
```
|
|
906
|
+
|
|
907
|
+
AWS X-Ray tracing does not. `@tracer.capture_method` is a no-op outside Lambda, and there is no X-Ray daemon in the task to receive segments either. Add one as a sidecar in your container service if you need traces.
|
|
908
|
+
|
|
909
|
+
The API Gateway HTTP API's access log records every request that reaches the API. The load balancer writes its own access logs to a bucket the `EcsHttpEndpoint` construct creates, which is where you can see which task served a request and what it answered.
|
|
910
|
+
|
|
911
|
+
### Image Build and Scanning
|
|
912
|
+
|
|
913
|
+
The container generator vends the image targets, named after your API:
|
|
914
|
+
|
|
915
|
+
- `<api-name>-container-docker` builds an image for the CPU architecture in the `architecture` option, `linux/arm64` or `linux/amd64`. The option defaults to the architecture of the machine you run the generator on, and you set it with `--architecture`. The target runs the matching bundle target first, `bundle-arm` or `bundle-x86`, so the image holds your package and its dependencies built for that architecture.
|
|
916
|
+
- `<api-name>-container-trivy` scans that image for vulnerabilities.
|
|
917
|
+
|
|
918
|
+
See <Link path="/guides/py-container">Python Container Service</Link> for how the image is built, and <Link path="/guides/docker-bundling">Docker Bundling</Link> for how it is published at deploy time.
|
|
919
|
+
|
|
920
|
+
### Deploying and Tearing Down
|
|
921
|
+
|
|
922
|
+
`deploy-sandbox` deploys in <Link path="/guides/typescript-infrastructure#deploying-your-sandbox-stage">CloudFormation express mode</Link>, which returns once each resource's configuration is applied rather than waiting for the ECS service to reach a steady state. So the target can finish while the first task is still starting, and until a task has passed two health checks fifteen seconds apart the load balancer has no target to send a request to and answers `503`. Expect about a minute of that after the deploy returns. Wait a minute or two and call again. A `503` that never clears is a real failure rather than a slow start: the service's deployment circuit breaker rolls it back when no task becomes healthy.
|
|
923
|
+
|
|
924
|
+
ECS runs a health check of its own inside the container. It calls `GET /health` every 30 seconds, and failures in the first 30 seconds after a task starts do not count against it. Until that check first reports, `aws ecs describe-tasks` gives the task a `healthStatus` of `UNKNOWN`, which turns to `HEALTHY` once it passes. `UNKNOWN` means the check has not reported yet rather than that it failed.
|
|
925
|
+
|
|
926
|
+
The API's URL is a stack output, but not under the name you would guess. The `CfnOutput` is created inside your API construct and nothing overrides its logical ID, so CDK builds the output key from the construct's path plus an eight-character hash: an API added as `new MyApi(this, 'MyApi', …)` outputs `MyApiMyApiUrl` and that hash, not `MyApiUrl`. `cdk deploy` prints the value, and a script should match on the suffix rather than the whole key:
|
|
927
|
+
|
|
928
|
+
```bash
|
|
929
|
+
aws cloudformation describe-stacks --stack-name <stack-name> \
|
|
930
|
+
--query "Stacks[0].Outputs[?contains(OutputKey, 'Url')].OutputValue" --output text
|
|
931
|
+
```
|
|
932
|
+
|
|
933
|
+
`destroy-sandbox` leaves the HTTP API's access log group and the customer-managed KMS key that encrypts it behind. Neither sets a removal policy, so CDK's default of `Retain` applies and CloudFormation skips them rather than deleting them. List them before you tear the stack down, since a deleted stack no longer reports its resources:
|
|
934
|
+
|
|
935
|
+
```bash
|
|
936
|
+
aws cloudformation describe-stack-resources --stack-name <stack-name> \
|
|
937
|
+
--query "StackResources[?contains(LogicalResourceId, 'AccessLogs') && (ResourceType=='AWS::Logs::LogGroup' || ResourceType=='AWS::KMS::Key')].[ResourceType,PhysicalResourceId]" \
|
|
938
|
+
--output text
|
|
939
|
+
```
|
|
940
|
+
|
|
941
|
+
Then remove them with `aws logs delete-log-group --log-group-name <log-group-name>` and `aws kms schedule-key-deletion --key-id <key-id> --pending-window-in-days 7`, seven days being the shortest window KMS accepts.
|
|
942
|
+
|
|
943
|
+
API Gateway accepts one VPC link deletion every 30 seconds per account, and that quota cannot be raised. Every `http-ecs` API in a stack that uses the same VPC shares one VPC link, so a stack deletes a single link however many of these APIs it holds. Nothing is shared across a stack boundary, and a nested stack counts as its own stack, so stacks torn down at the same time in one account can still clash. If a stack ends in `DELETE_FAILED` on a VPC link, wait 30 seconds and delete the stack again.
|
|
944
|
+
|
|
945
|
+
Upgrading from a release that gave each API its own link needs one check, if the stack already deployed three or more of these APIs on one VPC. The next deploy keeps the first API's link and deletes the others in a single cleanup phase, so the second deletion goes out seconds after the first and API Gateway throttles it. The stack events then show `DELETE_FAILED` on a VPC link and on its security group, which cannot be deleted while the link still uses it, and the update takes about 20 minutes longer while CloudFormation retries both in the same cleanup. It deletes them on a later attempt and the update ends `UPDATE_COMPLETE`. Once it has completed, check the stack events, or the VPC links list in the API Gateway console, for a link still in `DELETE_FAILED`. Wait 30 seconds and delete any link in that state, then its security group. A link left behind counts against the ten-per-Region quota until it is gone; its security group does not.
|
|
946
|
+
|
|
947
|
+
### Limits
|
|
948
|
+
|
|
949
|
+
- **Integration timeout.** API Gateway HTTP APIs cap an integration at about 30 seconds. An operation that takes longer than that fails at the gateway however long the container keeps working.
|
|
950
|
+
- **No streaming.** Streaming operations need the `rest-lambda` infrastructure. An HTTP API cannot stream a response.
|
|
951
|
+
- **Payload size.** Requests and responses follow API Gateway's 10 MB limit.
|
|
952
|
+
- **VPC links per Region.** API Gateway allows ten VPC links per account per Region, raisable through Service Quotas. APIs sharing a stack and a VPC share one link, so what counts against the quota is stacks and VPCs rather than APIs.
|
|
953
|
+
- **One service for every operation.** `integrationPattern` is `shared`: there is one container service, and every route is routed to it. The option always resolves to `shared` for this infrastructure, and `isolated` is ignored with a warning.
|
|
954
|
+
|
|
955
|
+
### Cost
|
|
956
|
+
|
|
957
|
+
The load balancer is the new fixed cost compared with Lambda, where you pay per request and nothing when idle. Rough monthly figures in us-east-1, before data transfer:
|
|
958
|
+
|
|
959
|
+
| Item | Approximately |
|
|
960
|
+
| --- | --- |
|
|
961
|
+
| Internal load balancer | $16 plus load balancer capacity units |
|
|
962
|
+
| Fargate task, 0.25 vCPU / 0.5 GB | $7 per task on ARM64, and more on X86_64, which Fargate bills at a higher rate per vCPU and per GB |
|
|
963
|
+
| NAT gateway, if your VPC has one | $33 |
|
|
964
|
+
| VPC link | no hourly charge |
|
|
965
|
+
| S3 storage for the load balancer's access logs | cents per GB a month |
|
|
966
|
+
|
|
967
|
+
So a one-task service costs roughly $23 a month on ARM64 before any traffic, and more if the VPC you pass in has a NAT gateway. The access logs grow with traffic: add a lifecycle rule to `endpoint.accessLogsBucket` to expire them. Check the [Elastic Load Balancing](https://aws.amazon.com/elasticloadbalancing/pricing/) and [AWS Fargate](https://aws.amazon.com/fargate/pricing/) pricing pages for current prices in your Region.
|
|
968
|
+
</OptionFilter>
|
|
969
|
+
|
|
743
970
|
## Invoking your FastAPI
|
|
744
971
|
|
|
745
972
|
To invoke your API from a React website, you can use the <Link path="guides/connection/react-fastapi">`connection` generator</Link>.
|
|
@@ -182,6 +182,13 @@ export class ApplicationStack extends Stack {
|
|
|
182
182
|
|
|
183
183
|
The `UserIdentity` construct automatically adds the necessary <Link path="guides/react-website#runtime-configuration">Runtime Configuration</Link> to ensure that your website can point to the correct Cognito User Pool for authentication.
|
|
184
184
|
|
|
185
|
+
If your website is served from a URL other than a CloudFront distribution in the same stack, permit sign-in to redirect there with `addCallbackUrl`:
|
|
186
|
+
|
|
187
|
+
```ts
|
|
188
|
+
const identity = new UserIdentity(this, 'Identity');
|
|
189
|
+
identity.addCallbackUrl('https://app.example.com');
|
|
190
|
+
```
|
|
191
|
+
|
|
185
192
|
:::note[SMS role retention]
|
|
186
193
|
The User Pool is backed by an IAM role that Cognito assumes to send SMS messages. The role's removal policy mirrors the User Pool's — by default CDK's `UserPool` uses `RemovalPolicy.RETAIN`, so on `cdk destroy` both the User Pool and its SMS role are retained and will need to be cleaned up manually if no longer needed.
|
|
187
194
|
:::
|
|
@@ -485,6 +485,61 @@ module "my_website" {
|
|
|
485
485
|
|
|
486
486
|
You will also need to create DNS records (for example in Route 53) pointing your domain at the CloudFront distribution.
|
|
487
487
|
|
|
488
|
+
### Hosting through API Gateway
|
|
489
|
+
|
|
490
|
+
Instead of CloudFront, you can serve your website through the routes of an existing REST API, on the API's own origin, with no CloudFront distribution and no cross-origin requests. This is supported with CDK only.
|
|
491
|
+
|
|
492
|
+
Generate the API first (with <Link path="guides/ts-smithy-api">`ts#smithy-api`</Link>, or with <Link path="guides/trpc">`ts#trpc-api`</Link> or <Link path="guides/fastapi">`py#fast-api`</Link> using REST infrastructure), then generate your website with `hosting` set to `api-gateway`:
|
|
493
|
+
|
|
494
|
+
<RunGenerator generator="ts#react-website" requiredParameters={{ name: 'my-website', hosting: 'api-gateway', apiProject: 'my-api' }} />
|
|
495
|
+
|
|
496
|
+
The generated website construct then extends `ApiGatewayStaticWebsite`, which adds these routes to the API:
|
|
497
|
+
|
|
498
|
+
- `GET /` and any path without a file extension return `index.html`, so routes of your app load directly.
|
|
499
|
+
- `GET /runtime-config.json` returns the <Link path="guides/react-website#runtime-configuration">runtime configuration</Link>.
|
|
500
|
+
- `GET /assets/{proxy+}` returns the files named by their content's hash, cached for a year.
|
|
501
|
+
- Any other path with a file extension returns that file from the bundle.
|
|
502
|
+
|
|
503
|
+
These routes need no authorization, while the API's operations keep theirs. For an API with IAM authorization, the resource policy allows anyone every `GET` except the API's own IAM operations, which still need an IAM grant. The API's Web ACL covers the website too.
|
|
504
|
+
|
|
505
|
+
The website is built with a relative Vite `base` (`./`), and the router's `basepath` comes from the page's `<base href>`. The construct deploys a copy of `index.html` for each path the API is reached at, such as `/prod/` on the execute-api URL or a custom domain's base path, and serves each request the copy for its domain. So one build serves every URL of the API.
|
|
506
|
+
|
|
507
|
+
Pass your API to the website:
|
|
508
|
+
|
|
509
|
+
```diff lang="ts" title="packages/infra/src/stacks/application-stack.ts"
|
|
510
|
+
const identity = new UserIdentity(this, 'Identity');
|
|
511
|
+
const api = new MyApi(this, 'MyApi', {
|
|
512
|
+
integrations: MyApi.defaultIntegrations(this).build(),
|
|
513
|
+
});
|
|
514
|
+
api.grantInvokeAccess(identity.identityPool.authenticatedRole);
|
|
515
|
+
- new MyWebsite(this, 'MyWebsite');
|
|
516
|
+
+ new MyWebsite(this, 'MyWebsite', { api });
|
|
517
|
+
```
|
|
518
|
+
|
|
519
|
+
The User Pool's web client permits every URL the website is served at as a callback, so sign-in returns to the URL it started from.
|
|
520
|
+
|
|
521
|
+
#### Custom domains
|
|
522
|
+
|
|
523
|
+
The website is also served from each custom domain that maps the API, whether you set the API's `domainName`, call `addDomainName`, or map it with a `DomainName` of your own, before or after the website construct. On each domain it is served under the mapping's base path: at `https://app.example.com` for an empty base path, or at `https://example.com/app/` for `app`. The execute-api URL keeps serving it too.
|
|
524
|
+
|
|
525
|
+
- `url` is always the execute-api URL, such as `https://<api-id>.execute-api.<region>.amazonaws.com/prod/`. It exists for every API and does not depend on which domains you add. The stack output `WebsiteUrl` names it.
|
|
526
|
+
- `urls` lists every URL the website is served at: the execute-api URL first, then each custom domain sorted by domain name and base path. The stack output `WebsiteUrls` lists them, separated by spaces.
|
|
527
|
+
|
|
528
|
+
The construct never picks one domain over another. If you want a canonical domain, name it yourself, for example in an output of your own:
|
|
529
|
+
|
|
530
|
+
```ts
|
|
531
|
+
new CfnOutput(this, 'AppUrl', { value: 'https://app.example.com' });
|
|
532
|
+
```
|
|
533
|
+
|
|
534
|
+
#### Pitfalls
|
|
535
|
+
|
|
536
|
+
- The deployed website runs under a path such as `/prod/`, while the local dev server runs at the root. Link through the router or with relative URLs: a link starting with `/` works locally and breaks once deployed.
|
|
537
|
+
- The runtime configuration names one URL for the API, which the website calls from whichever URL it was loaded at. The API allows every origin by default. If you restrict its CORS origins with `restrictCorsTo`, include the origin of each of the website's `urls`.
|
|
538
|
+
- Keep `<base href="%BASE_URL%" />` in `index.html`. The construct writes the website's path there in each copy it deploys, and the build loads everything relative to it.
|
|
539
|
+
- The construct refuses to synthesise for an API with a `GET` operation at `/` or at a top-level path parameter such as `/{id}`, which would take the paths the website is served from.
|
|
540
|
+
- Switching an existing website from CloudFront replaces its CloudFront website construct only if you have not changed it. Otherwise the generator stops: move your changes aside, delete the construct, and run the generator again.
|
|
541
|
+
- If `RuntimeConfig`, `CognitoAuth`, `UserIdentity` or your API's construct was generated by an older version of the plugin, the generator stops and names it. Run `nx migrate @aws/nx-plugin`, apply the migrations, and run the generator again.
|
|
542
|
+
|
|
488
543
|
## Runtime Configuration
|
|
489
544
|
|
|
490
545
|
Configuration from your infrastructure is provided to your website via <Link path="guides/runtime-config">Runtime Configuration</Link>. This allows your website to access details such as API URLs which are not known until your application is deployed.
|
package/docs/guides/trpc.mdx
CHANGED
|
@@ -1058,11 +1058,16 @@ aws cloudformation describe-stack-resources --stack-name <stack-name> \
|
|
|
1058
1058
|
|
|
1059
1059
|
Then remove them with `aws logs delete-log-group --log-group-name <log-group-name>` and `aws kms schedule-key-deletion --key-id <key-id> --pending-window-in-days 7`, seven days being the shortest window KMS accepts.
|
|
1060
1060
|
|
|
1061
|
+
API Gateway accepts one VPC link deletion every 30 seconds per account, and that quota cannot be raised. Every `http-ecs` API in a stack that uses the same VPC shares one VPC link, so a stack deletes a single link however many of these APIs it holds. Nothing is shared across a stack boundary, and a nested stack counts as its own stack, so stacks torn down at the same time in one account can still clash. If a stack ends in `DELETE_FAILED` on a VPC link, wait 30 seconds and delete the stack again.
|
|
1062
|
+
|
|
1063
|
+
Upgrading from a release that gave each API its own link needs one check, if the stack already deployed three or more of these APIs on one VPC. The next deploy keeps the first API's link and deletes the others in a single cleanup phase, so the second deletion goes out seconds after the first and API Gateway throttles it. The stack events then show `DELETE_FAILED` on a VPC link and on its security group, which cannot be deleted while the link still uses it, and the update takes about 20 minutes longer while CloudFormation retries both in the same cleanup. It deletes them on a later attempt and the update ends `UPDATE_COMPLETE`. Once it has completed, check the stack events, or the VPC links list in the API Gateway console, for a link still in `DELETE_FAILED`. Wait 30 seconds and delete any link in that state, then its security group. A link left behind counts against the ten-per-Region quota until it is gone; its security group does not.
|
|
1064
|
+
|
|
1061
1065
|
### Limits
|
|
1062
1066
|
|
|
1063
1067
|
- **Integration timeout.** API Gateway HTTP APIs cap an integration at about 30 seconds. A procedure that takes longer than that fails at the gateway however long the container keeps working.
|
|
1064
1068
|
- **No streaming.** tRPC subscriptions need the `rest-lambda` infrastructure. An HTTP API cannot stream a response.
|
|
1065
1069
|
- **Payload size.** Requests and responses follow API Gateway's 10 MB limit.
|
|
1070
|
+
- **VPC links per Region.** API Gateway allows ten VPC links per account per Region, raisable through Service Quotas. APIs sharing a stack and a VPC share one link, so what counts against the quota is stacks and VPCs rather than APIs.
|
|
1066
1071
|
- **One service for every operation.** `integrationPattern` is `shared`: there is one container service, and every route is routed to it. The option always resolves to `shared` for this infrastructure, and `isolated` is ignored with a warning.
|
|
1067
1072
|
|
|
1068
1073
|
### Cost
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Verified Access
|
|
3
|
+
description: Put an AWS Verified Access door in front of a private REST API
|
|
4
|
+
generator: ts#verified-access
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
import { FileTree } from '@astrojs/starlight/components';
|
|
8
|
+
import Link from '@components/link.astro';
|
|
9
|
+
import RunGenerator from '@components/run-generator.astro';
|
|
10
|
+
import GeneratorParameters from '@components/generator-parameters.astro';
|
|
11
|
+
|
|
12
|
+
The Verified Access generator makes a REST API private to a VPC, and puts an [AWS Verified Access](https://docs.aws.amazon.com/verified-access/latest/ug/what-is-verified-access.html) endpoint (the door) in front of it, on a domain of your own. The API then has no public endpoint. Users sign in at the door with your Cognito User Pool, and the door applies a [Cedar](https://docs.aws.amazon.com/verified-access/latest/ug/auth-policies.html) policy before any request reaches the API, which still checks its own authorization.
|
|
13
|
+
|
|
14
|
+
Together with a website <Link path="guides/react-website#hosting-through-api-gateway">hosted through the API</Link>, this serves both the website and the API from the door, with no public endpoint for either.
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
Generate a REST API first, for example with <Link path="guides/ts-smithy-api">`ts#smithy-api`</Link>, and add <Link path="guides/react-website-auth">authentication</Link> to your website so the workspace has a User Pool. Then run:
|
|
19
|
+
|
|
20
|
+
<RunGenerator generator="ts#verified-access" requiredParameters={{ apiProject: 'my-api' }} />
|
|
21
|
+
|
|
22
|
+
### Options
|
|
23
|
+
|
|
24
|
+
<GeneratorParameters generator="ts#verified-access" />
|
|
25
|
+
|
|
26
|
+
## Generator Output
|
|
27
|
+
|
|
28
|
+
The generator adds a construct to your shared constructs. It supports CDK only.
|
|
29
|
+
|
|
30
|
+
<FileTree>
|
|
31
|
+
|
|
32
|
+
- packages/common/constructs/src/core
|
|
33
|
+
- verified-access.ts The door, its trust provider, Web ACL and DNS record, and the private custom domain it forwards to
|
|
34
|
+
|
|
35
|
+
</FileTree>
|
|
36
|
+
|
|
37
|
+
## Infrastructure Usage
|
|
38
|
+
|
|
39
|
+
Add the door to your stack, after the API. It needs a VPC of yours to reach the private API from, such as an existing one you look up:
|
|
40
|
+
|
|
41
|
+
```diff lang="ts" title="packages/infra/src/stacks/application-stack.ts"
|
|
42
|
+
+import { Vpc } from 'aws-cdk-lib/aws-ec2';
|
|
43
|
+
+import { HostedZone } from 'aws-cdk-lib/aws-route53';
|
|
44
|
+
import {
|
|
45
|
+
MyApi,
|
|
46
|
+
MyWebsite,
|
|
47
|
+
UserIdentity,
|
|
48
|
+
+ VerifiedAccess,
|
|
49
|
+
} from '@my-scope/common-constructs';
|
|
50
|
+
|
|
51
|
+
const identity = new UserIdentity(this, 'Identity');
|
|
52
|
+
const api = new MyApi(this, 'MyApi', {
|
|
53
|
+
integrations: MyApi.defaultIntegrations(this).build(),
|
|
54
|
+
});
|
|
55
|
+
api.grantInvokeAccess(identity.identityPool.authenticatedRole);
|
|
56
|
+
+ new VerifiedAccess(this, 'Door', {
|
|
57
|
+
+ api,
|
|
58
|
+
+ identity,
|
|
59
|
+
+ vpc: Vpc.fromLookup(this, 'Vpc', { vpcId: 'vpc-0123456789abcdef0' }),
|
|
60
|
+
+ hostedZone: HostedZone.fromHostedZoneAttributes(this, 'Zone', {
|
|
61
|
+
+ hostedZoneId: 'Z0123456789EXAMPLE',
|
|
62
|
+
+ zoneName: 'example.com',
|
|
63
|
+
+ }),
|
|
64
|
+
+ recordName: 'app',
|
|
65
|
+
+ });
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
The door makes the API private to an execute-api endpoint (`door.executeApiEndpoint`), and denies any call not made through it. It allows nothing itself, so the API's own grants still decide who may call what. It points the API's entry in the <Link path="guides/react-website#runtime-configuration">runtime configuration</Link> at `https://app.example.com/prod/`, and allows sign-in to return there.
|
|
69
|
+
|
|
70
|
+
The VPC needs a subnet in at least one Availability Zone that the execute-api endpoint can use. In it, the door creates:
|
|
71
|
+
|
|
72
|
+
- unless you pass an `endpoint`, an execute-api interface endpoint, with a security group that admits HTTPS from the door alone, so nothing else in the VPC can skip its sign-in;
|
|
73
|
+
- a security group for the Verified Access endpoint, allowed to the execute-api endpoint on port 443.
|
|
74
|
+
|
|
75
|
+
It also creates:
|
|
76
|
+
|
|
77
|
+
- a certificate for `app.example.com`, validated in the hosted zone;
|
|
78
|
+
- a Cognito client of its own, and an OIDC trust provider on your User Pool;
|
|
79
|
+
- a Verified Access instance, group and endpoint, with access logs and a regional Web ACL;
|
|
80
|
+
- a CNAME record for `app.example.com`, pointing at the endpoint;
|
|
81
|
+
- a private custom domain for the API's stage on `app.example.com`, reached through the endpoint.
|
|
82
|
+
|
|
83
|
+
### Choosing the VPC
|
|
84
|
+
|
|
85
|
+
`vpc` is required: an existing VPC from `Vpc.fromLookup`, or one your stack creates. If it already has an execute-api endpoint, pass that as `endpoint`, and the door allows its own security group to it on port 443 instead of creating another. An endpoint the door creates has private DNS turned off, so other calls to API Gateway from the VPC are unchanged.
|
|
86
|
+
|
|
87
|
+
By default the door admits anyone whose email address the User Pool has verified. Pass a Cedar `policyDocument` to change that, referring to the user's claims as `context.cognito`.
|
|
88
|
+
|
|
89
|
+
This works with a website <Link path="guides/react-website#hosting-through-api-gateway">hosted through the API</Link>, which the door then serves with the API. Each adds its own statement to the stack, in any order.
|
|
90
|
+
|
|
91
|
+
### Pitfalls
|
|
92
|
+
|
|
93
|
+
- The hosted zone must be a public zone delegated from its parent, so ACM can validate the certificate and browsers can resolve the door's domain.
|
|
94
|
+
- The first deploy takes about 20 minutes, most of it creating the Verified Access endpoint.
|
|
95
|
+
- Verified Access is charged per hour for each application endpoint, as well as per GB of traffic, whether or not it is used. See [AWS Verified Access pricing](https://aws.amazon.com/verified-access/pricing/).
|
|
96
|
+
- Each door is a Verified Access instance, and instances count against an [account quota](https://docs.aws.amazon.com/verified-access/latest/ug/verified-access-quotas.html).
|
|
97
|
+
- The User Pool requires MFA by default, so users set it up at their first sign-in at the door.
|
|
98
|
+
- The default policy refuses users whose email address the User Pool has not verified.
|
|
99
|
+
- A private API is reachable only through its VPC endpoint, so the website's `serve` target cannot call the deployed API. Use its <Link path="guides/react-website#dev-target">`dev` target</Link>, which runs the API's local server.
|
|
100
|
+
- A URL's `#fragment` is not sent to the server, so it is lost through the door's sign-in. The path and query are kept.
|
|
101
|
+
- A private API can be mapped only to a private custom domain name, which the door creates. So the door refuses, at synth, an API with a public custom domain name or base path mapping of its own.
|
|
102
|
+
- User Pools are retained when the stack is deleted, and have deletion protection on. Delete them by hand afterwards.
|
|
103
|
+
- If `UserIdentity`, your API's construct or the website's `useSigV4` hook was generated by an older version of the plugin, the generator stops and names it. Run `nx migrate @aws/nx-plugin`, apply the migrations, and run the generator again.
|
package/generators.json
CHANGED
|
@@ -357,6 +357,14 @@
|
|
|
357
357
|
"guidePages": ["react-website-auth"],
|
|
358
358
|
"hidden": true
|
|
359
359
|
},
|
|
360
|
+
"ts#verified-access": {
|
|
361
|
+
"factory": "./src/ts/verified-access/generator",
|
|
362
|
+
"schema": "./src/ts/verified-access/schema.json",
|
|
363
|
+
"description": "Put an AWS Verified Access door in front of a private REST API",
|
|
364
|
+
"metric": "g90",
|
|
365
|
+
"experimental": true,
|
|
366
|
+
"guidePages": ["ts-verified-access"]
|
|
367
|
+
},
|
|
360
368
|
"ts#react-website#runtime-config": {
|
|
361
369
|
"factory": "./src/ts/react-website/runtime-config/generator",
|
|
362
370
|
"schema": "./src/ts/react-website/runtime-config/schema.json",
|
package/package.json
CHANGED
package/src/py/api/schema.json
CHANGED
|
@@ -35,9 +35,11 @@
|
|
|
35
35
|
},
|
|
36
36
|
"integrationPattern": {
|
|
37
37
|
"type": "string",
|
|
38
|
-
"description": "How API Gateway integrations are generated for the API. Choose between isolated
|
|
39
|
-
"default": "isolated",
|
|
38
|
+
"description": "How API Gateway integrations are generated for the API. Choose between isolated and shared. Defaults to isolated. The http-ecs infrastructure always uses shared, since one container service answers every operation, so isolated is ignored there with a warning.",
|
|
40
39
|
"enum": ["isolated", "shared"],
|
|
40
|
+
"x-value-when": {
|
|
41
|
+
"isolated": { "infra": ["rest-lambda", "http-lambda", "none"] }
|
|
42
|
+
},
|
|
41
43
|
"x-priority": "important",
|
|
42
44
|
"x-prompt": {
|
|
43
45
|
"message": "How would you like to map integrations to your API operations?",
|
|
@@ -89,12 +91,17 @@
|
|
|
89
91
|
},
|
|
90
92
|
"infra": {
|
|
91
93
|
"type": "string",
|
|
92
|
-
"description": "The type of infrastructure to use to deploy this API.",
|
|
94
|
+
"description": "The type of infrastructure to use to deploy this API. http-ecs (experimental) runs the API as a container service on Amazon ECS behind an API Gateway HTTP API, and requires the CDK IaC provider. It always uses integrationPattern shared, since one container service answers every operation.",
|
|
93
95
|
"default": "rest-lambda",
|
|
94
|
-
"enum": ["rest-lambda", "http-lambda", "none"],
|
|
96
|
+
"enum": ["rest-lambda", "http-lambda", "http-ecs", "none"],
|
|
95
97
|
"x-prompt": "What infrastructure would you like to deploy your API with?",
|
|
96
98
|
"x-priority": "important"
|
|
97
99
|
},
|
|
100
|
+
"architecture": {
|
|
101
|
+
"type": "string",
|
|
102
|
+
"description": "The CPU architecture your container image is built for and its ECS task runs on. Applies to the http-ecs infrastructure. ARM64 runs on AWS Graviton, which costs less per task on AWS Fargate. By default this is the architecture of the machine you run the generator on, and re-running keeps the architecture already generated.",
|
|
103
|
+
"enum": ["ARM64", "X86_64"]
|
|
104
|
+
},
|
|
98
105
|
"preferInstallDependencies": {
|
|
99
106
|
"type": "boolean",
|
|
100
107
|
"description": "Whether to prefer installing dependencies after the generator runs. Set to false to defer installing when batching multiple generators (an install still runs if needed so subsequent generators can compute the Nx project graph); install once at the end.",
|
|
@@ -18,9 +18,11 @@
|
|
|
18
18
|
},
|
|
19
19
|
"integrationPattern": {
|
|
20
20
|
"type": "string",
|
|
21
|
-
"description": "How API Gateway integrations are generated for the API. Choose between isolated
|
|
22
|
-
"default": "isolated",
|
|
21
|
+
"description": "How API Gateway integrations are generated for the API. Choose between isolated and shared. Defaults to isolated. The http-ecs infrastructure always uses shared, since one container service answers every operation, so isolated is ignored there with a warning.",
|
|
23
22
|
"enum": ["isolated", "shared"],
|
|
23
|
+
"x-value-when": {
|
|
24
|
+
"isolated": { "infra": ["rest-lambda", "http-lambda", "none"] }
|
|
25
|
+
},
|
|
24
26
|
"x-priority": "important",
|
|
25
27
|
"x-prompt": {
|
|
26
28
|
"message": "How would you like to map integrations to your FastAPI operations?",
|
|
@@ -72,12 +74,17 @@
|
|
|
72
74
|
},
|
|
73
75
|
"infra": {
|
|
74
76
|
"type": "string",
|
|
75
|
-
"description": "The type of infrastructure to use to deploy this API.",
|
|
77
|
+
"description": "The type of infrastructure to use to deploy this API. http-ecs (experimental) runs the API as a container service on Amazon ECS behind an API Gateway HTTP API, and requires the CDK IaC provider. It always uses integrationPattern shared, since one container service answers every operation.",
|
|
76
78
|
"default": "rest-lambda",
|
|
77
|
-
"enum": ["rest-lambda", "http-lambda", "none"],
|
|
79
|
+
"enum": ["rest-lambda", "http-lambda", "http-ecs", "none"],
|
|
78
80
|
"x-prompt": "What infrastructure would you like to deploy your API with?",
|
|
79
81
|
"x-priority": "important"
|
|
80
82
|
},
|
|
83
|
+
"architecture": {
|
|
84
|
+
"type": "string",
|
|
85
|
+
"description": "The CPU architecture your container image is built for and its ECS task runs on. Applies to the http-ecs infrastructure. ARM64 runs on AWS Graviton, which costs less per task on AWS Fargate. By default this is the architecture of the machine you run the generator on, and re-running keeps the architecture already generated.",
|
|
86
|
+
"enum": ["ARM64", "X86_64"]
|
|
87
|
+
},
|
|
81
88
|
"preferInstallDependencies": {
|
|
82
89
|
"type": "boolean",
|
|
83
90
|
"description": "Whether to prefer installing dependencies after the generator runs. Set to false to defer installing when batching multiple generators (an install still runs if needed so subsequent generators can compute the Nx project graph); install once at the end.",
|
|
@@ -85,6 +85,17 @@
|
|
|
85
85
|
"default": "inherit",
|
|
86
86
|
"x-prompt": "Which provider would you like to manage your infrastructure? (default: inherit)"
|
|
87
87
|
},
|
|
88
|
+
"hosting": {
|
|
89
|
+
"type": "string",
|
|
90
|
+
"description": "How the website is served: from CloudFront, or through the routes of an existing REST API, on the API's own origin. API Gateway hosting requires CDK.",
|
|
91
|
+
"enum": ["cloudfront", "api-gateway"],
|
|
92
|
+
"default": "cloudfront"
|
|
93
|
+
},
|
|
94
|
+
"apiProject": {
|
|
95
|
+
"type": "string",
|
|
96
|
+
"description": "The REST API project to serve the website through, when hosting is api-gateway.",
|
|
97
|
+
"x-dropdown": "projects"
|
|
98
|
+
},
|
|
88
99
|
"preferInstallDependencies": {
|
|
89
100
|
"type": "boolean",
|
|
90
101
|
"description": "Whether to prefer installing dependencies after the generator runs. Set to false to defer installing when batching multiple generators (an install still runs if needed so subsequent generators can compute the Nx project graph); install once at the end.",
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json-schema.org/schema",
|
|
3
|
+
"$id": "ts#verified-access",
|
|
4
|
+
"title": "ts#verified-access",
|
|
5
|
+
"x-label": "Verified Access",
|
|
6
|
+
"description": "Put an AWS Verified Access door in front of a REST API, made private to a VPC",
|
|
7
|
+
"type": "object",
|
|
8
|
+
"properties": {
|
|
9
|
+
"apiProject": {
|
|
10
|
+
"type": "string",
|
|
11
|
+
"description": "The REST API project to put behind the door",
|
|
12
|
+
"$default": {
|
|
13
|
+
"$source": "argv",
|
|
14
|
+
"index": 0
|
|
15
|
+
},
|
|
16
|
+
"x-prompt": "Select the REST API project to put behind the door",
|
|
17
|
+
"x-dropdown": "projects"
|
|
18
|
+
},
|
|
19
|
+
"iac": {
|
|
20
|
+
"type": "string",
|
|
21
|
+
"description": "The preferred IaC provider. Only CDK is supported. By default this is inherited from your initial selection.",
|
|
22
|
+
"enum": ["inherit", "cdk"],
|
|
23
|
+
"default": "inherit"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"required": ["apiProject"]
|
|
27
|
+
}
|