@aws/nx-plugin-mcp 1.0.0-rc.7 → 1.0.0-rc.71
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 +12317 -10933
- package/docs/get_started/building-with-ai.mdx +116 -0
- package/docs/get_started/concepts.mdx +67 -0
- package/docs/get_started/existing-project.mdx +180 -0
- package/docs/get_started/graph-builder.mdx +39 -0
- package/docs/get_started/quick-start.mdx +277 -0
- package/docs/get_started/tutorials/contribute-generator.mdx +408 -0
- package/docs/get_started/tutorials/dungeon-game/1.mdx +1301 -0
- package/docs/get_started/tutorials/dungeon-game/2.mdx +237 -0
- package/docs/get_started/tutorials/dungeon-game/3.mdx +76 -0
- package/docs/get_started/tutorials/dungeon-game/4.mdx +164 -0
- package/docs/get_started/tutorials/dungeon-game/overview.mdx +145 -0
- package/docs/get_started/tutorials/dungeon-game/wrap-up.mdx +41 -0
- package/docs/get_started/tutorials/existing-project.mdx +4 -0
- package/docs/get_started/upgrading.mdx +147 -0
- package/docs/guides/agentcore-gateway.mdx +490 -0
- package/docs/guides/agentcore-harness.mdx +275 -0
- package/docs/guides/astro-docs.mdx +8 -0
- package/docs/guides/connection/agentcore-gateway-agent.mdx +222 -0
- package/docs/guides/connection/agentcore-gateway-gateway.mdx +154 -0
- package/docs/guides/connection/agentcore-gateway-mcp.mdx +134 -0
- package/docs/guides/connection/py-agent-a2a.mdx +48 -16
- package/docs/guides/connection/py-agent-dynamodb.mdx +116 -0
- package/docs/guides/connection/py-agent-gateway.mdx +178 -0
- package/docs/guides/connection/py-agent-mcp.mdx +43 -14
- package/docs/guides/connection/py-agent-rdb.mdx +178 -0
- package/docs/guides/connection/py-fast-api-dynamodb.mdx +56 -0
- package/docs/guides/connection/py-fast-api-rdb.mdx +184 -0
- package/docs/guides/connection/py-mcp-server-dynamodb.mdx +116 -0
- package/docs/guides/connection/py-mcp-server-rdb.mdx +187 -0
- package/docs/guides/connection/react-agentcore-gateway.mdx +112 -0
- package/docs/guides/connection/react-agui.mdx +13 -13
- package/docs/guides/connection/react-fastapi.mdx +38 -2
- package/docs/guides/connection/react-py-agent.mdx +9 -15
- package/docs/guides/connection/react-smithy.mdx +3 -3
- package/docs/guides/connection/react-trpc.mdx +1 -1
- package/docs/guides/connection/react-ts-agent.mdx +8 -8
- package/docs/guides/connection/smithy-dynamodb.mdx +5 -5
- package/docs/guides/connection/smithy-rdb.mdx +9 -9
- package/docs/guides/connection/trpc-dynamodb.mdx +5 -5
- package/docs/guides/connection/trpc-rdb.mdx +6 -6
- package/docs/guides/connection/ts-agent-a2a.mdx +14 -11
- package/docs/guides/connection/ts-agent-dynamodb.mdx +5 -5
- package/docs/guides/connection/ts-agent-gateway.mdx +143 -0
- package/docs/guides/connection/ts-agent-mcp.mdx +12 -9
- package/docs/guides/connection/ts-agent-rdb.mdx +70 -25
- package/docs/guides/connection/ts-mcp-server-dynamodb.mdx +5 -5
- package/docs/guides/connection/ts-mcp-server-rdb.mdx +68 -18
- package/docs/guides/connection.mdx +122 -5
- package/docs/guides/docker-bundling.mdx +69 -12
- package/docs/guides/fastapi.mdx +249 -9
- package/docs/guides/local-development.mdx +87 -0
- package/docs/guides/nx-generator.mdx +4 -3
- package/docs/guides/nx-migration.mdx +165 -0
- package/docs/guides/py-agent.mdx +264 -49
- package/docs/guides/py-dynamodb.mdx +476 -0
- package/docs/guides/py-mcp-server.mdx +61 -2
- package/docs/guides/py-rdb.mdx +265 -0
- package/docs/guides/python-lambda-function.mdx +1 -1
- package/docs/guides/react-website-auth.mdx +65 -4
- package/docs/guides/react-website.mdx +149 -30
- package/docs/guides/runtime-config.mdx +1 -1
- package/docs/guides/security.mdx +75 -0
- package/docs/guides/smithy-project.mdx +167 -0
- package/docs/guides/terraform-project.mdx +2 -2
- package/docs/guides/trpc.mdx +53 -16
- package/docs/guides/ts-agent.mdx +183 -10
- package/docs/guides/ts-dcr-proxy.mdx +569 -0
- package/docs/guides/ts-dynamodb.mdx +66 -242
- package/docs/guides/ts-lambda-function.mdx +1 -1
- package/docs/guides/ts-mcp-server.mdx +109 -29
- package/docs/guides/ts-nx-plugin.mdx +3 -3
- package/docs/guides/ts-rdb.mdx +113 -467
- package/docs/guides/ts-smithy-api.mdx +258 -18
- package/docs/guides/typescript-infrastructure.mdx +46 -24
- package/docs/guides/typescript-project.mdx +134 -27
- package/docs/guides/workspace.mdx +10 -3
- package/docs/snippets/agent/architecture.mdx +1 -1
- package/docs/snippets/agent/bedrock-deployment.mdx +9 -5
- package/docs/snippets/agent/runtime-arn.mdx +23 -2
- package/docs/snippets/agent/securing-your-agent.mdx +39 -0
- package/docs/snippets/api/access-logging.mdx +33 -0
- package/docs/snippets/api/cors-configuration-cdk-note.mdx +1 -1
- package/docs/snippets/api/cors-configuration-terraform-note.mdx +1 -1
- package/docs/snippets/api/type-safe-api-integrations.mdx +33 -2
- package/docs/snippets/api/waf-configuration.mdx +3 -3
- package/docs/snippets/connection/a2a-infrastructure.mdx +1 -1
- package/docs/snippets/connection/dynamodb-local-development.mdx +2 -2
- package/docs/snippets/connection/lambda-dynamodb-access.mdx +1 -1
- package/docs/snippets/connection/py-dynamodb-local-development.mdx +7 -0
- package/docs/snippets/connection/py-lambda-rdb-ssl-requirements.mdx +7 -0
- package/docs/snippets/connection/rdb-api-infrastructure.mdx +51 -19
- package/docs/snippets/dynamodb/deploying-table.mdx +166 -0
- package/docs/snippets/dynamodb/gsi-config.mdx +38 -0
- package/docs/snippets/dynamodb/infrastructure.mdx +33 -0
- package/docs/snippets/dynamodb/local-dev-start.mdx +13 -0
- package/docs/snippets/dynamodb/local-dev-windows.mdx +15 -0
- package/docs/snippets/lambda-function/deploying-your-function.mdx +3 -3
- package/docs/snippets/mcp/architecture.mdx +1 -1
- package/docs/snippets/mcp/bedrock-deployment.mdx +9 -5
- package/docs/snippets/mcp/config.mdx +3 -2
- package/docs/snippets/pdk-migration/example/01-migrate-api.mdx +7 -5
- package/docs/snippets/pdk-migration/example/02-migrate-website.mdx +29 -10
- package/docs/snippets/pdk-migration/example/03-migrate-infra.mdx +4 -4
- package/docs/snippets/pdk-migration/faq/type-safe-api.mdx +5 -111
- package/docs/snippets/prerequisites.mdx +1 -4
- package/docs/snippets/rdb/architecture.mdx +38 -0
- package/docs/snippets/rdb/cluster-instances.mdx +31 -0
- package/docs/snippets/rdb/deletion-protection.mdx +34 -0
- package/docs/snippets/rdb/deploying.mdx +187 -0
- package/docs/snippets/rdb/encryption-key-rotation.mdx +30 -0
- package/docs/snippets/rdb/engine-version.mdx +63 -0
- package/docs/snippets/rdb/infrastructure.mdx +35 -0
- package/docs/snippets/rdb/logging-mysql.mdx +5 -0
- package/docs/snippets/rdb/logging-postgres.mdx +5 -0
- package/docs/snippets/rdb/performance-insights.mdx +34 -0
- package/docs/snippets/rdb/rds-proxy.mdx +50 -0
- package/docs/snippets/rdb/removal-policy.mdx +57 -0
- package/docs/snippets/rdb/serverless-capacity.mdx +32 -0
- package/docs/snippets/recommended-prerequisites.mdx +10 -0
- package/docs/snippets/required-prerequisites.mdx +1 -4
- package/docs/snippets/runtime-config-app-id-note.mdx +8 -0
- package/docs/snippets/shared-constructs.mdx +1 -1
- package/docs/snippets/trivy-image-scan.mdx +37 -0
- package/generators.json +152 -10
- package/package.json +1 -1
- package/src/agentcore-gateway/agent-connection/schema.json +31 -0
- package/src/agentcore-gateway/gateway-connection/schema.json +31 -0
- package/src/agentcore-gateway/mcp-connection/schema.json +31 -0
- package/src/agentcore-gateway/react-connection/schema.json +31 -0
- package/src/agentcore-gateway/schema.json +72 -0
- package/src/agentcore-harness/schema.json +53 -0
- package/src/connection/schema.json +5 -0
- package/src/infra/app/schema.json +5 -0
- package/src/init/schema.json +35 -0
- package/src/internal/test-matrix/schema.json +21 -0
- package/src/license/schema.json +5 -0
- package/src/preset/schema.json +16 -5
- package/src/py/agent/a2a-connection/schema.json +5 -0
- package/src/py/agent/gateway-connection/schema.json +31 -0
- package/src/py/agent/mcp-connection/schema.json +5 -0
- package/src/py/agent/react-connection/schema.json +5 -0
- package/src/py/agent/schema.json +15 -1
- package/src/py/api/schema.json +5 -0
- package/src/py/dynamodb/agent-connection/schema.json +27 -0
- package/src/py/dynamodb/fast-api-connection/schema.json +23 -0
- package/src/py/dynamodb/mcp-server-connection/schema.json +27 -0
- package/src/py/dynamodb/schema.json +76 -0
- package/src/py/fast-api/react/schema.json +5 -0
- package/src/py/fast-api/schema.json +6 -0
- package/src/py/lambda-function/schema.json +5 -0
- package/src/py/mcp-server/schema.json +6 -0
- package/src/py/project/schema.json +5 -0
- package/src/py/rdb/agent-connection/schema.json +27 -0
- package/src/py/rdb/fast-api-connection/schema.json +23 -0
- package/src/py/rdb/mcp-server-connection/schema.json +27 -0
- package/src/py/rdb/schema.json +78 -0
- package/src/smithy/project/schema.json +28 -1
- package/src/smithy/react-connection/schema.json +5 -0
- package/src/smithy/ts/api/schema.json +6 -0
- package/src/terraform/project/schema.json +5 -0
- package/src/trpc/backend/schema.json +6 -0
- package/src/trpc/react/schema.json +5 -0
- package/src/ts/agent/a2a-connection/schema.json +5 -0
- package/src/ts/agent/gateway-connection/schema.json +31 -0
- package/src/ts/agent/mcp-connection/schema.json +5 -0
- package/src/ts/agent/react-connection/schema.json +5 -0
- package/src/ts/agent/schema.json +14 -0
- package/src/ts/api/schema.json +5 -0
- package/src/ts/astro-docs/schema.json +3 -3
- package/src/ts/dcr-proxy/schema.json +44 -0
- package/src/ts/docs/schema.json +3 -3
- package/src/ts/dynamodb/agent-connection/schema.json +5 -0
- package/src/ts/dynamodb/mcp-server-connection/schema.json +5 -0
- package/src/ts/dynamodb/schema.json +26 -2
- package/src/ts/dynamodb/smithy-connection/schema.json +5 -0
- package/src/ts/dynamodb/trpc-connection/schema.json +5 -0
- package/src/ts/lambda-function/schema.json +5 -0
- package/src/ts/lib/schema.json +5 -0
- package/src/ts/mcp-server/schema.json +6 -0
- package/src/ts/nx-generator/schema.json +5 -0
- package/src/ts/nx-migration/schema.json +63 -0
- package/src/ts/nx-plugin/schema.json +5 -0
- package/src/ts/rdb/agent-connection/schema.json +5 -0
- package/src/ts/rdb/mcp-server-connection/schema.json +5 -0
- package/src/ts/rdb/schema.json +7 -1
- package/src/ts/rdb/smithy-connection/schema.json +5 -0
- package/src/ts/rdb/trpc-connection/schema.json +5 -0
- package/src/ts/react-website/app/schema.json +12 -6
- package/src/ts/react-website/cognito-auth/schema.json +5 -0
- package/src/ts/react-website/runtime-config/schema.json +5 -0
- package/src/ts/website/app/schema.json +11 -6
- package/src/ts/website/auth/schema.json +5 -0
- /package/docs/snippets/connection/{lambda-rdb-ssl-requirements.mdx → ts-lambda-rdb-ssl-requirements.mdx} +0 -0
- /package/docs/snippets/connection/{mcp-server-rdb-ssl-requirements.mdx → ts-mcp-server-rdb-ssl-requirements.mdx} +0 -0
package/docs/guides/fastapi.mdx
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
---
|
|
2
2
|
title: FastAPI
|
|
3
3
|
description: Reference documentation for FastAPI
|
|
4
|
-
generator: py#
|
|
4
|
+
generator: py#api
|
|
5
5
|
when:
|
|
6
6
|
framework: [fastapi]
|
|
7
7
|
---
|
|
8
8
|
|
|
9
|
-
import { FileTree, Tabs, TabItem } from '@astrojs/starlight/components';
|
|
9
|
+
import { FileTree, Tabs, TabItem, CardGrid } from '@astrojs/starlight/components';
|
|
10
|
+
import Astro from '@astrojs/react';
|
|
11
|
+
import ConnectionCard from '@components/connection-card.astro';
|
|
10
12
|
import Link from '@components/link.astro';
|
|
11
13
|
import RunGenerator from '@components/run-generator.astro';
|
|
12
14
|
import GeneratorParameters from '@components/generator-parameters.astro';
|
|
@@ -26,11 +28,11 @@ The FastAPI generator creates a new FastAPI with AWS CDK or Terraform infrastruc
|
|
|
26
28
|
|
|
27
29
|
You can generate a new FastAPI in two ways:
|
|
28
30
|
|
|
29
|
-
<RunGenerator generator="py#
|
|
31
|
+
<RunGenerator generator="py#api" requiredParameters={{ framework: 'fastapi' }} />
|
|
30
32
|
|
|
31
33
|
### Options
|
|
32
34
|
|
|
33
|
-
<GeneratorParameters generator="py#
|
|
35
|
+
<GeneratorParameters generator="py#api" />
|
|
34
36
|
|
|
35
37
|
<Snippet name="api/api-choice-note" />
|
|
36
38
|
|
|
@@ -185,6 +187,216 @@ Unhandled exceptions are caught by the middleware and:
|
|
|
185
187
|
It's recommended to specify response models for your API operations for better code generation if using the `connection` generator. <Link path="guides/connection/react-fastapi#errors">See here for more details</Link>.
|
|
186
188
|
:::
|
|
187
189
|
|
|
190
|
+
### Accessing the Calling User
|
|
191
|
+
|
|
192
|
+
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.
|
|
193
|
+
|
|
194
|
+
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.
|
|
195
|
+
|
|
196
|
+
<OptionFilter when={{ auth: 'iam' }} description="Identity extraction for IAM-authenticated APIs">
|
|
197
|
+
For `IAM` authentication, we look up the caller in Cognito using the sub extracted from the API Gateway request context. Create `identity.py` alongside `main.py`:
|
|
198
|
+
|
|
199
|
+
<Tabs syncKey="http-rest">
|
|
200
|
+
<TabItem label="REST API" _filter={{ infra: 'rest-lambda' }}>
|
|
201
|
+
```python
|
|
202
|
+
import json
|
|
203
|
+
import os
|
|
204
|
+
from typing import Annotated
|
|
205
|
+
|
|
206
|
+
from boto3 import client
|
|
207
|
+
from fastapi import Depends, HTTPException, Request
|
|
208
|
+
from pydantic import BaseModel
|
|
209
|
+
|
|
210
|
+
cognito = client("cognito-idp")
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
class Identity(BaseModel):
|
|
214
|
+
sub: str
|
|
215
|
+
username: str
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
def get_identity(request: Request) -> Identity:
|
|
219
|
+
# The Lambda Web Adapter forwards the API Gateway request context as JSON
|
|
220
|
+
request_context_header = request.headers.get("x-amzn-request-context")
|
|
221
|
+
if not request_context_header:
|
|
222
|
+
raise HTTPException(status_code=403, detail="Unable to determine calling user")
|
|
223
|
+
|
|
224
|
+
request_context = json.loads(request_context_header)
|
|
225
|
+
provider = request_context.get("identity", {}).get("cognitoAuthenticationProvider")
|
|
226
|
+
|
|
227
|
+
sub = provider.split(":")[-1] if provider else None
|
|
228
|
+
if not sub:
|
|
229
|
+
raise HTTPException(status_code=403, detail="Unable to determine calling user")
|
|
230
|
+
|
|
231
|
+
users = cognito.list_users(
|
|
232
|
+
# Assumes user pool id is configured in lambda environment
|
|
233
|
+
UserPoolId=os.environ["USER_POOL_ID"],
|
|
234
|
+
Limit=1,
|
|
235
|
+
Filter=f'sub="{sub}"',
|
|
236
|
+
).get("Users", [])
|
|
237
|
+
|
|
238
|
+
if len(users) != 1:
|
|
239
|
+
raise HTTPException(status_code=403, detail=f"No user found with subjectId {sub}")
|
|
240
|
+
|
|
241
|
+
return Identity(sub=sub, username=users[0]["Username"])
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
CurrentUser = Annotated[Identity, Depends(get_identity)]
|
|
245
|
+
```
|
|
246
|
+
</TabItem>
|
|
247
|
+
<TabItem label="HTTP API" _filter={{ infra: 'http-lambda' }}>
|
|
248
|
+
```python
|
|
249
|
+
import json
|
|
250
|
+
import os
|
|
251
|
+
from typing import Annotated
|
|
252
|
+
|
|
253
|
+
from boto3 import client
|
|
254
|
+
from fastapi import Depends, HTTPException, Request
|
|
255
|
+
from pydantic import BaseModel
|
|
256
|
+
|
|
257
|
+
cognito = client("cognito-idp")
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
class Identity(BaseModel):
|
|
261
|
+
sub: str
|
|
262
|
+
username: str
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
def get_identity(request: Request) -> Identity:
|
|
266
|
+
# The Lambda Web Adapter forwards the API Gateway request context as JSON
|
|
267
|
+
request_context_header = request.headers.get("x-amzn-request-context")
|
|
268
|
+
if not request_context_header:
|
|
269
|
+
raise HTTPException(status_code=403, detail="Unable to determine calling user")
|
|
270
|
+
|
|
271
|
+
request_context = json.loads(request_context_header)
|
|
272
|
+
amr = (
|
|
273
|
+
request_context.get("authorizer", {})
|
|
274
|
+
.get("iam", {})
|
|
275
|
+
.get("cognitoIdentity", {})
|
|
276
|
+
.get("amr", [])
|
|
277
|
+
)
|
|
278
|
+
sign_in = next((s for s in amr if ":CognitoSignIn:" in s), None)
|
|
279
|
+
sub = sign_in.split(":")[-1] if sign_in else None
|
|
280
|
+
|
|
281
|
+
if not sub:
|
|
282
|
+
raise HTTPException(status_code=403, detail="Unable to determine calling user")
|
|
283
|
+
|
|
284
|
+
users = cognito.list_users(
|
|
285
|
+
# Assumes user pool id is configured in lambda environment
|
|
286
|
+
UserPoolId=os.environ["USER_POOL_ID"],
|
|
287
|
+
Limit=1,
|
|
288
|
+
Filter=f'sub="{sub}"',
|
|
289
|
+
).get("Users", [])
|
|
290
|
+
|
|
291
|
+
if len(users) != 1:
|
|
292
|
+
raise HTTPException(status_code=403, detail=f"No user found with subjectId {sub}")
|
|
293
|
+
|
|
294
|
+
return Identity(sub=sub, username=users[0]["Username"])
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
CurrentUser = Annotated[Identity, Depends(get_identity)]
|
|
298
|
+
```
|
|
299
|
+
</TabItem>
|
|
300
|
+
</Tabs>
|
|
301
|
+
</OptionFilter>
|
|
302
|
+
|
|
303
|
+
<OptionFilter when={{ auth: 'cognito' }} description="Identity extraction for Cognito-authenticated APIs">
|
|
304
|
+
With `auth: 'cognito'`, the API Gateway Cognito User Pools authorizer verifies the JWT that the caller supplies in the `Authorization` header and places the verified claims on the request context.
|
|
305
|
+
|
|
306
|
+
Create `identity.py` alongside `main.py`:
|
|
307
|
+
|
|
308
|
+
<Tabs syncKey="http-rest">
|
|
309
|
+
<TabItem label="REST API" _filter={{ infra: 'rest-lambda' }}>
|
|
310
|
+
```python
|
|
311
|
+
import json
|
|
312
|
+
from typing import Annotated
|
|
313
|
+
|
|
314
|
+
from fastapi import Depends, HTTPException, Request
|
|
315
|
+
from pydantic import BaseModel
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
class Identity(BaseModel):
|
|
319
|
+
sub: str
|
|
320
|
+
username: str
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
def get_identity(request: Request) -> Identity:
|
|
324
|
+
# The Lambda Web Adapter forwards the API Gateway request context as JSON
|
|
325
|
+
request_context_header = request.headers.get("x-amzn-request-context")
|
|
326
|
+
if not request_context_header:
|
|
327
|
+
raise HTTPException(status_code=403, detail="Unable to determine calling user")
|
|
328
|
+
|
|
329
|
+
request_context = json.loads(request_context_header)
|
|
330
|
+
claims = request_context.get("authorizer", {}).get("claims", {})
|
|
331
|
+
|
|
332
|
+
sub = claims.get("sub")
|
|
333
|
+
username = claims.get("username")
|
|
334
|
+
|
|
335
|
+
if not sub or not username:
|
|
336
|
+
raise HTTPException(status_code=403, detail="Unable to determine calling user")
|
|
337
|
+
|
|
338
|
+
return Identity(sub=sub, username=username)
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
CurrentUser = Annotated[Identity, Depends(get_identity)]
|
|
342
|
+
```
|
|
343
|
+
</TabItem>
|
|
344
|
+
<TabItem label="HTTP API" _filter={{ infra: 'http-lambda' }}>
|
|
345
|
+
HTTP APIs use a JWT authorizer which places the verified claims under `authorizer.jwt.claims`:
|
|
346
|
+
|
|
347
|
+
```python
|
|
348
|
+
import json
|
|
349
|
+
from typing import Annotated
|
|
350
|
+
|
|
351
|
+
from fastapi import Depends, HTTPException, Request
|
|
352
|
+
from pydantic import BaseModel
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
class Identity(BaseModel):
|
|
356
|
+
sub: str
|
|
357
|
+
username: str
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
def get_identity(request: Request) -> Identity:
|
|
361
|
+
# The Lambda Web Adapter forwards the API Gateway request context as JSON
|
|
362
|
+
request_context_header = request.headers.get("x-amzn-request-context")
|
|
363
|
+
if not request_context_header:
|
|
364
|
+
raise HTTPException(status_code=403, detail="Unable to determine calling user")
|
|
365
|
+
|
|
366
|
+
request_context = json.loads(request_context_header)
|
|
367
|
+
claims = request_context.get("authorizer", {}).get("jwt", {}).get("claims", {})
|
|
368
|
+
|
|
369
|
+
sub = claims.get("sub")
|
|
370
|
+
username = claims.get("username")
|
|
371
|
+
|
|
372
|
+
if not sub or not username:
|
|
373
|
+
raise HTTPException(status_code=403, detail="Unable to determine calling user")
|
|
374
|
+
|
|
375
|
+
return Identity(sub=sub, username=username)
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
CurrentUser = Annotated[Identity, Depends(get_identity)]
|
|
379
|
+
```
|
|
380
|
+
</TabItem>
|
|
381
|
+
</Tabs>
|
|
382
|
+
|
|
383
|
+
:::tip[No token verification required]
|
|
384
|
+
You don't need any JWT-verification library here — the API Gateway Cognito User Pools authorizer has already verified the signature, issuer, scopes, and expiry by the time your Lambda runs. If any of those checks fail, API Gateway returns `401 Unauthorized` and your handler is never invoked.
|
|
385
|
+
:::
|
|
386
|
+
</OptionFilter>
|
|
387
|
+
|
|
388
|
+
You can then inject the `CurrentUser` dependency into any route that needs the caller's identity:
|
|
389
|
+
|
|
390
|
+
```python
|
|
391
|
+
from .identity import CurrentUser, Identity
|
|
392
|
+
from .init import app, tracer
|
|
393
|
+
|
|
394
|
+
@app.get("/me")
|
|
395
|
+
@tracer.capture_method
|
|
396
|
+
def me(identity: CurrentUser) -> Identity:
|
|
397
|
+
return identity
|
|
398
|
+
```
|
|
399
|
+
|
|
188
400
|
<OptionFilter when={{ infra: 'rest-lambda' }} description="Streaming — REST API only">
|
|
189
401
|
### Streaming
|
|
190
402
|
|
|
@@ -226,14 +438,14 @@ To consume a stream of responses, you can make use of the <Link path="guides/con
|
|
|
226
438
|
|
|
227
439
|
## Deploying your FastAPI
|
|
228
440
|
|
|
229
|
-
The FastAPI generator creates CDK or Terraform infrastructure as code based on your selected `
|
|
441
|
+
The FastAPI generator creates CDK or Terraform infrastructure as code based on your selected `iac`. You can use this to deploy your FastAPI.
|
|
230
442
|
|
|
231
443
|
<Infrastructure>
|
|
232
444
|
<Fragment slot="cdk">
|
|
233
445
|
The CDK construct for deploying your API in the `common/constructs` folder. You can use this in a CDK application:
|
|
234
446
|
|
|
235
447
|
```ts {6-8}
|
|
236
|
-
import { MyApi } from '
|
|
448
|
+
import { MyApi } from '@my-scope/common-constructs';
|
|
237
449
|
|
|
238
450
|
export class ExampleStack extends Stack {
|
|
239
451
|
constructor(scope: Construct, id: string) {
|
|
@@ -261,7 +473,7 @@ This sets up:
|
|
|
261
473
|
If you selected to use `Cognito` authentication, you will need to supply the `identity` property to the API construct:
|
|
262
474
|
|
|
263
475
|
```ts {9}
|
|
264
|
-
import { MyApi, UserIdentity } from '
|
|
476
|
+
import { MyApi, UserIdentity } from '@my-scope/common-constructs';
|
|
265
477
|
|
|
266
478
|
export class ExampleStack extends Stack {
|
|
267
479
|
constructor(scope: Construct, id: string) {
|
|
@@ -330,7 +542,7 @@ This sets up:
|
|
|
330
542
|
:::note[Cognito Authentication]
|
|
331
543
|
If you selected to use `Cognito` authentication, you will need to supply the Cognito configuration:
|
|
332
544
|
|
|
333
|
-
```hcl {
|
|
545
|
+
```hcl {6-7}
|
|
334
546
|
module "my_api" {
|
|
335
547
|
source = "../../common/terraform/src/app/apis/my-api"
|
|
336
548
|
|
|
@@ -406,6 +618,12 @@ When using `Custom` auth, your API is protected by a Lambda Authorizer that **de
|
|
|
406
618
|
<Snippet name="api/waf-configuration" parentHeading="WAF" />
|
|
407
619
|
</OptionFilter>
|
|
408
620
|
|
|
621
|
+
<OptionFilter when={{ infra: 'rest-lambda' }} description="Access logging — REST APIs log requests to CloudWatch by default">
|
|
622
|
+
### Access logging
|
|
623
|
+
|
|
624
|
+
<Snippet name="api/access-logging" parentHeading="Access logging" />
|
|
625
|
+
</OptionFilter>
|
|
626
|
+
|
|
409
627
|
### Integrations
|
|
410
628
|
|
|
411
629
|
<Snippet name="api/type-safe-api-integrations" parentHeading="Integrations" />
|
|
@@ -437,7 +655,7 @@ If you are actively working on both your CDK infrastructure and FastAPI together
|
|
|
437
655
|
</Fragment>
|
|
438
656
|
<Fragment slot="terraform">
|
|
439
657
|
:::note[Terraform Limitations]
|
|
440
|
-
We do not support type-safe integrations for Terraform, and therefore no code generation targets are configured if you selected Terraform for your `
|
|
658
|
+
We do not support type-safe integrations for Terraform, and therefore no code generation targets are configured if you selected Terraform for your `iac`.
|
|
441
659
|
:::
|
|
442
660
|
</Fragment>
|
|
443
661
|
</Infrastructure>
|
|
@@ -509,3 +727,25 @@ This starts a local FastAPI development server with:
|
|
|
509
727
|
## Invoking your FastAPI
|
|
510
728
|
|
|
511
729
|
To invoke your API from a React website, you can use the <Link path="guides/connection/react-fastapi">`connection` generator</Link>.
|
|
730
|
+
|
|
731
|
+
## Connections
|
|
732
|
+
|
|
733
|
+
Use the <Link path="guides/connection">`connection`</Link> generator to integrate this project with others in your workspace. The following connections involve this project:
|
|
734
|
+
|
|
735
|
+
<CardGrid>
|
|
736
|
+
<ConnectionCard
|
|
737
|
+
title="React to FastAPI"
|
|
738
|
+
description="Call a Python FastAPI from a React website"
|
|
739
|
+
href={`/nx-plugin-for-aws/${Astro.currentLocale || 'en'}/guides/connection/react-fastapi`}
|
|
740
|
+
source="react"
|
|
741
|
+
target="fastapi"
|
|
742
|
+
/>
|
|
743
|
+
<ConnectionCard
|
|
744
|
+
title="FastAPI to Python DynamoDB"
|
|
745
|
+
description="Connect a FastAPI to a DynamoDB table"
|
|
746
|
+
href={`/nx-plugin-for-aws/${Astro.currentLocale || 'en'}/guides/connection/py-fast-api-dynamodb`}
|
|
747
|
+
source="fastapi"
|
|
748
|
+
target="dynamodb"
|
|
749
|
+
targetBadge="python"
|
|
750
|
+
/>
|
|
751
|
+
</CardGrid>
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Local Development
|
|
3
|
+
description: How local development works with the serve and dev targets
|
|
4
|
+
---
|
|
5
|
+
import Link from '@components/link.astro';
|
|
6
|
+
import NxCommands from '@components/nx-commands.astro';
|
|
7
|
+
import PackageManagerShortCommand from '@components/package-manager-short-command.astro';
|
|
8
|
+
|
|
9
|
+
Connected projects expose two targets for running them on your machine: `serve` and `dev`. The difference is one of **scope** — how much of your application runs locally versus pointing at deployed AWS infrastructure.
|
|
10
|
+
|
|
11
|
+
Consider a workspace with the following connections: a website that calls a tRPC `api`, and also calls an `agent` which in turn calls an `mcp` server.
|
|
12
|
+
|
|
13
|
+
## `serve`
|
|
14
|
+
|
|
15
|
+
The `serve` target runs **only the targeted project** locally. Every other project it connects to is expected to be **deployed**, and is reached at its real AWS URL.
|
|
16
|
+
|
|
17
|
+
<NxCommands commands={['serve website']} />
|
|
18
|
+
|
|
19
|
+
Because the targeted project talks to deployed resources, it needs to know where they are. For a website this is provided by its `runtime-config.json` (see <Link path="guides/react-website#local-runtime-config">Local Runtime Config</Link>), which you load from a deployed application. For server-side projects (APIs and agents) that read <Link path="guides/runtime-config">Runtime Configuration</Link>, set the `RUNTIME_CONFIG_APP_ID` environment variable to point at your deployed AppConfig application.
|
|
20
|
+
|
|
21
|
+
```d2
|
|
22
|
+
direction: right
|
|
23
|
+
|
|
24
|
+
local: Local {
|
|
25
|
+
style.stroke-dash: 3
|
|
26
|
+
website: website
|
|
27
|
+
}
|
|
28
|
+
deployed: Deployed {
|
|
29
|
+
style.stroke-dash: 3
|
|
30
|
+
api: api
|
|
31
|
+
agent: agent
|
|
32
|
+
mcp: mcp
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
local.website -> deployed.api
|
|
36
|
+
local.website -> deployed.agent
|
|
37
|
+
deployed.agent -> deployed.mcp
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Use `serve` when you want to iterate on a single project against the "real", deployed versions of everything it depends on.
|
|
41
|
+
|
|
42
|
+
## `dev`
|
|
43
|
+
|
|
44
|
+
The `dev` target runs the **targeted project** and **every project connected to it transitively**, all on your machine. The connection generator wires this up automatically — running `dev` on the website also starts local servers for the `api`, the `agent`, and the `mcp` server it reaches through the agent.
|
|
45
|
+
|
|
46
|
+
<NxCommands commands={['dev website']} />
|
|
47
|
+
|
|
48
|
+
When run this way, the website's `runtime-config.json` is automatically overridden (via [Vite's `MODE`](https://vite.dev/guide/env-and-mode), set to `local-dev`) so that it points at your locally running servers instead of deployed URLs.
|
|
49
|
+
|
|
50
|
+
```d2
|
|
51
|
+
direction: right
|
|
52
|
+
|
|
53
|
+
local: Local {
|
|
54
|
+
style.stroke-dash: 3
|
|
55
|
+
website: website
|
|
56
|
+
api: api
|
|
57
|
+
agent: agent
|
|
58
|
+
mcp: mcp
|
|
59
|
+
|
|
60
|
+
website -> api
|
|
61
|
+
website -> agent
|
|
62
|
+
agent -> mcp
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Every project runs locally, so there are no deployed dependencies.
|
|
67
|
+
|
|
68
|
+
Use `dev` when you are working across several connected projects at once and want to iterate quickly without deploying your infrastructure.
|
|
69
|
+
|
|
70
|
+
### Projects with multiple components
|
|
71
|
+
|
|
72
|
+
Some project types can hold multiple components in a single project (for example a TypeScript or Python project containing several agents and MCP servers). For these:
|
|
73
|
+
|
|
74
|
+
- The project-level `dev` target starts **all** of the project's components together. Each component is added to `dev` as it is generated:
|
|
75
|
+
|
|
76
|
+
<NxCommands commands={['dev my-project']} />
|
|
77
|
+
- Each component also exposes a `<component-name>-dev` target, so you can run a single component on its own:
|
|
78
|
+
|
|
79
|
+
<NxCommands commands={['my-component-dev my-project']} />
|
|
80
|
+
|
|
81
|
+
:::tip[Workspace `dev` script]
|
|
82
|
+
Your workspace's root `package.json` includes a `dev` script that runs `nx run-many --target dev`, starting the `dev` target for every project that has one. Spin up your whole workspace's local development servers with:
|
|
83
|
+
|
|
84
|
+
<PackageManagerShortCommand commands={["dev"]} />
|
|
85
|
+
:::
|
|
86
|
+
|
|
87
|
+
For more detail on each project type's local development server, see the relevant guide — for example the <Link path="guides/react-website#local-development-server">React Website</Link> guide.
|
|
@@ -286,7 +286,7 @@ import { generateFiles, joinPathFragments } from '@nx/devkit';
|
|
|
286
286
|
// Generate files from templates
|
|
287
287
|
generateFiles(
|
|
288
288
|
tree,
|
|
289
|
-
joinPathFragments(
|
|
289
|
+
joinPathFragments(import.meta.dirname, 'files'), // Template directory
|
|
290
290
|
'path/to/output', // Output directory
|
|
291
291
|
{
|
|
292
292
|
// Variables to replace in templates
|
|
@@ -459,7 +459,7 @@ export const myGenerator = async (tree: Tree, schema: MyGeneratorSchema) => {
|
|
|
459
459
|
|
|
460
460
|
generateFiles(
|
|
461
461
|
tree,
|
|
462
|
-
joinPathFragments(
|
|
462
|
+
joinPathFragments(import.meta.dirname, 'files'), // Template directory
|
|
463
463
|
'path/to/output', // Output directory
|
|
464
464
|
data,
|
|
465
465
|
);
|
|
@@ -497,7 +497,7 @@ Unit tests for generators are straightforward to implement. Here's a typical pat
|
|
|
497
497
|
|
|
498
498
|
```typescript
|
|
499
499
|
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
|
|
500
|
-
import { yourGenerator } from './generator';
|
|
500
|
+
import { yourGenerator } from './generator.js';
|
|
501
501
|
|
|
502
502
|
describe('your generator', () => {
|
|
503
503
|
let tree;
|
|
@@ -583,6 +583,7 @@ When this generator is run in our repository, it'll generate the following files
|
|
|
583
583
|
- docs/src/content/docs/guides/
|
|
584
584
|
- \<name>.mdx Documentation page for your generator
|
|
585
585
|
- packages/nx-plugin/generators.json Updated to include your generator
|
|
586
|
+
- packages/nx-plugin/sdk/\<prefix>.ts Updated to expose your generator from the SDK (for `ts#` and `py#` generators)
|
|
586
587
|
</FileTree>
|
|
587
588
|
|
|
588
589
|
You can then start to implement your generator.
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Nx Migration Generator
|
|
3
|
+
description: Generates an Nx Migration for an Nx Plugin
|
|
4
|
+
generator: ts#nx-migration
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
import { FileTree } from '@astrojs/starlight/components';
|
|
8
|
+
import RunGenerator from '@components/run-generator.astro';
|
|
9
|
+
import NxCommands from '@components/nx-commands.astro';
|
|
10
|
+
import GeneratorParameters from '@components/generator-parameters.astro';
|
|
11
|
+
import Link from '@components/link.astro';
|
|
12
|
+
|
|
13
|
+
Adds an [Nx Migration](https://nx.dev/docs/kb/migration-generators) to an Nx Plugin. Migrations let `nx migrate` automatically update projects that were generated by a previous version of your plugin when you make a breaking change — renamed targets, changed vended config, or a dependency bump that requires accompanying code changes.
|
|
14
|
+
|
|
15
|
+
The generator scaffolds the migration, registers it in your plugin's `migrations.json`, and wires the `nx-migrations` field into your plugin's `package.json` (creating both if they don't exist yet).
|
|
16
|
+
|
|
17
|
+
:::tip[Recommended Setup]
|
|
18
|
+
We recommend using the <Link path="/guides/ts-nx-plugin">`ts#nx-plugin`</Link> generator as the base project for your migrations, since it configures a plugin project set up to publish generators and migrations.
|
|
19
|
+
:::
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
### Generate a Migration
|
|
24
|
+
|
|
25
|
+
<RunGenerator generator="ts#nx-migration" />
|
|
26
|
+
|
|
27
|
+
### Options
|
|
28
|
+
|
|
29
|
+
<GeneratorParameters generator="ts#nx-migration" />
|
|
30
|
+
|
|
31
|
+
## The three kinds of migration
|
|
32
|
+
|
|
33
|
+
Migrations come in three forms, discriminated by which fields the `migrations.json` entry carries. Pass `--kind` to choose (default `deterministic`):
|
|
34
|
+
|
|
35
|
+
- **Deterministic** (`implementation`) — a codemod with an exact before/after, and the one to reach for first. It runs unattended, including in CI and non-interactive terminals, and applies identically for every user. Use it alone when the change has a shape you can match reliably everywhere it appears, reporting anything it skipped via `nextSteps`.
|
|
36
|
+
- **Hybrid** (`implementation` + `prompt`) — both halves of one change. Use it when a change is breaking and can't be fully applied mechanically: the codemod does as much as it can safely match and returns `agentContext` describing what it changed *and what it skipped*; Nx passes that context to the paired `prompt`, which directs the agent at the rest. The prompt is what stops a workspace being left broken when the leftover edits span too many shapes to codemod.
|
|
37
|
+
- **Agentic** (`prompt`) — a markdown instruction file applied by the user's local coding agent via Nx's agentic `nx migrate` flow. Use it alone when no mechanical before/after holds at all, because the correct edit depends on what the user has built. When no agent runs (CI, no agent installed, consent declined), Nx surfaces the prompt as manual instructions — so write prompts as self-contained, human-actionable steps.
|
|
38
|
+
|
|
39
|
+
## Generator Output
|
|
40
|
+
|
|
41
|
+
The generator creates the following files under your plugin project's source folder, depending on the chosen `kind`:
|
|
42
|
+
|
|
43
|
+
<FileTree>
|
|
44
|
+
- src/migrations/latest/\<name>/
|
|
45
|
+
- migration.ts Codemod implementation (deterministic and hybrid)
|
|
46
|
+
- migration.spec.ts Tests for your migration (deterministic and hybrid)
|
|
47
|
+
- prompt.md Agent/human instructions (agentic and hybrid)
|
|
48
|
+
- migrations.json Created or updated to register your migration
|
|
49
|
+
- package.json Created or updated to add an "nx-migrations" entry
|
|
50
|
+
</FileTree>
|
|
51
|
+
|
|
52
|
+
A new migration lands in `latest/`. Grouping migrations by the release that ships them keeps their order visible at a glance as the collection grows — move a migration into a `v<x.y.z>/` folder (and update its paths in `migrations.json`) when you assign its version.
|
|
53
|
+
|
|
54
|
+
It registers the migration in `migrations.json` with the fields for its kind and no `version` — see [Versioning](#versioning) below. The registered key is prefixed with the folder the migration lives in (`latest-<name>`), so reusing a name for a later change can't silently overwrite a migration that has already shipped.
|
|
55
|
+
|
|
56
|
+
## Implementing a Migration
|
|
57
|
+
|
|
58
|
+
A deterministic (or hybrid) migration is a function which mutates the virtual filesystem (the `Tree`), returning a [`MigrationReturnObject`](https://nx.dev/docs/reference/devkit/MigrationReturnObject). Here's a hybrid one, which returns both:
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
import { type MigrationReturnObject, type Tree } from '@nx/devkit';
|
|
62
|
+
import { formatFilesInSubtree } from '@aws/nx-plugin/sdk/utils/format';
|
|
63
|
+
|
|
64
|
+
export default async function migration(
|
|
65
|
+
tree: Tree,
|
|
66
|
+
): Promise<MigrationReturnObject> {
|
|
67
|
+
const nextSteps: string[] = [];
|
|
68
|
+
const agentContext: string[] = [];
|
|
69
|
+
|
|
70
|
+
// Read and update the files your generators produce here.
|
|
71
|
+
|
|
72
|
+
await formatFilesInSubtree(tree);
|
|
73
|
+
|
|
74
|
+
return { nextSteps, agentContext };
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
- **`nextSteps`** — the follow-up actions Nx prints for the user after `nx migrate` runs. Every entry should be something they need to do by hand: reconcile a file you skipped (see the guardrails below), or finish something beyond the migration's reach, like an install to refresh a lock file. They aren't a log of what you changed — when a transform applies cleanly, add nothing, otherwise the entries that do need action get buried.
|
|
79
|
+
- **`agentContext`** (hybrid only) — a summary of what the codemod changed, passed to the paired `prompt` when it runs under Nx's agentic flow so the agent can focus on the user-owned parts.
|
|
80
|
+
|
|
81
|
+
For some example operations you can perform in your migration (generating files, modifying existing files using GritQL, reading and updating JSON), refer to the <Link path="/guides/nx-generator">Nx Generator guide</Link>.
|
|
82
|
+
|
|
83
|
+
### Guardrails
|
|
84
|
+
|
|
85
|
+
Migrations run against workspaces you don't control, so it is recommended to adhere to the following (the scaffolded skeleton bakes these in):
|
|
86
|
+
|
|
87
|
+
- **Transform source code with GritQL.** Use the <Link path="/guides/nx-generator">GritQL helpers</Link> (`applyGritQL`, `matchGritQL`) to edit source rather than regexes or string replacements. GritQL matches the AST, so one pattern holds across the formatting, whitespace and quoting a user's copy may have drifted into, where text matching quietly misses equivalent code or corrupts the file. Use `updateJson` for JSON and the matching parser for other structured config.
|
|
88
|
+
- **Pattern-match before writing.** If a target file has diverged from the shape your generators produce, skip it and report it via `nextSteps`, or consider a hybrid migration, rather than clobbering the user's changes. `matchGritQL` is a convenient way to make that check.
|
|
89
|
+
- **Report only what's left to do.** `nextSteps` is for work the user has to pick up, not a record of the edits you made — those are already in their `git diff`.
|
|
90
|
+
- **Idempotent.** Re-running the migration must be a no-op. Guard GritQL rewrites that inject code with a `where { ... <: not contains ... }` clause so a second run doesn't append a duplicate.
|
|
91
|
+
- **Format what you write.** Finish with `formatFilesInSubtree(tree)` so the files your migration wrote are formatted correctly.
|
|
92
|
+
|
|
93
|
+
### Testing Your Migration
|
|
94
|
+
|
|
95
|
+
The scaffolded `migration.spec.ts` uses `createTreeUsingTsSolutionSetup()` to build a virtual workspace matching the shape our preset generates. Test that the migration takes a workspace from the "before" state to the target state, skips (and reports) code it doesn't recognise, and is idempotent:
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
import type { Tree } from '@nx/devkit';
|
|
99
|
+
import { createTreeUsingTsSolutionSetup } from '@aws/nx-plugin/sdk/utils/test';
|
|
100
|
+
import migration from './migration.js';
|
|
101
|
+
|
|
102
|
+
describe('my migration', () => {
|
|
103
|
+
let tree: Tree;
|
|
104
|
+
|
|
105
|
+
beforeEach(() => {
|
|
106
|
+
tree = createTreeUsingTsSolutionSetup();
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('should update the generated shape', async () => {
|
|
110
|
+
// Set up a tree with the "before" state
|
|
111
|
+
tree.write('project.json', JSON.stringify({ targets: { foo: {} } }));
|
|
112
|
+
|
|
113
|
+
// Run the migration
|
|
114
|
+
await migration(tree);
|
|
115
|
+
|
|
116
|
+
// Check that the tree matches the target state
|
|
117
|
+
const projectJson = JSON.parse(tree.read('project.json', 'utf-8'));
|
|
118
|
+
expect(projectJson.targets).toHaveProperty('bar');
|
|
119
|
+
expect(projectJson.targets).not.toHaveProperty('foo');
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
Unit tests only prove the migration does what you wrote it to do. Also test it against a workspace generated by the **published** version of your plugin — the state your users upgrade from — and confirm the result matches what your generators produce today: inspect the diff, generate the same projects into a fresh workspace and compare, check the workspace still builds, then re-run the migration to confirm it changes nothing. Repeat with a workspace you've customised first, to check the migration leaves your edits alone and reports them instead.
|
|
125
|
+
|
|
126
|
+
## Versioning
|
|
127
|
+
|
|
128
|
+
Nx runs a migration when the user's installed plugin version is less than the migration's `version`. This generator writes **no** `version` field — assign it (or stamp it from your release process) when you cut the release that ships the migration, so it runs for everyone upgrading from an earlier version.
|
|
129
|
+
|
|
130
|
+
Once you assign versions, a `migrations.json` registering one migration of each kind looks like this:
|
|
131
|
+
|
|
132
|
+
```json
|
|
133
|
+
{
|
|
134
|
+
"$schema": "http://json-schema.org/schema",
|
|
135
|
+
"name": "my-plugin",
|
|
136
|
+
"generators": {
|
|
137
|
+
"v2.0.0-rename-foo-target": {
|
|
138
|
+
"version": "2.0.0",
|
|
139
|
+
"description": "Rename the foo target to bar",
|
|
140
|
+
"implementation": "./src/migrations/v2.0.0/rename-foo-target/migration"
|
|
141
|
+
},
|
|
142
|
+
"v3.0.0-migrate-custom-handlers": {
|
|
143
|
+
"version": "3.0.0",
|
|
144
|
+
"description": "Update custom handlers for the new API",
|
|
145
|
+
"prompt": "./src/migrations/v3.0.0/migrate-custom-handlers/prompt.md"
|
|
146
|
+
},
|
|
147
|
+
"v4.0.0-upgrade-framework": {
|
|
148
|
+
"version": "4.0.0",
|
|
149
|
+
"description": "Upgrade the framework and reconcile call sites",
|
|
150
|
+
"implementation": "./src/migrations/v4.0.0/upgrade-framework/migration",
|
|
151
|
+
"prompt": "./src/migrations/v4.0.0/upgrade-framework/prompt.md"
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## Running Migrations
|
|
158
|
+
|
|
159
|
+
Users apply your plugin's migrations with the two-step `nx migrate` flow — generate the pending migrations, install, then run them:
|
|
160
|
+
|
|
161
|
+
<NxCommands commands={['migrate my-plugin@latest', 'migrate --run-migrations']} />
|
|
162
|
+
|
|
163
|
+
Agentic and hybrid migrations are applied by the user's local coding agent during `nx migrate --run-migrations` when one is available.
|
|
164
|
+
|
|
165
|
+
For the full flow from the upgrading user's perspective — including example output for deterministic and agentic migrations — see <Link path="get_started/upgrading">Upgrading Your Workspace</Link>.
|