@aws/nx-plugin-mcp 1.8.0 → 1.10.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.
|
@@ -381,6 +381,29 @@ function UserList() {
|
|
|
381
381
|
}
|
|
382
382
|
```
|
|
383
383
|
|
|
384
|
+
### Prefetching in Route Loaders
|
|
385
|
+
|
|
386
|
+
If your website uses TanStack Router, the generator also adds the query client and the tRPC options proxy to the router context. The context property is named after the API in camel case, so a `MyApi` API is available as `context.myApi` and several APIs can be connected to the same website. Use them to prefetch data in a route `loader`, before the route renders:
|
|
387
|
+
|
|
388
|
+
```tsx {5-7}
|
|
389
|
+
import { createFileRoute } from '@tanstack/react-router';
|
|
390
|
+
|
|
391
|
+
export const Route = createFileRoute('/users')({
|
|
392
|
+
loader: async ({ context }) => {
|
|
393
|
+
// The context properties are optional, so use a non-null assertion or a guard
|
|
394
|
+
const { queryClient, myApi } = context;
|
|
395
|
+
await queryClient!.ensureQueryData(myApi!.users.list.queryOptions());
|
|
396
|
+
},
|
|
397
|
+
component: UserList,
|
|
398
|
+
});
|
|
399
|
+
```
|
|
400
|
+
|
|
401
|
+
The component can then read the prefetched data with `useQuery(trpc.users.list.queryOptions())`, which resolves immediately from the cache.
|
|
402
|
+
|
|
403
|
+
:::note
|
|
404
|
+
An API named `Auth`, `QueryClient` or `RuntimeConfig` would clash with the `auth`, `queryClient` and `runtimeConfig` context properties, so it is not added to the router context and the generator logs a warning. Use a different API name if you need it in the router context.
|
|
405
|
+
:::
|
|
406
|
+
|
|
384
407
|
### Infinite Queries
|
|
385
408
|
|
|
386
409
|
Handle pagination with infinite queries:
|
package/docs/guides/trpc.mdx
CHANGED
|
@@ -4,7 +4,7 @@ description: Reference documentation for tRPC
|
|
|
4
4
|
generator: ts#api
|
|
5
5
|
when:
|
|
6
6
|
framework: [trpc]
|
|
7
|
-
infra: [rest-lambda, http-lambda]
|
|
7
|
+
infra: [rest-lambda, http-lambda, http-ecs]
|
|
8
8
|
---
|
|
9
9
|
import { FileTree, Tabs, TabItem, CardGrid } from '@astrojs/starlight/components';
|
|
10
10
|
import Astro from '@astrojs/react';
|
|
@@ -21,7 +21,17 @@ import { TS_VERSIONS } from '../../../../../../packages/nx-plugin/src/utils/vers
|
|
|
21
21
|
|
|
22
22
|
[tRPC](https://trpc.io/) is a framework for building APIs in TypeScript with end-to-end type safety. Using tRPC, updates to API operation inputs and outputs are immediately reflected in client code and are visible in your IDE without the need to rebuild your project.
|
|
23
23
|
|
|
24
|
-
The tRPC API generator creates a new tRPC API with AWS CDK or Terraform infrastructure setup. The generated backend
|
|
24
|
+
The tRPC API generator creates a new tRPC API with AWS CDK or Terraform infrastructure setup. The generated backend is exposed via an AWS API Gateway API, and includes schema validation using [Zod](https://zod.dev/). It sets up [AWS Lambda Powertools](https://docs.powertools.aws.dev/lambda/typescript/latest/) for observability, including structured logging and CloudWatch metrics.
|
|
25
|
+
|
|
26
|
+
<OptionFilter not when={{ infra: 'http-ecs' }} description="Lambda hosting">
|
|
27
|
+
AWS X-Ray tracing is set up too.
|
|
28
|
+
|
|
29
|
+
Your procedures run on AWS Lambda, so there is no server to manage and you pay only for the requests you serve.
|
|
30
|
+
</OptionFilter>
|
|
31
|
+
|
|
32
|
+
<OptionFilter when={{ infra: 'http-ecs' }} description="ECS hosting">
|
|
33
|
+
Your procedures run as a long-lived HTTP server in a container on Amazon ECS, behind an API Gateway HTTP API. 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.
|
|
34
|
+
</OptionFilter>
|
|
25
35
|
|
|
26
36
|
## Usage
|
|
27
37
|
|
|
@@ -43,8 +53,22 @@ Select `rest-lambda` (default) as your `infra` if you would like to use [tRPC Su
|
|
|
43
53
|
|
|
44
54
|
:::tip[Integration Pattern]
|
|
45
55
|
The `integrationPattern` option defaults to `isolated`, which creates one Lambda per tRPC procedure. Select `shared` if you would prefer a single shared Lambda handler for the whole API, with optional per-procedure overrides.
|
|
56
|
+
|
|
57
|
+
With `infra` set to `http-ecs` the option is always `shared`, since one container service answers every operation.
|
|
46
58
|
:::
|
|
47
59
|
|
|
60
|
+
<OptionFilter when={{ infra: 'http-ecs' }} description="ECS hosting is experimental">
|
|
61
|
+
:::caution[Experimental]
|
|
62
|
+
`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.
|
|
63
|
+
|
|
64
|
+
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.
|
|
65
|
+
|
|
66
|
+
`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.
|
|
67
|
+
|
|
68
|
+
The `architecture` option sets whether the container runs on `ARM64` or `X86_64`. Leave it out to use the architecture of the machine you run the generator on. See <Link path="/guides/ts-container#cpu-architecture">CPU Architecture</Link> in the container guide.
|
|
69
|
+
:::
|
|
70
|
+
</OptionFilter>
|
|
71
|
+
|
|
48
72
|
## Generator Output
|
|
49
73
|
|
|
50
74
|
The generator will create the following project structure in the `<directory>/<api-name>` directory:
|
|
@@ -82,6 +106,21 @@ The generator will create the following project structure in the `<directory>/<a
|
|
|
82
106
|
|
|
83
107
|
</FileTree>
|
|
84
108
|
|
|
109
|
+
<OptionFilter when={{ infra: 'http-ecs' }} description="Files that differ when the API runs on ECS">
|
|
110
|
+
With `infra` set to `http-ecs` the API runs as an HTTP server rather than a Lambda handler, so `src/handler.ts` and `src/local-server.ts` are not generated. In their place you get a container folder named after your API:
|
|
111
|
+
|
|
112
|
+
<FileTree>
|
|
113
|
+
- src
|
|
114
|
+
- \<api-name>-container/
|
|
115
|
+
- server.ts The `node:http` server: `GET /health`, and the tRPC standalone adapter for every procedure
|
|
116
|
+
- index.ts Entry point that reads `PORT` and `HOST` and starts the server. The bundle, the image and the `serve` target all start from this file
|
|
117
|
+
- Dockerfile Container image definition
|
|
118
|
+
- .dockerignore Files left out of the image build context
|
|
119
|
+
</FileTree>
|
|
120
|
+
|
|
121
|
+
Your router, procedures, schema, middleware and client are the same for every `infra` value.
|
|
122
|
+
</OptionFilter>
|
|
123
|
+
|
|
85
124
|
### Infrastructure
|
|
86
125
|
|
|
87
126
|
<Snippet name="shared-constructs" />
|
|
@@ -134,7 +173,15 @@ Zod provides powerful utilities to combine or derive schemas such as `.merge`, `
|
|
|
134
173
|
|
|
135
174
|
### Router and Procedures
|
|
136
175
|
|
|
137
|
-
Your tRPC router is defined in `src/router.ts`, which registers all procedures. Each procedure defines the expected input, output, and implementation.
|
|
176
|
+
Your tRPC router is defined in `src/router.ts`, which registers all procedures. Each procedure defines the expected input, output, and implementation.
|
|
177
|
+
|
|
178
|
+
<OptionFilter not when={{ infra: 'http-ecs' }} description="The Lambda entry point">
|
|
179
|
+
The Lambda handler entry point is in `src/handler.ts`, which forwards requests to your router.
|
|
180
|
+
</OptionFilter>
|
|
181
|
+
|
|
182
|
+
<OptionFilter when={{ infra: 'http-ecs' }} description="The container entry point">
|
|
183
|
+
The entry point is `src/<api-name>-container/server.ts`, the HTTP server the container runs, which forwards requests to your router.
|
|
184
|
+
</OptionFilter>
|
|
138
185
|
|
|
139
186
|
The sample router generated for you has a single operation, called `echo`:
|
|
140
187
|
|
|
@@ -299,13 +346,19 @@ export const echo = publicProcedure
|
|
|
299
346
|
|
|
300
347
|
For more information, please refer to the [AWS Lambda Powertools Tracer documentation](https://docs.powertools.aws.dev/lambda/typescript/latest/core/tracer/).
|
|
301
348
|
|
|
349
|
+
<OptionFilter when={{ infra: 'http-ecs' }} description="X-Ray produces no traces on ECS">
|
|
350
|
+
:::caution[No traces on ECS]
|
|
351
|
+
The calls above run without error in a container, but produce no traces. Powertools turns its tracer off when it is not running in Lambda, so `getSegment()` returns a placeholder subsegment that is never sent anywhere. See <a href="#observability">Observability</a> for what you do get, and what to add if you need traces.
|
|
352
|
+
:::
|
|
353
|
+
</OptionFilter>
|
|
354
|
+
|
|
302
355
|
### Implementing Custom Middleware
|
|
303
356
|
|
|
304
357
|
You can add additional values to the context provided to procedures by implementing middleware.
|
|
305
358
|
|
|
306
359
|
As an example, let's implement some middlware to extract some details about the calling user from our API in `src/middleware/identity.ts`.
|
|
307
360
|
|
|
308
|
-
<OptionFilter when={{ auth: 'iam' }} description="Identity middleware example for IAM-authenticated APIs">
|
|
361
|
+
<OptionFilter when={{ auth: 'iam', infra: ['rest-lambda', 'http-lambda'] }} description="Identity middleware example for IAM-authenticated APIs on Lambda">
|
|
309
362
|
This example walks through identity middleware for `IAM` authentication. We look up the caller in Cognito using the sub extracted from the API Gateway event.
|
|
310
363
|
|
|
311
364
|
The lookup uses the Cognito Identity Provider client, which is not a dependency of a generated tRPC API. Install it into your API project first:
|
|
@@ -490,7 +543,7 @@ export const createIdentityPlugin = () => {
|
|
|
490
543
|
</Tabs>
|
|
491
544
|
</OptionFilter>
|
|
492
545
|
|
|
493
|
-
<OptionFilter when={{ auth: 'cognito' }} description="Identity middleware example for Cognito-authenticated APIs">
|
|
546
|
+
<OptionFilter when={{ auth: 'cognito', infra: ['rest-lambda', 'http-lambda'] }} description="Identity middleware example for Cognito-authenticated APIs on Lambda">
|
|
494
547
|
When you deploy with `auth: 'cognito'`, the API Gateway Cognito authorizer verifies the JWT that the caller supplies in the `Authorization` header and places the verified claims on the Lambda event. Our middleware just reads those claims — no extra AWS SDK calls, no manual JWT verification.
|
|
495
548
|
|
|
496
549
|
First, we define what we'll add to the context:
|
|
@@ -641,7 +694,7 @@ The tRPC API generator creates CDK or Terraform infrastructure as code based on
|
|
|
641
694
|
<Fragment slot="cdk">
|
|
642
695
|
The CDK construct for deploying your API lives in the `common/constructs` folder. You can consume this in a CDK application, for example:
|
|
643
696
|
|
|
644
|
-
<OptionFilter when={{ auth: ['iam', 'custom'] }} description="CDK usage for IAM or Custom authentication">
|
|
697
|
+
<OptionFilter when={{ auth: ['iam', 'custom'], infra: ['rest-lambda', 'http-lambda'] }} description="CDK usage for IAM or Custom authentication on Lambda">
|
|
645
698
|
```ts {6-8}
|
|
646
699
|
import { MyApi } from '@my-scope/common-constructs';
|
|
647
700
|
|
|
@@ -662,7 +715,7 @@ Authorizer results are cached for 5 minutes, keyed on the `Authorization` header
|
|
|
662
715
|
:::
|
|
663
716
|
</OptionFilter>
|
|
664
717
|
|
|
665
|
-
<OptionFilter when={{ auth: 'cognito' }} description="CDK usage with Cognito authentication — pass the identity construct">
|
|
718
|
+
<OptionFilter when={{ auth: 'cognito', infra: ['rest-lambda', 'http-lambda'] }} description="CDK usage with Cognito authentication — pass the identity construct">
|
|
666
719
|
```ts {6,10}
|
|
667
720
|
import { MyApi, UserIdentity } from '@my-scope/common-constructs';
|
|
668
721
|
|
|
@@ -682,11 +735,63 @@ export class ExampleStack extends Stack {
|
|
|
682
735
|
The `UserIdentity` construct can be generated using the <Link path="/guides/react-website-auth">`ts#website#auth` generator</Link>.
|
|
683
736
|
</OptionFilter>
|
|
684
737
|
|
|
738
|
+
<OptionFilter when={{ infra: 'http-ecs' }} description="CDK usage when the API runs on ECS — pass the VPC">
|
|
739
|
+
`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.
|
|
740
|
+
|
|
741
|
+
```ts {10,13-15}
|
|
742
|
+
import { MyApi } from '@my-scope/common-constructs';
|
|
743
|
+
import { Stack } from 'aws-cdk-lib';
|
|
744
|
+
import { Vpc } from 'aws-cdk-lib/aws-ec2';
|
|
745
|
+
import { Construct } from 'constructs';
|
|
746
|
+
|
|
747
|
+
export class ExampleStack extends Stack {
|
|
748
|
+
constructor(scope: Construct, id: string) {
|
|
749
|
+
super(scope, id);
|
|
750
|
+
|
|
751
|
+
const vpc = new Vpc(this, 'Vpc', { natGateways: 1 });
|
|
752
|
+
|
|
753
|
+
const api = new MyApi(this, 'MyApi', {
|
|
754
|
+
integrations: MyApi.defaultIntegrations(this, { vpc }).build(),
|
|
755
|
+
});
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
```
|
|
759
|
+
|
|
760
|
+
To use an existing VPC, look it up with `Vpc.fromLookup(this, 'Vpc', { vpcId: '<vpc-id>' })` and pass it as `vpc`.
|
|
761
|
+
|
|
762
|
+
Pass `service` to change the container service itself — its `cpu`, `memoryLimitMiB`, `desiredCount`, `environment`, or `grantTaskRole` to give the task role a permission:
|
|
763
|
+
|
|
764
|
+
```ts
|
|
765
|
+
MyApi.defaultIntegrations(this, {
|
|
766
|
+
vpc,
|
|
767
|
+
service: {
|
|
768
|
+
cpu: 512,
|
|
769
|
+
memoryLimitMiB: 1024,
|
|
770
|
+
grantTaskRole: (taskRole) => myTable.grantReadWriteData(taskRole),
|
|
771
|
+
},
|
|
772
|
+
}).build();
|
|
773
|
+
```
|
|
774
|
+
|
|
775
|
+
With `auth` set to `cognito`, also pass `identity` to the construct as the Cognito example above does.
|
|
776
|
+
</OptionFilter>
|
|
777
|
+
|
|
778
|
+
<OptionFilter not when={{ infra: 'http-ecs' }} description="What the Lambda variants deploy">
|
|
685
779
|
This sets up your API infrastructure, including an AWS API Gateway REST or HTTP API, AWS Lambda functions for business logic, and authentication based on your chosen `auth` method.
|
|
780
|
+
</OptionFilter>
|
|
781
|
+
|
|
782
|
+
<OptionFilter when={{ infra: 'http-ecs' }} description="What the ECS variant deploys">
|
|
783
|
+
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.
|
|
784
|
+
</OptionFilter>
|
|
686
785
|
|
|
687
786
|
<Snippet name="api/cors-configuration-cdk-note" />
|
|
688
787
|
</Fragment>
|
|
689
788
|
<Fragment slot="terraform">
|
|
789
|
+
<OptionFilter when={{ infra: 'http-ecs' }} description="Terraform is not implemented for ECS hosting">
|
|
790
|
+
:::caution[CDK only]
|
|
791
|
+
`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.
|
|
792
|
+
:::
|
|
793
|
+
</OptionFilter>
|
|
794
|
+
|
|
690
795
|
The Terraform modules for deploying your API are in the `common/terraform` folder. You can use this in a Terraform configuration.
|
|
691
796
|
|
|
692
797
|
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:
|
|
@@ -891,13 +996,109 @@ The key outputs from the API module that you can use for IAM policies are:
|
|
|
891
996
|
|
|
892
997
|
<Snippet name="ts-bundle" />
|
|
893
998
|
|
|
999
|
+
<OptionFilter when={{ infra: 'http-ecs' }} description="How ECS hosting works, what it costs and what it limits">
|
|
1000
|
+
## Running on Amazon ECS
|
|
1001
|
+
|
|
1002
|
+
With `infra` set to `http-ecs`, your procedures run as a long-lived HTTP server in a container rather than in a Lambda function. A request takes this path:
|
|
1003
|
+
|
|
1004
|
+
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.
|
|
1005
|
+
2. API Gateway forwards the request through a VPC link, which is its private path into your VPC.
|
|
1006
|
+
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.
|
|
1007
|
+
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.
|
|
1008
|
+
|
|
1009
|
+
The tasks run in private subnets and never get a public IP address. Nothing in this path allows an address range inbound.
|
|
1010
|
+
|
|
1011
|
+
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.
|
|
1012
|
+
|
|
1013
|
+
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.
|
|
1014
|
+
|
|
1015
|
+
### What Runs in the Container
|
|
1016
|
+
|
|
1017
|
+
`src/<api-name>-container/server.ts` is a `node:http` server with two jobs. `GET /health` answers `{"status":"ok"}`, which is what the load balancer's target group and the container health check call. Every other request goes to the [tRPC standalone adapter](https://trpc.io/docs/server/adapters/standalone), which resolves it against your router.
|
|
1018
|
+
|
|
1019
|
+
There is no API Gateway event in the container, so the procedure context carries no `event`, `context` or `info` field: for `http-ecs` the vended `IMiddlewareContext` names only what the middleware puts there. The authorizer still decides whether a request reaches the container at all, but its claims do not arrive with the request, so the identity middleware examples above do not apply. To read the caller's identity in a procedure, map the authorizer's context onto a request header in your API construct and read that header.
|
|
1020
|
+
|
|
1021
|
+
### Observability
|
|
1022
|
+
|
|
1023
|
+
Logs and metrics work as they do on Lambda: the container writes to its own CloudWatch log group, and the metrics plugin publishes each request's metrics in the embedded metric format for CloudWatch Logs to extract.
|
|
1024
|
+
|
|
1025
|
+
The metrics plugin builds a metrics object for each request and hands it to your procedures as `opts.ctx.metrics`, so a metric one request adds is never published with another's. That matters more here than on Lambda, where one invocation owns the metrics from start to finish; this server handles requests at the same time. Add your own metrics through `opts.ctx.metrics` rather than a `Metrics` object of your own, which would share one store across every request in the process.
|
|
1026
|
+
|
|
1027
|
+
AWS X-Ray tracing does not. Powertools turns its tracer off outside Lambda, and there is no X-Ray daemon in the task to receive segments, so `opts.ctx.tracer` calls run without error and produce no traces. Add a daemon as a sidecar in your container service if you need them.
|
|
1028
|
+
|
|
1029
|
+
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.
|
|
1030
|
+
|
|
1031
|
+
### Image Build and Scanning
|
|
1032
|
+
|
|
1033
|
+
The container generator vends the image targets, named after your API:
|
|
1034
|
+
|
|
1035
|
+
- `<api-name>-container-docker` builds a linux/arm64 image, running `bundle` first so the image holds the bundled server.
|
|
1036
|
+
- `<api-name>-container-trivy` scans that image for vulnerabilities.
|
|
1037
|
+
|
|
1038
|
+
See <Link path="/guides/ts-container">TypeScript 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.
|
|
1039
|
+
|
|
1040
|
+
### Deploying and Tearing Down
|
|
1041
|
+
|
|
1042
|
+
`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`. 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.
|
|
1043
|
+
|
|
1044
|
+
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:
|
|
1045
|
+
|
|
1046
|
+
```bash
|
|
1047
|
+
aws cloudformation describe-stacks --stack-name <stack-name> \
|
|
1048
|
+
--query "Stacks[0].Outputs[?contains(OutputKey, 'Url')].OutputValue" --output text
|
|
1049
|
+
```
|
|
1050
|
+
|
|
1051
|
+
`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:
|
|
1052
|
+
|
|
1053
|
+
```bash
|
|
1054
|
+
aws cloudformation describe-stack-resources --stack-name <stack-name> \
|
|
1055
|
+
--query "StackResources[?contains(LogicalResourceId, 'AccessLogs') && (ResourceType=='AWS::Logs::LogGroup' || ResourceType=='AWS::KMS::Key')].[ResourceType,PhysicalResourceId]" \
|
|
1056
|
+
--output text
|
|
1057
|
+
```
|
|
1058
|
+
|
|
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
|
+
|
|
1061
|
+
### Limits
|
|
1062
|
+
|
|
1063
|
+
- **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
|
+
- **No streaming.** tRPC subscriptions need the `rest-lambda` infrastructure. An HTTP API cannot stream a response.
|
|
1065
|
+
- **Payload size.** Requests and responses follow API Gateway's 10 MB limit.
|
|
1066
|
+
- **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
|
+
|
|
1068
|
+
### Cost
|
|
1069
|
+
|
|
1070
|
+
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:
|
|
1071
|
+
|
|
1072
|
+
| Item | Approximately |
|
|
1073
|
+
| --- | --- |
|
|
1074
|
+
| Internal load balancer | $16 plus load balancer capacity units |
|
|
1075
|
+
| Fargate task, 0.25 vCPU / 0.5 GB arm64 | $7 per task |
|
|
1076
|
+
| NAT gateway, if your VPC has one | $33 |
|
|
1077
|
+
| VPC link | no hourly charge |
|
|
1078
|
+
| S3 storage for the load balancer's access logs | cents per GB a month |
|
|
1079
|
+
|
|
1080
|
+
So a one-task service costs roughly $23 a month 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.
|
|
1081
|
+
</OptionFilter>
|
|
1082
|
+
|
|
894
1083
|
## Local tRPC Server
|
|
895
1084
|
|
|
896
1085
|
You can use the `serve` target to run a local server for your API, for example:
|
|
897
1086
|
|
|
898
1087
|
<NxCommands commands={['serve my-api']} />
|
|
899
1088
|
|
|
1089
|
+
<OptionFilter not when={{ infra: 'http-ecs' }} description="Local server entry point for the Lambda variants">
|
|
900
1090
|
The entry point for the local server is `src/local-server.ts`.
|
|
1091
|
+
</OptionFilter>
|
|
1092
|
+
|
|
1093
|
+
<OptionFilter when={{ infra: 'http-ecs' }} description="Local server entry point when the API runs on ECS">
|
|
1094
|
+
The entry point for the local server is `src/<api-name>-container/index.ts`, the same file the container runs. It listens on the address in `HOST`, which defaults to `127.0.0.1`, so the local server accepts connections only from your own machine. The image sets `HOST=0.0.0.0` so the server listens on the task's own network interface, where the task's security group decides who may connect.
|
|
1095
|
+
|
|
1096
|
+
Check that the server is up with its health route, which returns status 200 and `{"status":"ok"}`:
|
|
1097
|
+
|
|
1098
|
+
```bash
|
|
1099
|
+
curl http://localhost:2022/health
|
|
1100
|
+
```
|
|
1101
|
+
</OptionFilter>
|
|
901
1102
|
|
|
902
1103
|
This will automatically reload when you make changes to your API.
|
|
903
1104
|
|
package/package.json
CHANGED
|
@@ -16,9 +16,11 @@
|
|
|
16
16
|
},
|
|
17
17
|
"integrationPattern": {
|
|
18
18
|
"type": "string",
|
|
19
|
-
"description": "How API Gateway integrations are generated for the API. Choose between isolated
|
|
20
|
-
"default": "isolated",
|
|
19
|
+
"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.",
|
|
21
20
|
"enum": ["isolated", "shared"],
|
|
21
|
+
"x-value-when": {
|
|
22
|
+
"isolated": { "infra": ["rest-lambda", "http-lambda", "none"] }
|
|
23
|
+
},
|
|
22
24
|
"x-priority": "important",
|
|
23
25
|
"x-prompt": {
|
|
24
26
|
"message": "How would you like to map integrations to your tRPC procedures?",
|
|
@@ -67,12 +69,17 @@
|
|
|
67
69
|
},
|
|
68
70
|
"infra": {
|
|
69
71
|
"type": "string",
|
|
70
|
-
"description": "The type of infrastructure to use to deploy this API.",
|
|
72
|
+
"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.",
|
|
71
73
|
"default": "rest-lambda",
|
|
72
|
-
"enum": ["rest-lambda", "http-lambda", "none"],
|
|
74
|
+
"enum": ["rest-lambda", "http-lambda", "http-ecs", "none"],
|
|
73
75
|
"x-prompt": "What infrastructure would you like to deploy your API with?",
|
|
74
76
|
"x-priority": "important"
|
|
75
77
|
},
|
|
78
|
+
"architecture": {
|
|
79
|
+
"type": "string",
|
|
80
|
+
"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.",
|
|
81
|
+
"enum": ["ARM64", "X86_64"]
|
|
82
|
+
},
|
|
76
83
|
"preferInstallDependencies": {
|
|
77
84
|
"type": "boolean",
|
|
78
85
|
"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.",
|
package/src/ts/api/schema.json
CHANGED
|
@@ -44,9 +44,11 @@
|
|
|
44
44
|
},
|
|
45
45
|
"integrationPattern": {
|
|
46
46
|
"type": "string",
|
|
47
|
-
"description": "How API Gateway integrations are generated for the API. Choose between isolated
|
|
48
|
-
"default": "isolated",
|
|
47
|
+
"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.",
|
|
49
48
|
"enum": ["isolated", "shared"],
|
|
49
|
+
"x-value-when": {
|
|
50
|
+
"isolated": { "infra": ["rest-lambda", "http-lambda", "none"] }
|
|
51
|
+
},
|
|
50
52
|
"x-priority": "important",
|
|
51
53
|
"x-prompt": {
|
|
52
54
|
"message": "How would you like to map integrations to your API operations?",
|
|
@@ -95,13 +97,21 @@
|
|
|
95
97
|
},
|
|
96
98
|
"infra": {
|
|
97
99
|
"type": "string",
|
|
98
|
-
"description": "The type of infrastructure to use to deploy this API.",
|
|
100
|
+
"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 trpc framework and the CDK IaC provider. It always uses integrationPattern shared, since one container service answers every operation.",
|
|
99
101
|
"default": "rest-lambda",
|
|
100
|
-
"enum": ["rest-lambda", "http-lambda", "none"],
|
|
101
|
-
"x-value-when": {
|
|
102
|
+
"enum": ["rest-lambda", "http-lambda", "http-ecs", "none"],
|
|
103
|
+
"x-value-when": {
|
|
104
|
+
"http-lambda": { "framework": "trpc" },
|
|
105
|
+
"http-ecs": { "framework": "trpc" }
|
|
106
|
+
},
|
|
102
107
|
"x-prompt": "What infrastructure would you like to deploy your API with?",
|
|
103
108
|
"x-priority": "important"
|
|
104
109
|
},
|
|
110
|
+
"architecture": {
|
|
111
|
+
"type": "string",
|
|
112
|
+
"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.",
|
|
113
|
+
"enum": ["ARM64", "X86_64"]
|
|
114
|
+
},
|
|
105
115
|
"preferInstallDependencies": {
|
|
106
116
|
"type": "boolean",
|
|
107
117
|
"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.",
|