@aws/nx-plugin-mcp 1.6.0 → 1.7.1

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.
@@ -0,0 +1,457 @@
1
+ ---
2
+ title: Python Container Service
3
+ description: Add a containerised service to a Python project and run it privately on Amazon ECS
4
+ generator: py#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 Python project. You get a small [FastAPI](https://fastapi.tiangolo.com/) server, run by the [uvicorn](https://www.uvicorn.org/) web server, with a `GET /health` route and a sample `GET /echo` 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 Python project in two ways:
34
+
35
+ <RunGenerator generator="py#container" />
36
+
37
+ :::tip[Project Prerequisite]
38
+ First use the <Link path="/guides/python-project">`py#project`</Link> generator to create a project to add your service to.
39
+ :::
40
+
41
+ ### Options
42
+
43
+ <GeneratorParameters generator="py#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 in snake_case (for example `my_container` for `--name=my-container`), or `container` if you leave it out:
50
+
51
+ <FileTree>
52
+ - your-project/
53
+ - your_module/
54
+ - container/ (or the name you chose)
55
+ - \_\_init\_\_.py Python package initialization
56
+ - main.py FastAPI app with `GET /health` and the sample route `GET /echo`, served by uvicorn on `PORT`
57
+ - Dockerfile Container image definition
58
+ - .dockerignore Files left out of the image build context
59
+ - pyproject.toml Updated with FastAPI and uvicorn dependencies
60
+ - project.json Updated with the `bundle-arm` and `bundle` targets 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-arm` and `bundle` targets are shared by everything the project bundles.
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 /echo?message=<x>` returns `{"message":"<x>"}`. The `message` parameter is required and may be at most 1024 characters long. A request without it, or with a longer value, gets status 422.
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-arm` target first, which writes your code and its linux/arm64 dependencies to `dist/<project root>/bundle-arm`. The aggregate `bundle` target depends on `bundle-arm`. The `<name>-docker` target then copies that folder, the `Dockerfile` and the `.dockerignore` into `dist/<project root>/docker/<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. The vended Dockerfile matches it: it creates a user `app` with uid and gid 1000 and sets `USER 1000:1000`. 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/py-container-rdb">Python Container Service to Relational Database</Link>
455
+ - <Link path="guides/connection/py-container-dynamodb">Python Container Service to DynamoDB</Link>
456
+
457
+ A connection needs a service with `infra` set to `ecs`.