@aws/nx-plugin-mcp 0.120.0 → 1.0.0-rc.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.
Files changed (92) hide show
  1. package/bin/aws-nx-mcp.js +74 -41
  2. package/docs/guides/astro-docs.mdx +144 -0
  3. package/docs/guides/connection/py-agent-a2a.mdx +105 -0
  4. package/docs/guides/connection/py-agent-mcp.mdx +147 -0
  5. package/docs/guides/connection/react-agui.mdx +240 -0
  6. package/docs/guides/connection/react-fastapi.mdx +1476 -0
  7. package/docs/guides/connection/react-py-agent.mdx +198 -0
  8. package/docs/guides/connection/react-smithy.mdx +1355 -0
  9. package/docs/guides/connection/react-trpc.mdx +447 -0
  10. package/docs/guides/connection/react-ts-agent.mdx +198 -0
  11. package/docs/guides/connection/smithy-rdb.mdx +161 -0
  12. package/docs/guides/connection/trpc-rdb.mdx +127 -0
  13. package/docs/guides/connection/ts-agent-a2a.mdx +106 -0
  14. package/docs/guides/connection/ts-agent-mcp.mdx +144 -0
  15. package/docs/guides/connection/ts-agent-rdb.mdx +141 -0
  16. package/docs/guides/connection/ts-mcp-server-rdb.mdx +135 -0
  17. package/docs/guides/connection.mdx +126 -0
  18. package/docs/guides/docker-bundling.mdx +403 -0
  19. package/docs/guides/docs.mdx +21 -0
  20. package/docs/guides/fastapi.mdx +511 -0
  21. package/docs/guides/license.mdx +293 -0
  22. package/docs/guides/nx-generator.mdx +588 -0
  23. package/docs/guides/py-agent.mdx +483 -0
  24. package/docs/guides/py-api.mdx +22 -0
  25. package/docs/guides/py-mcp-server.mdx +161 -0
  26. package/docs/guides/python-lambda-function.mdx +207 -0
  27. package/docs/guides/python-project.mdx +228 -0
  28. package/docs/guides/react-website-auth.mdx +216 -0
  29. package/docs/guides/react-website.mdx +424 -0
  30. package/docs/guides/runtime-config.mdx +312 -0
  31. package/docs/guides/terraform-project.mdx +317 -0
  32. package/docs/guides/trpc.mdx +818 -0
  33. package/docs/guides/ts-agent.mdx +436 -0
  34. package/docs/guides/ts-api.mdx +28 -0
  35. package/docs/guides/ts-lambda-function.mdx +218 -0
  36. package/docs/guides/ts-mcp-server.mdx +169 -0
  37. package/docs/guides/ts-nx-plugin.mdx +159 -0
  38. package/docs/guides/ts-rdb.mdx +759 -0
  39. package/docs/guides/ts-smithy-api.mdx +663 -0
  40. package/docs/guides/typescript-infrastructure.mdx +408 -0
  41. package/docs/guides/typescript-project.mdx +312 -0
  42. package/docs/guides/website.mdx +21 -0
  43. package/docs/guides/workspace.mdx +181 -0
  44. package/docs/snippets/agent/architecture.mdx +72 -0
  45. package/docs/snippets/agent/bedrock-deployment.mdx +172 -0
  46. package/docs/snippets/agent/runtime-arn.mdx +64 -0
  47. package/docs/snippets/api/api-architecture.mdx +93 -0
  48. package/docs/snippets/api/api-choice-note.mdx +6 -0
  49. package/docs/snippets/api/cors-configuration-cdk-note.mdx +25 -0
  50. package/docs/snippets/api/cors-configuration-terraform-note.mdx +28 -0
  51. package/docs/snippets/api/shared-constructs.mdx +38 -0
  52. package/docs/snippets/api/type-safe-api-integrations.mdx +643 -0
  53. package/docs/snippets/api/waf-configuration.mdx +37 -0
  54. package/docs/snippets/connection/a2a-infrastructure.mdx +63 -0
  55. package/docs/snippets/connection/lambda-rdb-ssl-requirements.mdx +40 -0
  56. package/docs/snippets/connection/mcp-server-rdb-ssl-requirements.mdx +35 -0
  57. package/docs/snippets/connection/rdb-api-infrastructure.mdx +72 -0
  58. package/docs/snippets/connection/react-agent-infrastructure.mdx +61 -0
  59. package/docs/snippets/connection/strands-agent-rdb-ssl-requirements.mdx +35 -0
  60. package/docs/snippets/lambda-function/architecture.mdx +36 -0
  61. package/docs/snippets/lambda-function/deploying-your-function.mdx +118 -0
  62. package/docs/snippets/mcp/architecture.mdx +58 -0
  63. package/docs/snippets/mcp/assistant-docs.mdx +10 -0
  64. package/docs/snippets/mcp/bedrock-deployment.mdx +167 -0
  65. package/docs/snippets/mcp/config.mdx +13 -0
  66. package/docs/snippets/mcp/configuration-py.mdx +42 -0
  67. package/docs/snippets/mcp/configuration-ts.mdx +53 -0
  68. package/docs/snippets/mcp/observability.mdx +8 -0
  69. package/docs/snippets/mcp/shared-constructs.mdx +32 -0
  70. package/docs/snippets/pdk-migration/example/01-migrate-api.mdx +602 -0
  71. package/docs/snippets/pdk-migration/example/02-migrate-website.mdx +915 -0
  72. package/docs/snippets/pdk-migration/example/03-migrate-infra.mdx +161 -0
  73. package/docs/snippets/pdk-migration/example/04-deploy.mdx +229 -0
  74. package/docs/snippets/pdk-migration/faq/aws-arch.mdx +17 -0
  75. package/docs/snippets/pdk-migration/faq/cdk-graph.mdx +29 -0
  76. package/docs/snippets/pdk-migration/faq/infrastructure-python-java.mdx +19 -0
  77. package/docs/snippets/pdk-migration/faq/pdk-nag.mdx +15 -0
  78. package/docs/snippets/pdk-migration/faq/pipeline.mdx +15 -0
  79. package/docs/snippets/pdk-migration/faq/type-safe-api.mdx +310 -0
  80. package/docs/snippets/pdk-migration/faq/use-of-projen.mdx +15 -0
  81. package/docs/snippets/prerequisites.mdx +20 -0
  82. package/docs/snippets/required-prerequisites.mdx +12 -0
  83. package/docs/snippets/shared-constructs.mdx +40 -0
  84. package/docs/snippets/tools/acurl.mdx +73 -0
  85. package/docs/snippets/ts-bundle.mdx +14 -0
  86. package/generators.json +47 -6
  87. package/package.json +1 -1
  88. package/src/py/api/schema.json +100 -0
  89. package/src/ts/api/schema.json +105 -0
  90. package/src/ts/docs/schema.json +70 -0
  91. package/src/ts/website/app/schema.json +97 -0
  92. package/src/ts/website/auth/schema.json +46 -0
