@aws/nx-plugin-mcp 1.6.0 → 1.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/aws-nx-mcp.js +50 -1
- package/docs/guides/connection/py-container-dynamodb.mdx +120 -0
- package/docs/guides/connection/py-container-rdb.mdx +153 -0
- package/docs/guides/connection/ts-container-dynamodb.mdx +122 -0
- package/docs/guides/connection/ts-container-rdb.mdx +155 -0
- package/docs/guides/docker-bundling.mdx +6 -0
- package/docs/guides/py-container.mdx +457 -0
- package/docs/guides/ts-container.mdx +457 -0
- package/generators.json +49 -1
- package/package.json +1 -1
- package/src/py/container/schema.json +47 -0
- package/src/py/dynamodb/container-connection/schema.json +27 -0
- package/src/py/rdb/container-connection/schema.json +27 -0
- package/src/ts/container/schema.json +47 -0
- package/src/ts/dynamodb/container-connection/schema.json +27 -0
- package/src/ts/rdb/container-connection/schema.json +27 -0
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: TypeScript Container Service
|
|
3
|
+
description: Add a containerised service to a TypeScript project and run it privately on Amazon ECS
|
|
4
|
+
generator: ts#container
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
import { FileTree } from '@astrojs/starlight/components';
|
|
8
|
+
import ArchitectureDiagram from '@components/architecture-diagram.astro';
|
|
9
|
+
import GeneratorParameters from '@components/generator-parameters.astro';
|
|
10
|
+
import Infrastructure from '@components/infrastructure.astro';
|
|
11
|
+
import Link from '@components/link.astro';
|
|
12
|
+
import NxCommands from '@components/nx-commands.astro';
|
|
13
|
+
import OptionFilter from '@components/option-filter.astro';
|
|
14
|
+
import RunGenerator from '@components/run-generator.astro';
|
|
15
|
+
import Snippet from '@components/snippet.astro';
|
|
16
|
+
|
|
17
|
+
This generator adds a containerised HTTP service to an existing TypeScript project. You get a small HTTP server with a `GET /health` route and a sample `GET /hello` route, a Dockerfile, and targets to run the server locally, build its image and scan the image. You also get CDK or Terraform code that runs the image on [Amazon Elastic Container Service (Amazon ECS)](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/Welcome.html), the AWS service that runs and scales containers.
|
|
18
|
+
|
|
19
|
+
## How This Works
|
|
20
|
+
|
|
21
|
+
The service runs on [AWS Fargate](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/AWS_Fargate.html), which runs each container without servers for you to manage. Each running copy of your container is an ECS task. The tasks run in your own VPC (virtual private cloud, an isolated network in your AWS account). A security group (a set of firewall rules attached to the tasks) controls which network traffic may reach them and leave them.
|
|
22
|
+
|
|
23
|
+
The service is private. It has no load balancer, no API and no public IP address. Its security group has no inbound rule, so nothing can open a connection to the service until you allow a caller. The service can still make outbound calls, for example to a database or to other AWS services.
|
|
24
|
+
|
|
25
|
+
:::note[Reaching Your Service Over HTTP]
|
|
26
|
+
The generated service is private and has no HTTP endpoint you can reach from outside the VPC. To serve HTTP clients, for example a website, an agent or a caller outside AWS, put an API in front of the service. Running the API generators (tRPC, FastAPI and Smithy) on Amazon ECS is planned as a follow-up. Until then, you can let a caller inside the VPC connect with `connections.allowFrom` in CDK or an ingress rule in Terraform, as "Letting a Caller Reach Your Service" below shows.
|
|
27
|
+
:::
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
### Add a Container Service
|
|
32
|
+
|
|
33
|
+
You can add a container service to a TypeScript project in two ways:
|
|
34
|
+
|
|
35
|
+
<RunGenerator generator="ts#container" />
|
|
36
|
+
|
|
37
|
+
:::tip[Project Prerequisite]
|
|
38
|
+
First use the <Link path="/guides/typescript-project">`ts#project`</Link> generator to create a project to add your service to.
|
|
39
|
+
:::
|
|
40
|
+
|
|
41
|
+
### Options
|
|
42
|
+
|
|
43
|
+
<GeneratorParameters generator="ts#container" />
|
|
44
|
+
|
|
45
|
+
With `infra` set to `none` the generator vends no infrastructure, and the service runs only locally.
|
|
46
|
+
|
|
47
|
+
## Generator Output
|
|
48
|
+
|
|
49
|
+
The generator adds the following files to your project. The source folder is named after the `name` option, or `container` if you leave it out:
|
|
50
|
+
|
|
51
|
+
<FileTree>
|
|
52
|
+
- your-project/
|
|
53
|
+
- src/
|
|
54
|
+
- container/ (or the name you chose)
|
|
55
|
+
- server.ts HTTP routes: `GET /health` and the sample route `GET /hello`
|
|
56
|
+
- index.ts Entry point that reads `PORT` and starts the server. The bundle and the `<name>-serve` target start from this file
|
|
57
|
+
- Dockerfile Container image definition
|
|
58
|
+
- .dockerignore Files left out of the image build context
|
|
59
|
+
- rolldown.config.ts Updated with a bundle entry for `index.ts`
|
|
60
|
+
- project.json Updated with the `bundle` target and the `<name>-serve`, `<name>-docker` and `<name>-trivy` targets for your service
|
|
61
|
+
</FileTree>
|
|
62
|
+
|
|
63
|
+
Target names start with the `name` option, or with `container` if you leave it out. For example, the targets are `container-serve`, `container-docker` and `container-trivy` without a name, and `orders-serve`, `orders-docker` and `orders-trivy` for `--name=orders`. The `bundle` target is shared by every bundled entry point in the project.
|
|
64
|
+
|
|
65
|
+
<OptionFilter when={{ infra: 'ecs' }} description="ECS service infrastructure">
|
|
66
|
+
### Infrastructure
|
|
67
|
+
|
|
68
|
+
<Snippet name="shared-constructs" />
|
|
69
|
+
|
|
70
|
+
For deploying your service, the following files are generated. `<name>` is your service name in kebab-case: the `name` option, or the last part of your project name followed by `-container` when you leave the option out. Project `orders` with no `name` option gives `orders-container`, and `--name=api` gives `api`:
|
|
71
|
+
|
|
72
|
+
<Infrastructure>
|
|
73
|
+
<Fragment slot="cdk">
|
|
74
|
+
<FileTree>
|
|
75
|
+
- packages/common/constructs/src
|
|
76
|
+
- app
|
|
77
|
+
- services
|
|
78
|
+
- \<name>.ts CDK construct for your service, with its image
|
|
79
|
+
- core
|
|
80
|
+
- container
|
|
81
|
+
- ecs-service.ts Generic construct for an ECS service on Fargate
|
|
82
|
+
</FileTree>
|
|
83
|
+
</Fragment>
|
|
84
|
+
<Fragment slot="terraform">
|
|
85
|
+
<FileTree>
|
|
86
|
+
- packages/common/terraform/src
|
|
87
|
+
- app
|
|
88
|
+
- services
|
|
89
|
+
- \<name>
|
|
90
|
+
- \<name>.tf Module for your service, which publishes its image
|
|
91
|
+
- core
|
|
92
|
+
- container
|
|
93
|
+
- ecs-service
|
|
94
|
+
- ecs-service.tf Generic module for an ECS service on Fargate
|
|
95
|
+
- variables.tf Inputs of the generic module
|
|
96
|
+
- outputs.tf Outputs of the generic module
|
|
97
|
+
</FileTree>
|
|
98
|
+
</Fragment>
|
|
99
|
+
</Infrastructure>
|
|
100
|
+
|
|
101
|
+
The construct and the module create an ECS cluster with Container Insights (CloudWatch metrics for the cluster's containers), a task definition, the service, a task role and an execution role, a log group encrypted with an AWS Key Management Service (AWS KMS) key, CPU-based autoscaling and the service's security group.
|
|
102
|
+
</OptionFilter>
|
|
103
|
+
|
|
104
|
+
<OptionFilter when={{ infra: 'none' }} description="No infrastructure is vended for infra=none">
|
|
105
|
+
If you set `infra` to `none`, no CDK constructs or Terraform modules are generated, and the service runs only locally.
|
|
106
|
+
</OptionFilter>
|
|
107
|
+
|
|
108
|
+
#### Architecture
|
|
109
|
+
|
|
110
|
+
<ArchitectureDiagram />
|
|
111
|
+
|
|
112
|
+
## Running Your Service Locally
|
|
113
|
+
|
|
114
|
+
The `<name>-serve` target runs the server on your machine, without a container:
|
|
115
|
+
|
|
116
|
+
<NxCommands commands={['run your-project:container-serve']} />
|
|
117
|
+
|
|
118
|
+
The server listens on the port in the `PORT` environment variable. The `<name>-serve` target sets it to a port the generator assigns, starting at 8081 and skipping ports other projects in your workspace already use. Inside the container the server listens on port 8080.
|
|
119
|
+
|
|
120
|
+
The server listens on the address in the `HOST` environment variable, which defaults to `127.0.0.1`, so the `<name>-serve` target accepts connections only from your own machine. The image sets `HOST=0.0.0.0`, so inside a task the server listens on the task's own network interface. The task's security group still decides who may connect.
|
|
121
|
+
|
|
122
|
+
Check that the server is up with its health route, which returns status 200 and `{"status":"ok"}`:
|
|
123
|
+
|
|
124
|
+
```bash
|
|
125
|
+
curl http://localhost:<port>/health
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
The server also has a sample route that you replace with your own routes. `GET /hello?name=<x>` returns `{"message":"Hello, <x>!"}`, and `name` defaults to `world`. Any other path returns status 404.
|
|
129
|
+
|
|
130
|
+
## Building and Scanning the Image
|
|
131
|
+
|
|
132
|
+
The `<name>-docker` target builds a linux/arm64 image of your service. It runs the `bundle` target first, so the image holds the bundled server. It then copies the `Dockerfile` and `.dockerignore` into the bundle output folder, `dist/<project root>/bundle/container/<name>`, and builds the image with that folder as the build context (the files the image build can read). It uses the container engine your workspace is set up with, `docker` or [Finch](https://runfinch.com/). The target runs as part of the aggregate `docker` target, which builds every image in the project.
|
|
133
|
+
|
|
134
|
+
<NxCommands commands={['run your-project:container-docker']} />
|
|
135
|
+
|
|
136
|
+
On ECS, a health check command runs inside the container and calls `GET /health` on the container port. It needs no inbound access. ECS replaces a task whose health check keeps failing. ECS ignores a `HEALTHCHECK` instruction in the Dockerfile. For more on how images are built and published, see <Link path="/guides/docker-bundling">Docker Bundling</Link>.
|
|
137
|
+
|
|
138
|
+
### Image Scanning
|
|
139
|
+
|
|
140
|
+
<Snippet name="trivy-image-scan" parentHeading="Image Scanning" />
|
|
141
|
+
|
|
142
|
+
<OptionFilter when={{ infra: 'ecs' }} description="Deploying the ECS service">
|
|
143
|
+
## Deploying Your Service
|
|
144
|
+
|
|
145
|
+
### Adding the Service to Your Infrastructure
|
|
146
|
+
|
|
147
|
+
The construct and the module never create a VPC. You create or import one in your stack or root module and pass it in. The tasks run in private subnets with egress, which reach AWS services through a NAT gateway (a managed gateway that lets private subnets make outbound connections) or through VPC endpoints (private connections from your VPC to AWS services).
|
|
148
|
+
|
|
149
|
+
<Infrastructure>
|
|
150
|
+
<Fragment slot="cdk">
|
|
151
|
+
The CDK construct is created in `common/constructs`. Its class name is `<name>` in PascalCase, so project `orders` with no `name` option gives the class `OrdersContainer` in `packages/common/constructs/src/app/services/orders-container.ts`, and `--name=api` gives `Api` in `api.ts`. `MyService` below is a placeholder for your own class, which your `@<scope>/common-constructs` package exports under that name. The package exports no construct called `Service`, so if your editor offers one it is `Service` from `aws-cdk-lib/aws-servicediscovery`.
|
|
152
|
+
|
|
153
|
+
Example usage with a new VPC:
|
|
154
|
+
|
|
155
|
+
```ts title="packages/infra/src/stacks/application-stack.ts"
|
|
156
|
+
import { MyService } from '@my-scope/common-constructs';
|
|
157
|
+
import { Stack, StackProps } from 'aws-cdk-lib';
|
|
158
|
+
import { SubnetType, Vpc } from 'aws-cdk-lib/aws-ec2';
|
|
159
|
+
import { Construct } from 'constructs';
|
|
160
|
+
|
|
161
|
+
export class ApplicationStack extends Stack {
|
|
162
|
+
constructor(scope: Construct, id: string, props?: StackProps) {
|
|
163
|
+
super(scope, id, props);
|
|
164
|
+
|
|
165
|
+
const vpc = new Vpc(this, 'Vpc', { natGateways: 1 });
|
|
166
|
+
|
|
167
|
+
new MyService(this, 'MyService', {
|
|
168
|
+
vpc,
|
|
169
|
+
vpcSubnets: { subnetType: SubnetType.PRIVATE_WITH_EGRESS },
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
To use an existing VPC, look it up with `Vpc.fromLookup(this, 'Vpc', { vpcId: '<vpc-id>' })` and pass it as `vpc`.
|
|
176
|
+
</Fragment>
|
|
177
|
+
<Fragment slot="terraform">
|
|
178
|
+
The Terraform module is created in `common/terraform`, in `app/services/<name>/<name>.tf`. Project `orders` with no `name` option gives `packages/common/terraform/src/app/services/orders-container/orders-container.tf`, and `my-service` below is a placeholder for your own module folder. The service's image is published to the shared asset repository, so instantiate the `core/asset-ecr` module once per deployment and pass its outputs to every service module. Also pass the outputs of the shared `runtime_config_appconfig` module, which hold the AWS AppConfig application the service reads its <Link path="/guides/runtime-config">runtime configuration</Link> from:
|
|
179
|
+
|
|
180
|
+
```hcl title="packages/infra/src/main.tf"
|
|
181
|
+
module "asset_ecr" {
|
|
182
|
+
source = "../../common/terraform/src/core/asset-ecr"
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
module "my_service" {
|
|
186
|
+
source = "../../common/terraform/src/app/services/my-service"
|
|
187
|
+
|
|
188
|
+
vpc_id = aws_vpc.main.id
|
|
189
|
+
subnet_ids = aws_subnet.private[*].id
|
|
190
|
+
|
|
191
|
+
asset_ecr_repository_url = module.asset_ecr.repository_url
|
|
192
|
+
asset_ecr_repository_arn = module.asset_ecr.repository_arn
|
|
193
|
+
|
|
194
|
+
appconfig_application_id = module.runtime_config_appconfig.application_id
|
|
195
|
+
appconfig_application_arn = module.runtime_config_appconfig.application_arn
|
|
196
|
+
}
|
|
197
|
+
```
|
|
198
|
+
</Fragment>
|
|
199
|
+
</Infrastructure>
|
|
200
|
+
|
|
201
|
+
### Running Without a NAT Gateway
|
|
202
|
+
|
|
203
|
+
When your tasks start, ECS pulls the image and sends logs on their behalf. Your server code may also call AWS services. In private subnets with no NAT gateway, each of these calls needs a VPC endpoint. Without the right endpoints, tasks fail to start or time out when they call AWS.
|
|
204
|
+
|
|
205
|
+
Create these endpoints in the VPC, replacing `<region>` with your AWS Region:
|
|
206
|
+
|
|
207
|
+
| Endpoint | Type | What uses it |
|
|
208
|
+
| --- | --- | --- |
|
|
209
|
+
| `com.amazonaws.<region>.ecr.api` | Interface | ECS, to authenticate with Amazon ECR and look up the image |
|
|
210
|
+
| `com.amazonaws.<region>.ecr.dkr` | Interface | ECS, to pull the image from Amazon ECR |
|
|
211
|
+
| `com.amazonaws.<region>.s3` | Gateway | ECS, to download image layers, which Amazon ECR stores in Amazon S3 |
|
|
212
|
+
| `com.amazonaws.<region>.logs` | Interface | The container's log output, sent to Amazon CloudWatch Logs |
|
|
213
|
+
| `com.amazonaws.<region>.secretsmanager` | Interface | ECS, to read the secrets you pass to the container when a task starts |
|
|
214
|
+
| `com.amazonaws.<region>.appconfigdata` | Interface | Your code, to read <Link path="/guides/runtime-config">runtime configuration</Link> from AWS AppConfig |
|
|
215
|
+
| `com.amazonaws.<region>.sts` | Interface | Your code, when it requests temporary credentials from AWS STS (for example to assume another role) |
|
|
216
|
+
|
|
217
|
+
Interface endpoints need a security group that allows HTTPS (port 443) from the tasks. For details on the Amazon ECR endpoints, see [Amazon ECR interface VPC endpoints](https://docs.aws.amazon.com/AmazonECR/latest/userguide/vpc-endpoints.html).
|
|
218
|
+
|
|
219
|
+
### Security Defaults
|
|
220
|
+
|
|
221
|
+
The generated infrastructure applies these settings by default:
|
|
222
|
+
|
|
223
|
+
- The service's security group has no inbound rule. No caller inside or outside the VPC can connect to the tasks until you add a rule.
|
|
224
|
+
- Tasks run in private subnets and never get a public IP address.
|
|
225
|
+
- The generator creates no load balancer and no API in front of the service.
|
|
226
|
+
- Tasks may send outbound traffic only on port 443. Connections to databases add the database port.
|
|
227
|
+
- The task role (the role your server code runs as) may only read runtime configuration from AWS AppConfig until you or a connection grant more.
|
|
228
|
+
- The execution role (the role ECS uses to start your task) may only pull the service's image, write to its log group and read the secrets you list.
|
|
229
|
+
- Secrets reach the container only as references to AWS Secrets Manager or AWS Systems Manager Parameter Store, never as plain environment values.
|
|
230
|
+
- The container has a read-only root file system with a writable `/tmp` folder, and runs as uid 1000, a non-root user. ECS Exec (shell access into a running task) is off.
|
|
231
|
+
- A deployment that fails its health checks stops and rolls back to the previous version.
|
|
232
|
+
- The log group is encrypted with an AWS KMS key, and logs are kept for one year.
|
|
233
|
+
|
|
234
|
+
### Letting a Caller Reach Your Service
|
|
235
|
+
|
|
236
|
+
This section is about callers inside the VPC. An HTTP client outside the VPC needs an API in front of the service, as the note at the top of this guide explains.
|
|
237
|
+
|
|
238
|
+
Nothing can call the service as generated. If another workload in the VPC needs to call it, for example a Lambda function or another ECS service, you add an inbound rule to the service's security group yourself. The generator adds no inbound rule. Allow one named caller by its security group, on the container port, and never allow an address range.
|
|
239
|
+
|
|
240
|
+
<Infrastructure>
|
|
241
|
+
<Fragment slot="cdk">
|
|
242
|
+
The construct implements `IConnectable`, so allow the caller's security group through its `connections`:
|
|
243
|
+
|
|
244
|
+
```ts title="packages/infra/src/stacks/application-stack.ts"
|
|
245
|
+
import { Port } from 'aws-cdk-lib/aws-ec2';
|
|
246
|
+
|
|
247
|
+
const svc = new MyService(this, 'MyService', { vpc });
|
|
248
|
+
svc.connections.allowFrom(callerSecurityGroup, Port.tcp(8080));
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
If the caller's security group is defined in your CDK app and limits outbound traffic, `allowFrom` also adds the matching outbound rule to it.
|
|
252
|
+
</Fragment>
|
|
253
|
+
<Fragment slot="terraform">
|
|
254
|
+
Add an inbound rule to the service's security group, which your service's module outputs as `security_group_id`, with the caller's security group as the source:
|
|
255
|
+
|
|
256
|
+
```hcl title="packages/infra/src/main.tf"
|
|
257
|
+
resource "aws_vpc_security_group_ingress_rule" "caller_to_my_service" {
|
|
258
|
+
security_group_id = module.my_service.security_group_id
|
|
259
|
+
referenced_security_group_id = aws_security_group.caller.id
|
|
260
|
+
from_port = 8080
|
|
261
|
+
to_port = 8080
|
|
262
|
+
ip_protocol = "tcp"
|
|
263
|
+
}
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
If the caller's security group limits outbound traffic, also add an `aws_vpc_security_group_egress_rule` on the caller's group to the service's security group.
|
|
267
|
+
</Fragment>
|
|
268
|
+
</Infrastructure>
|
|
269
|
+
|
|
270
|
+
The caller connects to a task's private IP address. The generator sets up no service discovery, and a task's address changes when ECS replaces the task. To give callers a stable name, set up service discovery yourself, for example with [Amazon ECS Service Connect](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/service-connect.html).
|
|
271
|
+
|
|
272
|
+
### Advanced Configuration
|
|
273
|
+
|
|
274
|
+
The generated service sets its name and image for you. The examples below use the generic `EcsService` construct and `ecs-service` module directly, for settings the generated service does not pass.
|
|
275
|
+
|
|
276
|
+
In CDK, your `common-constructs` package exports `EcsService` next to your service's construct, so you import it the same way: `import { EcsService } from '@my-scope/common-constructs'`. The generated construct extends `EcsService` and passes every other prop through, for example `cluster`, `environment`, `secrets` and `grantTaskRole`. Its optional `image` prop replaces the image built from your project.
|
|
277
|
+
|
|
278
|
+
In Terraform, your service's module lets you set only some of the `ecs-service` module's variables: `vpc_id`, `subnet_ids`, `cluster_arn`, `environment`, `secrets`, `task_role_policy_statements` and `tags`. To set any other variable, edit the `module "ecs_service"` block in your service's module.
|
|
279
|
+
|
|
280
|
+
#### Prebuilt Image
|
|
281
|
+
|
|
282
|
+
To run an image that is already built, for example from a public registry, pass its URI. Nothing is built locally. For an image in a private Amazon ECR repository, CDK grants the execution role pull access on that repository. Terraform grants pull access only when you also pass the repository's ARN as `image_repository_arn`.
|
|
283
|
+
|
|
284
|
+
<Infrastructure>
|
|
285
|
+
<Fragment slot="cdk">
|
|
286
|
+
|
|
287
|
+
```ts
|
|
288
|
+
new EcsService(this, 'Prebuilt', {
|
|
289
|
+
serviceName: 'Prebuilt',
|
|
290
|
+
vpc,
|
|
291
|
+
image: { imageUri: 'public.ecr.aws/my-org/my-image@sha256:<digest>' },
|
|
292
|
+
});
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
</Fragment>
|
|
296
|
+
<Fragment slot="terraform">
|
|
297
|
+
|
|
298
|
+
```hcl
|
|
299
|
+
module "prebuilt" {
|
|
300
|
+
source = "../../common/terraform/src/core/container/ecs-service"
|
|
301
|
+
|
|
302
|
+
service_name = "Prebuilt"
|
|
303
|
+
vpc_id = aws_vpc.main.id
|
|
304
|
+
subnet_ids = aws_subnet.private[*].id
|
|
305
|
+
image_uri = "public.ecr.aws/my-org/my-image@sha256:<digest>"
|
|
306
|
+
# For an image in a private Amazon ECR repository, also pass:
|
|
307
|
+
# image_repository_arn = aws_ecr_repository.my_image.arn
|
|
308
|
+
}
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
</Fragment>
|
|
312
|
+
</Infrastructure>
|
|
313
|
+
|
|
314
|
+
#### Several Services in One Stack
|
|
315
|
+
|
|
316
|
+
Each service creates its own ECS cluster unless you pass one in. To run several services in one cluster, pass the same cluster to each.
|
|
317
|
+
|
|
318
|
+
<Infrastructure>
|
|
319
|
+
<Fragment slot="cdk">
|
|
320
|
+
|
|
321
|
+
```ts
|
|
322
|
+
const cluster = new Cluster(this, 'Cluster', { vpc });
|
|
323
|
+
|
|
324
|
+
new MyService(this, 'MyService', { vpc, cluster });
|
|
325
|
+
new MyOtherService(this, 'MyOtherService', { vpc, cluster });
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
</Fragment>
|
|
329
|
+
<Fragment slot="terraform">
|
|
330
|
+
Pass the same `cluster_arn` to each `ecs-service` module. The `cluster_arn` output of the first service gives you one:
|
|
331
|
+
|
|
332
|
+
```hcl
|
|
333
|
+
cluster_arn = module.my_service.cluster_arn
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
</Fragment>
|
|
337
|
+
</Infrastructure>
|
|
338
|
+
|
|
339
|
+
#### Environment Variables and Secrets
|
|
340
|
+
|
|
341
|
+
Plain settings go in `environment`. Never put secret values there, because anyone who can read the task definition can read them. Pass secrets as references instead. The execution role may read only the secrets you list.
|
|
342
|
+
|
|
343
|
+
<Infrastructure>
|
|
344
|
+
<Fragment slot="cdk">
|
|
345
|
+
|
|
346
|
+
```ts
|
|
347
|
+
new EcsService(this, 'MyService', {
|
|
348
|
+
// ...
|
|
349
|
+
environment: { LOG_LEVEL: 'info' },
|
|
350
|
+
secrets: { API_KEY: Secret.fromSecretsManager(apiKeySecret) },
|
|
351
|
+
});
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
</Fragment>
|
|
355
|
+
<Fragment slot="terraform">
|
|
356
|
+
|
|
357
|
+
```hcl
|
|
358
|
+
environment = { LOG_LEVEL = "info" }
|
|
359
|
+
secrets = { API_KEY = aws_secretsmanager_secret.api_key.arn }
|
|
360
|
+
```
|
|
361
|
+
|
|
362
|
+
</Fragment>
|
|
363
|
+
</Infrastructure>
|
|
364
|
+
|
|
365
|
+
#### Sizing and Scaling
|
|
366
|
+
|
|
367
|
+
Each task gets 256 CPU units (a quarter of a vCPU) and 512 MiB of memory by default. The service starts one task, and autoscaling keeps between one and two tasks running, adding tasks when average CPU use goes above 70%.
|
|
368
|
+
|
|
369
|
+
| Setting | CDK prop | Terraform variable | Default |
|
|
370
|
+
| --- | --- | --- | --- |
|
|
371
|
+
| CPU units per task | `cpu` | `cpu` | 256 |
|
|
372
|
+
| Memory per task (MiB) | `memoryLimitMiB` | `memory` | 512 |
|
|
373
|
+
| Tasks at start | `desiredCount` | `desired_count` | 1 |
|
|
374
|
+
| Fewest tasks | `minCapacity` | `min_capacity` | 1 |
|
|
375
|
+
| Most tasks | `maxCapacity` | `max_capacity` | 2 |
|
|
376
|
+
| Target average CPU (%) | `targetCpuUtilizationPercent` | `target_cpu_utilization` | 70 |
|
|
377
|
+
|
|
378
|
+
#### Task Role Permissions
|
|
379
|
+
|
|
380
|
+
The task role of your service's construct or module starts with read access to runtime configuration only. Add the permissions your code needs:
|
|
381
|
+
|
|
382
|
+
<Infrastructure>
|
|
383
|
+
<Fragment slot="cdk">
|
|
384
|
+
|
|
385
|
+
```ts
|
|
386
|
+
new EcsService(this, 'MyService', {
|
|
387
|
+
// ...
|
|
388
|
+
grantTaskRole: (taskRole) => myTable.grantReadData(taskRole),
|
|
389
|
+
});
|
|
390
|
+
```
|
|
391
|
+
|
|
392
|
+
</Fragment>
|
|
393
|
+
<Fragment slot="terraform">
|
|
394
|
+
|
|
395
|
+
```hcl
|
|
396
|
+
task_role_policy_statements = [{
|
|
397
|
+
Effect = "Allow"
|
|
398
|
+
Action = ["dynamodb:GetItem", "dynamodb:Query"]
|
|
399
|
+
Resource = [aws_dynamodb_table.my_table.arn]
|
|
400
|
+
}]
|
|
401
|
+
```
|
|
402
|
+
|
|
403
|
+
</Fragment>
|
|
404
|
+
</Infrastructure>
|
|
405
|
+
|
|
406
|
+
#### Container User
|
|
407
|
+
|
|
408
|
+
The task definition runs the container as uid 1000, a non-root user. This matches the `node` user that the vended Dockerfile sets. To run as another user, set the CDK `user` prop or the Terraform `container_user` variable. Terraform rejects `0` and `root`.
|
|
409
|
+
|
|
410
|
+
| Setting | CDK prop | Terraform variable | Default |
|
|
411
|
+
| --- | --- | --- | --- |
|
|
412
|
+
| User the container runs as | `user` | `container_user` | `'1000'` (CDK), `"1000:1000"` (Terraform) |
|
|
413
|
+
|
|
414
|
+
In Terraform, `container_user` is a variable of the `ecs-service` module. Your service's module does not pass it, so set it in its `module "ecs_service"` block.
|
|
415
|
+
|
|
416
|
+
### Cost
|
|
417
|
+
|
|
418
|
+
A container service bills while it is idle, unlike a Lambda function. These parts bill by the hour whether or not anyone calls the service:
|
|
419
|
+
|
|
420
|
+
- Fargate tasks, per vCPU and per GB of memory. `desiredCount` and `minCapacity` keep at least one task running.
|
|
421
|
+
- Interface VPC endpoints, per hour in each Availability Zone, or a NAT gateway, per hour and per GB of data it processes.
|
|
422
|
+
- Log storage in CloudWatch Logs, and image storage in Amazon ECR.
|
|
423
|
+
|
|
424
|
+
To estimate the cost for your Region and traffic, use the [AWS Pricing Calculator](https://calculator.aws/).
|
|
425
|
+
|
|
426
|
+
### Destroying Your Service
|
|
427
|
+
|
|
428
|
+
To remove the service, destroy the stack or the Terraform configuration that holds it:
|
|
429
|
+
|
|
430
|
+
<Infrastructure>
|
|
431
|
+
<Fragment slot="cdk">
|
|
432
|
+
Run your infrastructure project's `destroy` target, which runs `cdk destroy`:
|
|
433
|
+
|
|
434
|
+
<NxCommands commands={['run infra:destroy']} />
|
|
435
|
+
</Fragment>
|
|
436
|
+
<Fragment slot="terraform">
|
|
437
|
+
Run your infrastructure project's `destroy` target:
|
|
438
|
+
|
|
439
|
+
<NxCommands commands={['run infra:destroy']} />
|
|
440
|
+
</Fragment>
|
|
441
|
+
</Infrastructure>
|
|
442
|
+
|
|
443
|
+
Then check in the AWS console or with the AWS CLI that the ECS service and cluster are gone. With both CDK and Terraform, destroy deletes the service's log groups and schedules its AWS KMS key for deletion. KMS waits 7 days before it deletes the key. Images stay in Amazon ECR, in the CDK assets repository or the shared `asset-ecr` repository. Delete them if you no longer need them. The VPC is yours, so destroying the service leaves it in place.
|
|
444
|
+
</OptionFilter>
|
|
445
|
+
|
|
446
|
+
## Connecting Your Service
|
|
447
|
+
|
|
448
|
+
Use the <Link path="/guides/connection">`connection`</Link> generator to connect your service to other projects in your workspace:
|
|
449
|
+
|
|
450
|
+
<RunGenerator generator="connection" />
|
|
451
|
+
|
|
452
|
+
Your service can use a relational database or a DynamoDB table. Each connection has its own guide:
|
|
453
|
+
|
|
454
|
+
- <Link path="guides/connection/ts-container-rdb">TypeScript Container Service to Relational Database</Link>
|
|
455
|
+
- <Link path="guides/connection/ts-container-dynamodb">TypeScript Container Service to DynamoDB</Link>
|
|
456
|
+
|
|
457
|
+
A connection needs a service with `infra` set to `ecs`.
|
package/generators.json
CHANGED
|
@@ -96,7 +96,11 @@
|
|
|
96
96
|
"connection/agentcore-gateway-mcp",
|
|
97
97
|
"connection/agentcore-gateway-gateway",
|
|
98
98
|
"connection/agentcore-gateway-agent",
|
|
99
|
-
"connection/react-agentcore-gateway"
|
|
99
|
+
"connection/react-agentcore-gateway",
|
|
100
|
+
"connection/ts-container-rdb",
|
|
101
|
+
"connection/py-container-rdb",
|
|
102
|
+
"connection/ts-container-dynamodb",
|
|
103
|
+
"connection/py-container-dynamodb"
|
|
100
104
|
]
|
|
101
105
|
},
|
|
102
106
|
"greengrass#component": {
|
|
@@ -576,6 +580,50 @@
|
|
|
576
580
|
"metric": "g59",
|
|
577
581
|
"hidden": true
|
|
578
582
|
},
|
|
583
|
+
"ts#container": {
|
|
584
|
+
"factory": "./src/ts/container/generator",
|
|
585
|
+
"schema": "./src/ts/container/schema.json",
|
|
586
|
+
"description": "Add a containerised service to a TypeScript project, run privately on Amazon ECS",
|
|
587
|
+
"metric": "g81",
|
|
588
|
+
"guidePages": ["ts-container"],
|
|
589
|
+
"experimental": true
|
|
590
|
+
},
|
|
591
|
+
"py#container": {
|
|
592
|
+
"factory": "./src/py/container/generator",
|
|
593
|
+
"schema": "./src/py/container/schema.json",
|
|
594
|
+
"description": "Add a containerised service to a Python project, run privately on Amazon ECS",
|
|
595
|
+
"metric": "g82",
|
|
596
|
+
"guidePages": ["py-container"],
|
|
597
|
+
"experimental": true
|
|
598
|
+
},
|
|
599
|
+
"ts#rdb#container-connection": {
|
|
600
|
+
"factory": "./src/ts/rdb/container-connection/generator",
|
|
601
|
+
"schema": "./src/ts/rdb/container-connection/schema.json",
|
|
602
|
+
"description": "Connect a ts#container service to a ts#rdb project",
|
|
603
|
+
"metric": "g86",
|
|
604
|
+
"hidden": true
|
|
605
|
+
},
|
|
606
|
+
"py#rdb#container-connection": {
|
|
607
|
+
"factory": "./src/py/rdb/container-connection/generator",
|
|
608
|
+
"schema": "./src/py/rdb/container-connection/schema.json",
|
|
609
|
+
"description": "Connect a py#container service to a py#rdb project",
|
|
610
|
+
"metric": "g87",
|
|
611
|
+
"hidden": true
|
|
612
|
+
},
|
|
613
|
+
"ts#dynamodb#container-connection": {
|
|
614
|
+
"factory": "./src/ts/dynamodb/container-connection/generator",
|
|
615
|
+
"schema": "./src/ts/dynamodb/container-connection/schema.json",
|
|
616
|
+
"description": "Connect a ts#container service to a ts#dynamodb project",
|
|
617
|
+
"metric": "g88",
|
|
618
|
+
"hidden": true
|
|
619
|
+
},
|
|
620
|
+
"py#dynamodb#container-connection": {
|
|
621
|
+
"factory": "./src/py/dynamodb/container-connection/generator",
|
|
622
|
+
"schema": "./src/py/dynamodb/container-connection/schema.json",
|
|
623
|
+
"description": "Connect a py#container service to a py#dynamodb project",
|
|
624
|
+
"metric": "g89",
|
|
625
|
+
"hidden": true
|
|
626
|
+
},
|
|
579
627
|
"internal#test-matrix": {
|
|
580
628
|
"factory": "./src/internal/test-matrix/generator",
|
|
581
629
|
"schema": "./src/internal/test-matrix/schema.json",
|
package/package.json
CHANGED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json-schema.org/schema",
|
|
3
|
+
"$id": "py#container",
|
|
4
|
+
"title": "py#container",
|
|
5
|
+
"x-label": "Container",
|
|
6
|
+
"description": "Add a containerised service to a Python project, run privately on Amazon ECS",
|
|
7
|
+
"type": "object",
|
|
8
|
+
"properties": {
|
|
9
|
+
"project": {
|
|
10
|
+
"type": "string",
|
|
11
|
+
"description": "The project to add the container service to",
|
|
12
|
+
"$default": {
|
|
13
|
+
"$source": "argv",
|
|
14
|
+
"index": 0
|
|
15
|
+
},
|
|
16
|
+
"x-prompt": "Select the Python project to add the container service to",
|
|
17
|
+
"x-dropdown": "projects"
|
|
18
|
+
},
|
|
19
|
+
"name": {
|
|
20
|
+
"type": "string",
|
|
21
|
+
"description": "The name of your container service (default: container)",
|
|
22
|
+
"x-prompt": "What would you like to call your container service? (default: container)"
|
|
23
|
+
},
|
|
24
|
+
"infra": {
|
|
25
|
+
"type": "string",
|
|
26
|
+
"description": "The infrastructure that runs your container. ecs runs it as an Amazon ECS service on AWS Fargate in your VPC, with no load balancer and no inbound access. none generates no infrastructure.",
|
|
27
|
+
"x-prompt": "How would you like to host your container service?",
|
|
28
|
+
"enum": ["ecs", "none"],
|
|
29
|
+
"default": "ecs",
|
|
30
|
+
"x-priority": "important"
|
|
31
|
+
},
|
|
32
|
+
"iac": {
|
|
33
|
+
"type": "string",
|
|
34
|
+
"description": "The preferred IaC provider. By default this is inherited from your initial selection.",
|
|
35
|
+
"enum": ["inherit", "cdk", "terraform"],
|
|
36
|
+
"x-priority": "important",
|
|
37
|
+
"default": "inherit",
|
|
38
|
+
"x-prompt": "Which provider would you like to manage your infrastructure? (default: inherit)"
|
|
39
|
+
},
|
|
40
|
+
"preferInstallDependencies": {
|
|
41
|
+
"type": "boolean",
|
|
42
|
+
"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.",
|
|
43
|
+
"default": true
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
"required": ["project"]
|
|
47
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json-schema.org/schema",
|
|
3
|
+
"$id": "py#dynamodb#container-connection",
|
|
4
|
+
"title": "py#dynamodb#container-connection",
|
|
5
|
+
"description": "Connect a py#container service to a py#dynamodb project",
|
|
6
|
+
"type": "object",
|
|
7
|
+
"properties": {
|
|
8
|
+
"sourceProject": {
|
|
9
|
+
"type": "string",
|
|
10
|
+
"description": "The project containing the py#container service to connect from"
|
|
11
|
+
},
|
|
12
|
+
"targetProject": {
|
|
13
|
+
"type": "string",
|
|
14
|
+
"description": "The py#dynamodb project to connect to"
|
|
15
|
+
},
|
|
16
|
+
"sourceComponent": {
|
|
17
|
+
"type": "string",
|
|
18
|
+
"description": "The container service component name"
|
|
19
|
+
},
|
|
20
|
+
"preferInstallDependencies": {
|
|
21
|
+
"type": "boolean",
|
|
22
|
+
"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.",
|
|
23
|
+
"default": true
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"required": ["sourceProject", "targetProject"]
|
|
27
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json-schema.org/schema",
|
|
3
|
+
"$id": "py#rdb#container-connection",
|
|
4
|
+
"title": "py#rdb#container-connection",
|
|
5
|
+
"description": "Connect a py#container service to a py#rdb project",
|
|
6
|
+
"type": "object",
|
|
7
|
+
"properties": {
|
|
8
|
+
"sourceProject": {
|
|
9
|
+
"type": "string",
|
|
10
|
+
"description": "The project containing the py#container service to connect from"
|
|
11
|
+
},
|
|
12
|
+
"targetProject": {
|
|
13
|
+
"type": "string",
|
|
14
|
+
"description": "The py#rdb project to connect to"
|
|
15
|
+
},
|
|
16
|
+
"sourceComponent": {
|
|
17
|
+
"type": "string",
|
|
18
|
+
"description": "The container service component name"
|
|
19
|
+
},
|
|
20
|
+
"preferInstallDependencies": {
|
|
21
|
+
"type": "boolean",
|
|
22
|
+
"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.",
|
|
23
|
+
"default": true
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"required": ["sourceProject", "targetProject"]
|
|
27
|
+
}
|