@@ -0,0 +1,663 @@
1
+ ---
2
+ title: Smithy TypeScript API
3
+ description: Reference documentation for Smithy TypeScript API
4
+ generator: ts#smithy-api
5
+ when:
6
+ framework: [smithy]
7
+ ---
8
+
9
+ import { FileTree } from '@astrojs/starlight/components';
10
+ import Link from '@components/link.astro';
11
+ import RunGenerator from '@components/run-generator.astro';
12
+ import GeneratorParameters from '@components/generator-parameters.astro';
13
+ import NxCommands from '@components/nx-commands.astro';
14
+ import PackageManagerShortCommand from '@components/package-manager-short-command.astro';
15
+ import Infrastructure from '@components/infrastructure.astro';
16
+ import Snippet from '@components/snippet.astro';
17
+ import OptionFilter from '@components/option-filter.astro';
18
+
19
+ [Smithy](https://smithy.io/) is a protocol-agnostic interface definition language for authoring APIs in a model driven fashion.
20
+
21
+ The Smithy TypeScript API generator creates a new API using Smithy for service definition, and the [Smithy TypeScript Server SDK](https://github.com/awslabs/smithy-typescript) for implementation. The generator vends CDK or Terraform infrastructure as code to deploy your service to AWS Lambda, exposed via an AWS API Gateway REST API. It provides type-safe API development with automatic code generation from Smithy models. The generated handler uses [AWS Lambda Powertools for TypeScript](https://docs.powertools.aws.dev/lambda/typescript/latest/) for observability, including logging, AWS X-Ray tracing and CloudWatch Metrics
22
+
23
+ ## Usage
24
+
25
+ ### Generate a Smithy TypeScript API
26
+
27
+ You can generate a new Smithy TypeScript API in two ways:
28
+
29
+ <RunGenerator generator="ts#smithy-api" />
30
+
31
+ ### Options
32
+
33
+ <GeneratorParameters generator="ts#smithy-api" />
34
+
35
+ :::tip[Integration Pattern]
36
+ The `integrationPattern` option defaults to `isolated`, which creates one Lambda per Smithy operation. Select `shared` if you would prefer a single shared Lambda handler for the whole API, with optional per-operation overrides.
37
+ :::
38
+
39
+ ## Generator Output
40
+
41
+ The generator creates two related projects in the `<directory>/<api-name>` directory:
42
+
43
+ <FileTree>
44
+
45
+ - **model/** Smithy model project
46
+ - project.json Project configuration and build targets
47
+ - smithy-build.json Smithy build configuration
48
+ - build.Dockerfile Docker configuration for building Smithy artifacts
49
+ - src/
50
+ - main.smithy Main service definition
51
+ - operations/
52
+ - echo.smithy Example operation definition
53
+ - **backend/** TypeScript backend implementation
54
+ - project.json Project configuration and build targets
55
+ - rolldown.config.ts Bundle configuration
56
+ - src/
57
+ - handler.ts AWS Lambda handler
58
+ - local-server.ts Local development server
59
+ - service.ts Service implementation
60
+ - context.ts Service context definition
61
+ - operations/
62
+ - echo.ts Example operation implementation
63
+ - generated/ Generated TypeScript SDK (created during build)
64
+
65
+ </FileTree>
66
+
67
+ ### Infrastructure
68
+
69
+ Since this generator creates infrastructure as code based on your chosen `iacProvider`, it will create a project in `packages/common` which includes the relevant CDK constructs or Terraform modules.
70
+
71
+ The common infrastructure as code project is structured as follows:
72
+
73
+ <Infrastructure>
74
+ <Fragment slot="cdk">
75
+ <FileTree>
76
+ - packages/common/constructs
77
+ - src
78
+ - app/ Constructs for infrastructure specific to a project/generator
79
+ - apis/
80
+ - \<project-name>.ts CDK construct for deploying your API
81
+ - core/ Generic constructs which are reused by constructs in `app`
82
+ - api/
83
+ - rest-api.ts CDK construct for deploying a REST API
84
+ - utils.ts Utilities for the API constructs
85
+ - index.ts Entry point exporting constructs from `app`
86
+ - project.json Project build targets and configuration
87
+ </FileTree>
88
+
89
+ :::note[Generated Project]
90
+ This project is generated using the [`ts#project`](guides/typescript-project) generator and therefore configures the same build targets.
91
+ :::
92
+ </Fragment>
93
+ <Fragment slot="terraform">
94
+ <FileTree>
95
+ - packages/common/terraform
96
+ - src
97
+ - app/ Terraform modules for infrastructure specific to a project/generator
98
+ - apis/
99
+ - \<project-name>/
100
+ - \<project-name>.tf Module for deploying your API
101
+ - core/ Generic modules which are reused by modules in `app`
102
+ - api/
103
+ - rest-api/
104
+ - rest-api.tf Module for deploying a REST API
105
+ - project.json Project build targets and configuration
106
+ </FileTree>
107
+
108
+ :::note[Generated Project]
109
+ This project is generated using the [`terraform#project`](guides/terraform-project) generator and therefore configures the same build targets.
110
+ :::
111
+ </Fragment>
112
+ </Infrastructure>
113
+
114
+ #### Architecture
115
+
116
+ The deployed Smithy API has the following architecture, with an [AWS WAFv2](https://docs.aws.amazon.com/waf/latest/developerguide/waf-chapter.html) Web ACL in front of the API Gateway stage:
117
+
118
+ ```d2 inline=true
119
+ direction: right
120
+
121
+ client: Client {
122
+ shape: image
123
+ icon: /nx-plugin-for-aws/icons/aws/client.svg
124
+ }
125
+
126
+ waf: WAF {
127
+ shape: image
128
+ icon: /nx-plugin-for-aws/icons/aws/waf.svg
129
+ }
130
+
131
+ apigw: API Gateway\n(REST API) {
132
+ shape: image
133
+ icon: /nx-plugin-for-aws/icons/aws/api-gateway.svg
134
+ }
135
+
136
+ lambda: Lambda\n(Smithy Server SDK) {
137
+ shape: image
138
+ icon: /nx-plugin-for-aws/icons/aws/lambda.svg
139
+ }
140
+
141
+ cw: CloudWatch\n(Logs, Metrics) {
142
+ shape: image
143
+ icon: /nx-plugin-for-aws/icons/aws/cloudwatch.svg
144
+ near: top-right
145
+ }
146
+
147
+ xray: X-Ray\n(Traces) {
148
+ shape: image
149
+ icon: /nx-plugin-for-aws/icons/aws/xray.svg
150
+ near: bottom-right
151
+ }
152
+
153
+ client -> waf
154
+ waf -> apigw
155
+ apigw -> lambda
156
+ lambda -> cw
157
+ lambda -> xray
158
+ ```
159
+
160
+ ## Implementing your Smithy API
161
+
162
+ ### Defining Operations in Smithy
163
+
164
+ Operations are defined in Smithy files within the model project. The main service definition is in `main.smithy`:
165
+
166
+ ```smithy
167
+ $version: "2.0"
168
+
169
+ namespace your.namespace
170
+
171
+ use aws.protocols#restJson1
172
+ use smithy.framework#ValidationException
173
+
174
+ @title("YourService")
175
+ @restJson1
176
+ service YourService {
177
+ version: "1.0.0"
178
+ operations: [
179
+ Echo,
180
+ // Add your operations here
181
+ ]
182
+ errors: [
183
+ ValidationException
184
+ ]
185
+ }
186
+ ```
187
+
188
+ Individual operations are defined in separate files in the `operations/` directory:
189
+
190
+ ```smithy
191
+ $version: "2.0"
192
+
193
+ namespace your.namespace
194
+
195
+ @http(method: "POST", uri: "/echo")
196
+ operation Echo {
197
+ input: EchoInput
198
+ output: EchoOutput
199
+ }
200
+
201
+ structure EchoInput {
202
+ @required
203
+ message: String
204
+
205
+ foo: Integer
206
+ bar: String
207
+ }
208
+
209
+ structure EchoOutput {
210
+ @required
211
+ message: String
212
+ }
213
+ ```
214
+
215
+ :::note[Custom Folder Structure]
216
+ You can change the folder structure however you like - all `.smithy` files in the `src` folder will be included in the Smithy build.
217
+ :::
218
+
219
+ :::tip[Smithy Documentation]
220
+ For more details on Smithy and its syntax, refer to the [Smithy specification](https://smithy.io/2.0/spec/index.html).
221
+ :::
222
+
223
+ ### Implementing Operations in TypeScript
224
+
225
+ Operation implementations are located in the backend project's `src/operations/` directory. Each operation is implemented using the generated types from the TypeScript Server SDK (generated at build time from your Smithy model).
226
+
227
+ ```typescript
228
+ import { ServiceContext } from '../context.js';
229
+ import { Echo as EchoOperation } from '../generated/ssdk/index.js';
230
+
231
+ export const Echo: EchoOperation<ServiceContext> = async (input) => {
232
+ // Your business logic here
233
+ return {
234
+ message: `Echo: ${input.message}` // type-safe based on your Smithy model
235
+ };
236
+ };
237
+ ```
238
+
239
+ Operations must be registered to the service definition in `src/service.ts`:
240
+
241
+ ```typescript
242
+ import { ServiceContext } from './context.js';
243
+ import { YourServiceService } from './generated/ssdk/index.js';
244
+ import { Echo } from './operations/echo.js';
245
+ // Import other operations here
246
+
247
+ // Register operations to the service here
248
+ export const Service: YourServiceService<ServiceContext> = {
249
+ Echo,
250
+ // Add other operations here
251
+ };
252
+ ```
253
+
254
+ ### Service Context
255
+
256
+ You can define shared context for your operations in `context.ts`:
257
+
258
+ ```typescript
259
+ export interface ServiceContext {
260
+ // Powertools tracer, logger and metrics are provided by default
261
+ tracer: Tracer;
262
+ logger: Logger;
263
+ metrics: Metrics;
264
+ // Add shared dependencies, database connections, etc.
265
+ dbClient: any;
266
+ userIdentity: string;
267
+ }
268
+ ```
269
+
270
+ This context is passed to all operation implementations and can be used to share resources like database connections, configuration, or logging utilities.
271
+
272
+ :::caution[Manual Context Required]
273
+ You must construct the context yourself in both `handler.ts` (the Lambda function entry point) and `local-server.ts` (the entry point for running locally via the `serve` target).
274
+ :::
275
+
276
+ ### Observability with AWS Lambda Powertools
277
+
278
+ #### Logging
279
+
280
+ The generator configures structured logging using AWS Lambda Powertools with automatic context injection via Middy middleware.
281
+
282
+ ```typescript {4}
283
+ // handler.ts
284
+ export const handler = middy<APIGatewayProxyEvent, APIGatewayProxyResult>()
285
+ .use(captureLambdaHandler(tracer))
286
+ .use(injectLambdaContext(logger))
287
+ .use(logMetrics(metrics))
288
+ .handler(lambdaHandler);
289
+ ```
290
+
291
+ You can reference the logger from your operation implementations via the context:
292
+
293
+ ```typescript {6}
294
+ // operations/echo.ts
295
+ import { ServiceContext } from '../context.js';
296
+ import { Echo as EchoOperation } from '../generated/ssdk/index.js';
297
+
298
+ export const Echo: EchoOperation<ServiceContext> = async (input, ctx) => {
299
+ ctx.logger.info('Your log message');
300
+ // ...
301
+ };
302
+ ```
303
+
304
+ #### Tracing
305
+
306
+ AWS X-Ray tracing is configured automatically via the `captureLambdaHandler` middleware.
307
+
308
+ ```typescript {3}
309
+ // handler.ts
310
+ export const handler = middy<APIGatewayProxyEvent, APIGatewayProxyResult>()
311
+ .use(captureLambdaHandler(tracer))
312
+ .use(injectLambdaContext(logger))
313
+ .use(logMetrics(metrics))
314
+ .handler(lambdaHandler);
315
+ ```
316
+
317
+ You can add custom subsegments to your traces in your operations:
318
+
319
+ ```typescript {7, 11, 14}
320
+ // operations/echo.ts
321
+ import { ServiceContext } from '../context.js';
322
+ import { Echo as EchoOperation } from '../generated/ssdk/index.js';
323
+
324
+ export const Echo: EchoOperation<ServiceContext> = async (input, ctx) => {
325
+ // Creates a new subsegment
326
+ const subsegment = ctx.tracer.getSegment()?.addNewSubsegment('custom-operation');
327
+ try {
328
+ // Your logic here
329
+ } catch (error) {
330
+ subsegment?.addError(error as Error);
331
+ throw error;
332
+ } finally {
333
+ subsegment?.close();
334
+ }
335
+ };
336
+ ```
337
+
338
+ #### Metrics
339
+
340
+ CloudWatch metrics are collected automatically for each request via the `logMetrics` middleware.
341
+
342
+ ```typescript {5}
343
+ // handler.ts
344
+ export const handler = middy<APIGatewayProxyEvent, APIGatewayProxyResult>()
345
+ .use(captureLambdaHandler(tracer))
346
+ .use(injectLambdaContext(logger))
347
+ .use(logMetrics(metrics))
348
+ .handler(lambdaHandler);
349
+ ```
350
+
351
+ You can add custom metrics in your operations:
352
+
353
+ ```typescript {7}
354
+ // operations/echo.ts
355
+ import { MetricUnit } from '@aws-lambda-powertools/metrics';
356
+ import { ServiceContext } from '../context.js';
357
+ import { Echo as EchoOperation } from '../generated/ssdk/index.js';
358
+
359
+ export const Echo: EchoOperation<ServiceContext> = async (input, ctx) => {
360
+ ctx.metrics.addMetric("CustomMetric", MetricUnit.Count, 1);
361
+ // ...
362
+ };
363
+ ```
364
+
365
+ ### Error Handling
366
+
367
+ Smithy provides built-in error handling. You can define custom errors in your Smithy model:
368
+
369
+ ```smithy
370
+ @error("client")
371
+ @httpError(400)
372
+ structure InvalidRequestError {
373
+ @required
374
+ message: String
375
+ }
376
+ ```
377
+
378
+ And register them to your operation/service:
379
+
380
+ ```smithy
381
+ operation MyOperation {
382
+ ...
383
+ errors: [InvalidRequestError]
384
+ }
385
+ ```
386
+
387
+ Then throw them in your TypeScript implementation:
388
+
389
+ ```typescript
390
+ import { InvalidRequestError } from '../generated/ssdk/index.js';
391
+
392
+ export const MyOperation: MyOperationHandler<ServiceContext> = async (input) => {
393
+ if (!input.requiredField) {
394
+ throw new InvalidRequestError({
395
+ message: "Required field is missing"
396
+ });
397
+ }
398
+
399
+ return { /* success response */ };
400
+ };
401
+ ```
402
+
403
+ ## Building and Code Generation
404
+
405
+ The Smithy model project uses [Docker](https://www.docker.com/) to build the Smithy artifacts and generate the TypeScript Server SDK:
406
+
407
+ <NxCommands commands={['build <model-project>']} />
408
+
409
+ This process:
410
+
411
+ 1. **Compiles the Smithy model** and validates it
412
+ 2. **Generates OpenAPI specification** from the Smithy model
413
+ 3. **Creates TypeScript Server SDK** with type-safe operation interfaces
414
+ 4. **Outputs build artifacts** to `dist/<model-project>/build/`
415
+
416
+ The backend project automatically copies the generated SDK during compilation:
417
+
418
+ <NxCommands commands={['copy-ssdk <backend-project>']} />
419
+
420
+ ### Bundle Target
421
+
422
+ <Snippet name="ts-bundle" />
423
+
424
+ ## Local Development
425
+
426
+ The generator configures a local development server with hot reloading:
427
+
428
+ <NxCommands commands={['serve <backend-project>']} />
429
+
430
+ :::tip[Hot Reloading]
431
+ The local server will not only hot-reload when you make TypeScript changes to your backend code, but will reload when you make changes to your Smithy model project, allowing you to continuously iterate on your Smithy model and server together.
432
+ :::
433
+
434
+ ## Deploying your Smithy API
435
+
436
+ The generator creates CDK or Terraform infrastructure based on your selected `iacProvider`.
437
+
438
+ <Infrastructure>
439
+ <Fragment slot="cdk">
440
+ The CDK construct for deploying your API is in the `common/constructs` folder:
441
+
442
+ ```ts {6-8}
443
+ import { MyApi } from ':my-scope/common-constructs';
444
+
445
+ export class ExampleStack extends Stack {
446
+ constructor(scope: Construct, id: string) {
447
+ // Add the API to your stack
448
+ const api = new MyApi(this, 'MyApi', {
449
+ integrations: MyApi.defaultIntegrations(this).build(),
450
+ });
451
+ }
452
+ }
453
+ ```
454
+
455
+ This sets up:
456
+
457
+ 1. An AWS Lambda function for the Smithy service
458
+ 2. API Gateway REST API as the function trigger
459
+ 3. IAM roles and permissions
460
+ 4. CloudWatch log group
461
+ 5. X-Ray tracing configuration
462
+
463
+ <Snippet name="api/cors-configuration-cdk-note" />
464
+
465
+ <OptionFilter when={{ auth: 'Cognito' }} description="Cognito identity construct wiring">
466
+ :::note[Cognito Authentication]
467
+ If you selected `Cognito` authentication, you will need to supply the `identity` property to the API construct:
468
+
469
+ ```ts {9}
470
+ import { MyApi, UserIdentity } from ':my-scope/common-constructs';
471
+
472
+ export class ExampleStack extends Stack {
473
+ constructor(scope: Construct, id: string) {
474
+ const identity = new UserIdentity(this, 'Identity');
475
+
476
+ const api = new MyApi(this, 'MyApi', {
477
+ integrations: MyApi.defaultIntegrations(this).build(),
478
+ identity,
479
+ });
480
+ }
481
+ }
482
+ ```
483
+
484
+ The `UserIdentity` construct can be generated using the <Link path="/guides/react-website-auth">`ts#website#auth` generator</Link>
485
+ :::
486
+ </OptionFilter>
487
+
488
+ <OptionFilter when={{ auth: 'Custom' }} description="Custom Lambda Authorizer CDK usage">
489
+ :::caution[Custom Lambda Authorizer]
490
+ When using `Custom` auth, the construct creates a Lambda Authorizer internally from the generated `src/authorizer.ts` file, which **denies all requests by default**. You must implement your authorization logic in that file before your API will accept any traffic.
491
+ :::
492
+ </OptionFilter>
493
+ </Fragment>
494
+ <Fragment slot="terraform">
495
+ The Terraform modules for deploying your API are in the `common/terraform` folder.
496
+
497
+ 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:
498
+
499
+ ```hcl {1-3, 8}
500
+ module "asset_bucket" {
501
+ source = "../../common/terraform/src/core/asset-bucket"
502
+ }
503
+
504
+ module "my_api" {
505
+ source = "../../common/terraform/src/app/apis/my-api"
506
+
507
+ asset_bucket_name = module.asset_bucket.bucket_name
508
+
509
+ # Environment variables for the Lambda function
510
+ env = {
511
+ ENVIRONMENT = var.environment
512
+ LOG_LEVEL = "INFO"
513
+ }
514
+
515
+ # Additional IAM policies if needed
516
+ additional_iam_policy_statements = [
517
+ # Add any additional permissions your API needs
518
+ ]
519
+
520
+ tags = local.common_tags
521
+ }
522
+ ```
523
+
524
+ This sets up:
525
+
526
+ 1. An AWS Lambda function that serves the Smithy API
527
+ 2. API Gateway REST API as the function trigger
528
+ 3. IAM roles and permissions
529
+ 4. CloudWatch log group
530
+ 5. X-Ray tracing configuration
531
+ 6. CORS configuration
532
+
533
+ <Snippet name="api/cors-configuration-terraform-note" />
534
+
535
+ <OptionFilter when={{ auth: 'Cognito' }} description="Cognito module wiring">
536
+ :::note[Cognito Authentication]
537
+ If you selected `Cognito` authentication, you will need to supply the Cognito configuration:
538
+
539
+ ```hcl {3, 5-6}
540
+ module "my_api" {
541
+ source = "../../common/terraform/src/app/apis/my-api"
542
+
543
+ asset_bucket_name = module.asset_bucket.bucket_name
544
+
545
+ user_pool_id = local.user_pool_id
546
+ user_pool_client_ids = [local.client_id]
547
+
548
+ env = {
549
+ ENVIRONMENT = var.environment
550
+ LOG_LEVEL = "INFO"
551
+ }
552
+
553
+ tags = local.common_tags
554
+ }
555
+ ```
556
+ :::
557
+ </OptionFilter>
558
+
559
+ <OptionFilter when={{ auth: 'Custom' }} description="Custom Lambda Authorizer usage with Terraform">
560
+ :::caution[Custom Lambda Authorizer]
561
+ When using `Custom` auth, your API is protected by a Lambda Authorizer that **denies all requests by default**. You must implement your authorization logic in the generated `src/authorizer.ts` file before your API will accept any traffic.
562
+ :::
563
+ </OptionFilter>
564
+
565
+ The Terraform module provides several outputs:
566
+
567
+ ```hcl
568
+ # Access the API endpoint
569
+ output "api_url" {
570
+ value = module.my_api.stage_invoke_url
571
+ }
572
+
573
+ # Access Lambda function details
574
+ output "lambda_function_name" {
575
+ value = module.my_api.lambda_function_name
576
+ }
577
+ ```
578
+ </Fragment>
579
+ </Infrastructure>
580
+
581
+ ### WAF
582
+
583
+ <Snippet name="api/waf-configuration" parentHeading="WAF" />
584
+
585
+ ### Integrations
586
+
587
+ <Snippet name="api/type-safe-api-integrations" parentHeading="Integrations" />
588
+
589
+ #### Code Generation
590
+
591
+ <Infrastructure>
592
+ <Fragment slot="cdk">
593
+ Since operations are defined in Smithy, we use code generation to supply metadata to the CDK construct for type-safe integrations.
594
+
595
+ A `generate:<ApiName>-metadata` target is added to the common constructs `project.json` to facilitate this code generation, which emits a file such as `packages/common/constructs/src/generated/my-api/metadata.gen.ts`. Since this is generated at build time, it is ignored in version control.
596
+
597
+ :::note[Build Before Deploy]
598
+ You will need to run a build whenever you change your Smithy model to ensure the types consumed by the CDK construct are up to date.
599
+
600
+ <PackageManagerShortCommand commands={["build"]} />
601
+ :::
602
+
603
+ :::tip[Hot Reloading]
604
+ If you are actively working on both your CDK infrastructure and Smithy API together, you can use [`nx watch`](https://nx.dev/nx-api/nx/documents/watch) to regenerate these types every time you make model changes:
605
+
606
+ <NxCommands
607
+ commands={[
608
+ 'watch --projects=<ModelProject> -- \\ ',
609
+ 'run <InfraProject>:"generate:<ApiName>-metadata"',
610
+ ]}
611
+ />
612
+ :::
613
+ </Fragment>
614
+ <Fragment slot="terraform">
615
+ :::note[Terraform Limitations]
616
+ We do not support type-safe integrations for Terraform, and therefore no code generation targets are configured if you selected Terraform for your `iacProvider`.
617
+ :::
618
+ </Fragment>
619
+ </Infrastructure>
620
+
621
+ <OptionFilter when={{ auth: 'IAM' }} description="IAM-authenticated APIs only">
622
+ ### Granting Access (IAM Only)
623
+
624
+ If you selected `IAM` authentication, you can use the `grantInvokeAccess` method to grant access to your API:
625
+
626
+ <Infrastructure>
627
+ <Fragment slot="cdk">
628
+ ```ts
629
+ api.grantInvokeAccess(myIdentityPool.authenticatedRole);
630
+ ```
631
+ </Fragment>
632
+ <Fragment slot="terraform">
633
+ ```hcl
634
+ # Create an IAM policy to allow invoking the API
635
+ resource "aws_iam_policy" "api_invoke_policy" {
636
+ name = "MyApiInvokePolicy"
637
+ description = "Policy to allow invoking the Smithy API"
638
+
639
+ policy = jsonencode({
640
+ Version = "2012-10-17"
641
+ Statement = [
642
+ {
643
+ Effect = "Allow"
644
+ Action = "execute-api:Invoke"
645
+ Resource = "${module.my_api.api_execution_arn}/*/*"
646
+ }
647
+ ]
648
+ })
649
+ }
650
+
651
+ # Attach the policy to an IAM role
652
+ resource "aws_iam_role_policy_attachment" "api_invoke_access" {
653
+ role = aws_iam_role.authenticated_user_role.name
654
+ policy_arn = aws_iam_policy.api_invoke_policy.arn
655
+ }
656
+ ```
657
+ </Fragment>
658
+ </Infrastructure>
659
+ </OptionFilter>
660
+
661
+ ## Invoking your Smithy API
662
+
663
+ To invoke your API from a React website, you can use the <Link path="guides/connection/react-smithy">`connection`</Link> generator, which provides type-safe client generation from your Smithy model.