@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.
- 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
package/bin/aws-nx-mcp.js
CHANGED
|
@@ -22618,6 +22618,7 @@ const NX_VERSION = {
|
|
|
22618
22618
|
minimatch: "11.0.8",
|
|
22619
22619
|
"deepmerge-ts": "8.0.2",
|
|
22620
22620
|
mysql2: "3.24.6",
|
|
22621
|
+
effect: "3.22.5",
|
|
22621
22622
|
"@aws-sdk/client-dynamodb": "3.1169.0",
|
|
22622
22623
|
"@aws-sdk/client-api-gateway": "3.1169.0",
|
|
22623
22624
|
"@aws-sdk/client-iam": "3.1169.0",
|
|
@@ -56005,7 +56006,11 @@ const generatorsJsonEntries = {
|
|
|
56005
56006
|
"connection/agentcore-gateway-mcp",
|
|
56006
56007
|
"connection/agentcore-gateway-gateway",
|
|
56007
56008
|
"connection/agentcore-gateway-agent",
|
|
56008
|
-
"connection/react-agentcore-gateway"
|
|
56009
|
+
"connection/react-agentcore-gateway",
|
|
56010
|
+
"connection/ts-container-rdb",
|
|
56011
|
+
"connection/py-container-rdb",
|
|
56012
|
+
"connection/ts-container-dynamodb",
|
|
56013
|
+
"connection/py-container-dynamodb"
|
|
56009
56014
|
]
|
|
56010
56015
|
},
|
|
56011
56016
|
"greengrass#component": {
|
|
@@ -56489,6 +56494,50 @@ const generatorsJsonEntries = {
|
|
|
56489
56494
|
"metric": "g59",
|
|
56490
56495
|
"hidden": true
|
|
56491
56496
|
},
|
|
56497
|
+
"ts#container": {
|
|
56498
|
+
"factory": "./src/ts/container/generator",
|
|
56499
|
+
"schema": "./src/ts/container/schema.json",
|
|
56500
|
+
"description": "Add a containerised service to a TypeScript project, run privately on Amazon ECS",
|
|
56501
|
+
"metric": "g81",
|
|
56502
|
+
"guidePages": ["ts-container"],
|
|
56503
|
+
"experimental": true
|
|
56504
|
+
},
|
|
56505
|
+
"py#container": {
|
|
56506
|
+
"factory": "./src/py/container/generator",
|
|
56507
|
+
"schema": "./src/py/container/schema.json",
|
|
56508
|
+
"description": "Add a containerised service to a Python project, run privately on Amazon ECS",
|
|
56509
|
+
"metric": "g82",
|
|
56510
|
+
"guidePages": ["py-container"],
|
|
56511
|
+
"experimental": true
|
|
56512
|
+
},
|
|
56513
|
+
"ts#rdb#container-connection": {
|
|
56514
|
+
"factory": "./src/ts/rdb/container-connection/generator",
|
|
56515
|
+
"schema": "./src/ts/rdb/container-connection/schema.json",
|
|
56516
|
+
"description": "Connect a ts#container service to a ts#rdb project",
|
|
56517
|
+
"metric": "g86",
|
|
56518
|
+
"hidden": true
|
|
56519
|
+
},
|
|
56520
|
+
"py#rdb#container-connection": {
|
|
56521
|
+
"factory": "./src/py/rdb/container-connection/generator",
|
|
56522
|
+
"schema": "./src/py/rdb/container-connection/schema.json",
|
|
56523
|
+
"description": "Connect a py#container service to a py#rdb project",
|
|
56524
|
+
"metric": "g87",
|
|
56525
|
+
"hidden": true
|
|
56526
|
+
},
|
|
56527
|
+
"ts#dynamodb#container-connection": {
|
|
56528
|
+
"factory": "./src/ts/dynamodb/container-connection/generator",
|
|
56529
|
+
"schema": "./src/ts/dynamodb/container-connection/schema.json",
|
|
56530
|
+
"description": "Connect a ts#container service to a ts#dynamodb project",
|
|
56531
|
+
"metric": "g88",
|
|
56532
|
+
"hidden": true
|
|
56533
|
+
},
|
|
56534
|
+
"py#dynamodb#container-connection": {
|
|
56535
|
+
"factory": "./src/py/dynamodb/container-connection/generator",
|
|
56536
|
+
"schema": "./src/py/dynamodb/container-connection/schema.json",
|
|
56537
|
+
"description": "Connect a py#container service to a py#dynamodb project",
|
|
56538
|
+
"metric": "g89",
|
|
56539
|
+
"hidden": true
|
|
56540
|
+
},
|
|
56492
56541
|
"internal#test-matrix": {
|
|
56493
56542
|
"factory": "./src/internal/test-matrix/generator",
|
|
56494
56543
|
"schema": "./src/internal/test-matrix/schema.json",
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Python Container Service to DynamoDB
|
|
3
|
+
description: Connect a Python container service to a Python DynamoDB project
|
|
4
|
+
when:
|
|
5
|
+
sourceType: py#container
|
|
6
|
+
targetType: py#dynamodb
|
|
7
|
+
---
|
|
8
|
+
import Link from '@components/link.astro';
|
|
9
|
+
import RunGenerator from '@components/run-generator.astro';
|
|
10
|
+
import GeneratorParameters from '@components/generator-parameters.astro';
|
|
11
|
+
import NxCommands from '@components/nx-commands.astro';
|
|
12
|
+
import Infrastructure from '@components/infrastructure.astro';
|
|
13
|
+
|
|
14
|
+
The `connection` generator lets a <Link path="guides/py-container">Python container service</Link> use a <Link path="guides/py-dynamodb">Python DynamoDB</Link> project. It adds the DynamoDB package as a dependency of the service's project, configures local development so the service and DynamoDB Local start together, and prints the grants the service needs.
|
|
15
|
+
|
|
16
|
+
## How this works
|
|
17
|
+
|
|
18
|
+
A container service with `infra=ecs` runs as tasks on Amazon ECS (Elastic Container Service). Each task runs as the service's task role, an IAM role that can only read runtime configuration until you grant more. To read and write the table, the task role needs the DynamoDB actions on the table and its indexes, and use of the table's AWS KMS encryption key.
|
|
19
|
+
|
|
20
|
+
The generator rejects a service with `infra=none`, because such a service has no task role to grant access to.
|
|
21
|
+
|
|
22
|
+
## Prerequisites
|
|
23
|
+
|
|
24
|
+
Before using this generator, ensure you have:
|
|
25
|
+
|
|
26
|
+
1. A container service with `infra=ecs` (generated using the <Link path="guides/py-container">`py#container`</Link> generator)
|
|
27
|
+
2. A <Link path="guides/py-dynamodb">`py#dynamodb`</Link> project
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
### Run the Generator
|
|
32
|
+
|
|
33
|
+
<RunGenerator generator="connection" />
|
|
34
|
+
|
|
35
|
+
Select the project containing your container service as the source and your DynamoDB project as the target. If the source project holds several components, set `sourceComponent` to the service's name.
|
|
36
|
+
|
|
37
|
+
### Options
|
|
38
|
+
|
|
39
|
+
<GeneratorParameters generator="connection" />
|
|
40
|
+
|
|
41
|
+
## Generator Output
|
|
42
|
+
|
|
43
|
+
The generator adds the DynamoDB package to the service project's `pyproject.toml` as a workspace dependency, and updates the service's `<service-name>-serve` target in `project.json` to depend on the DynamoDB project's `dev` target.
|
|
44
|
+
|
|
45
|
+
## Using DynamoDB in the Service
|
|
46
|
+
|
|
47
|
+
Import entity classes from the DynamoDB package and use them where you handle a request:
|
|
48
|
+
|
|
49
|
+
```python title="packages/my_project/my_project/my_service/examples.py"
|
|
50
|
+
from my_scope_my_table.entities.example import ExampleModel
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def list_examples() -> list[dict]:
|
|
54
|
+
return [item.attribute_values for item in ExampleModel.scan()]
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Infrastructure
|
|
58
|
+
|
|
59
|
+
The generator does not change your stack or root Terraform module. Instead it prints the code to add, which is shown below for a service named `my-service` and a table named `my-table`. Until you add it, the service's requests to the table are denied.
|
|
60
|
+
|
|
61
|
+
<Infrastructure>
|
|
62
|
+
<Fragment slot="cdk">
|
|
63
|
+
|
|
64
|
+
In the example, `myService` is your `MyService` construct and `myTable` is your `MyTable` construct:
|
|
65
|
+
|
|
66
|
+
```ts title="packages/infra/src/stacks/application-stack.ts"
|
|
67
|
+
myTable.grantReadWriteData(myService.taskRole);
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
`grantReadWriteData` grants both the DynamoDB and the KMS permissions to the service's task role.
|
|
71
|
+
|
|
72
|
+
</Fragment>
|
|
73
|
+
<Fragment slot="terraform">
|
|
74
|
+
|
|
75
|
+
In the example, `module "my_service"` deploys the service and `module "my_table"` deploys the table:
|
|
76
|
+
|
|
77
|
+
```hcl title="packages/infra/src/main.tf"
|
|
78
|
+
resource "aws_iam_role_policy" "my_service_my_table_access" {
|
|
79
|
+
name = "my-service-my-table-access"
|
|
80
|
+
role = module.my_service.task_role_name
|
|
81
|
+
|
|
82
|
+
policy = jsonencode({
|
|
83
|
+
Version = "2012-10-17"
|
|
84
|
+
Statement = [
|
|
85
|
+
{
|
|
86
|
+
Effect = "Allow"
|
|
87
|
+
Action = [
|
|
88
|
+
"dynamodb:GetItem", "dynamodb:PutItem", "dynamodb:UpdateItem",
|
|
89
|
+
"dynamodb:DeleteItem", "dynamodb:Query", "dynamodb:Scan",
|
|
90
|
+
"dynamodb:BatchGetItem", "dynamodb:BatchWriteItem",
|
|
91
|
+
]
|
|
92
|
+
Resource = [
|
|
93
|
+
module.my_table.table_arn,
|
|
94
|
+
"${module.my_table.table_arn}/index/*",
|
|
95
|
+
]
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
Effect = "Allow"
|
|
99
|
+
Action = [
|
|
100
|
+
"kms:Encrypt",
|
|
101
|
+
"kms:Decrypt",
|
|
102
|
+
"kms:ReEncrypt*",
|
|
103
|
+
"kms:GenerateDataKey*",
|
|
104
|
+
"kms:DescribeKey",
|
|
105
|
+
]
|
|
106
|
+
Resource = [module.my_table.kms_key_arn]
|
|
107
|
+
},
|
|
108
|
+
]
|
|
109
|
+
})
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
</Fragment>
|
|
114
|
+
</Infrastructure>
|
|
115
|
+
|
|
116
|
+
## Local Development
|
|
117
|
+
|
|
118
|
+
<NxCommands commands={["<service-name>-serve <project-name>"]} />
|
|
119
|
+
|
|
120
|
+
This starts the service and DynamoDB Local. With `LOCAL_DEV=true` set, `is_local()` returns `True` and your PynamoDB entities connect to DynamoDB Local instead of AWS.
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Python Container Service to Relational Database
|
|
3
|
+
description: Connect a Python container service to a Python Relational Database
|
|
4
|
+
when:
|
|
5
|
+
sourceType: py#container
|
|
6
|
+
targetType: py#rdb
|
|
7
|
+
---
|
|
8
|
+
import { FileTree } from '@astrojs/starlight/components';
|
|
9
|
+
import Link from '@components/link.astro';
|
|
10
|
+
import RunGenerator from '@components/run-generator.astro';
|
|
11
|
+
import GeneratorParameters from '@components/generator-parameters.astro';
|
|
12
|
+
import NxCommands from '@components/nx-commands.astro';
|
|
13
|
+
import Infrastructure from '@components/infrastructure.astro';
|
|
14
|
+
|
|
15
|
+
The `connection` generator lets a <Link path="guides/py-container">Python container service</Link> use a <Link path="guides/py-rdb">Python Relational Database</Link> project. It adds the database package as a dependency of the service's project, and prepares the service's Docker image for SSL connections to Amazon Aurora.
|
|
16
|
+
|
|
17
|
+
## How this works
|
|
18
|
+
|
|
19
|
+
A container service with `infra=ecs` runs as tasks on Amazon ECS (Elastic Container Service) in your VPC. Each task runs as the service's task role, an IAM role that can only read runtime configuration until you grant more, and sits in the service's security group, which allows outbound traffic on port 443 only. To use the database, the service needs two things that it does not have by default:
|
|
20
|
+
|
|
21
|
+
- IAM permission for `rds-db:connect`, so it can sign in to the database with IAM authentication.
|
|
22
|
+
- A security group rule pair on the database port: an inbound rule on the database's security group, and an outbound rule on the service's security group. Without the outbound rule the service cannot reach the database, because its security group allows only port 443 out.
|
|
23
|
+
|
|
24
|
+
The generator rejects a service with `infra=none`, because such a service has no task role to grant access to.
|
|
25
|
+
|
|
26
|
+
## Prerequisites
|
|
27
|
+
|
|
28
|
+
Before using this generator, ensure you have:
|
|
29
|
+
|
|
30
|
+
1. A container service with `infra=ecs` (generated using the <Link path="guides/py-container">`py#container`</Link> generator)
|
|
31
|
+
2. A <Link path="guides/py-rdb">`py#rdb`</Link> project
|
|
32
|
+
|
|
33
|
+
## Usage
|
|
34
|
+
|
|
35
|
+
### Run the Generator
|
|
36
|
+
|
|
37
|
+
<RunGenerator generator="connection" />
|
|
38
|
+
|
|
39
|
+
Select the project containing your container service as the source and your relational database project as the target. If the source project holds several components, set `sourceComponent` to the service's name.
|
|
40
|
+
|
|
41
|
+
### Options
|
|
42
|
+
|
|
43
|
+
<GeneratorParameters generator="connection" />
|
|
44
|
+
|
|
45
|
+
## Generator Output
|
|
46
|
+
|
|
47
|
+
<FileTree>
|
|
48
|
+
|
|
49
|
+
- packages/my\_project
|
|
50
|
+
- project.json Adds a dependency from `my-service-serve` on the database's `dev` target
|
|
51
|
+
- pyproject.toml Adds the database package as a workspace dependency
|
|
52
|
+
- my\_project
|
|
53
|
+
- my\_service
|
|
54
|
+
- Dockerfile Installs the Amazon RDS CA bundle for SSL connections to Aurora
|
|
55
|
+
|
|
56
|
+
</FileTree>
|
|
57
|
+
|
|
58
|
+
## Using the Database in the Service
|
|
59
|
+
|
|
60
|
+
Import `session_context` from your database package and use it where you handle a request:
|
|
61
|
+
|
|
62
|
+
```python title="packages/my_project/my_project/my_service/orders.py"
|
|
63
|
+
from sqlmodel import select
|
|
64
|
+
from my_scope_my_database import session_context
|
|
65
|
+
from my_scope_my_database.models.example import ExampleModel
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
async def list_examples() -> list[dict]:
|
|
69
|
+
async with session_context() as session:
|
|
70
|
+
items = (await session.execute(select(ExampleModel))).scalars().all()
|
|
71
|
+
return [item.model_dump() for item in items]
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
The database package reads the database's connection details from <Link path="guides/runtime-config">runtime configuration</Link>, using the `RUNTIME_CONFIG_APP_ID` environment variable.
|
|
75
|
+
|
|
76
|
+
The generated container service sets `RUNTIME_CONFIG_APP_ID` and grants its task role read access to runtime configuration, so you do not add either yourself. In Terraform, the service's module block must pass `appconfig_application_id` and `appconfig_application_arn`, as shown in the <Link path="guides/py-container">container guide</Link>.
|
|
77
|
+
|
|
78
|
+
## Infrastructure
|
|
79
|
+
|
|
80
|
+
The generator does not change your stack or root Terraform module. Instead it prints the code to add, which is shown below for a service named `my-service` and a database named `my-database`. Until you add it, the service cannot reach the database port and the database refuses its IAM authentication.
|
|
81
|
+
|
|
82
|
+
<Infrastructure>
|
|
83
|
+
<Fragment slot="cdk">
|
|
84
|
+
|
|
85
|
+
In the example, `myService` is your `MyService` construct and `myDatabase` is your `MyDatabase` construct:
|
|
86
|
+
|
|
87
|
+
```ts title="packages/infra/src/stacks/application-stack.ts"
|
|
88
|
+
myDatabase.allowDefaultPortFrom(myService);
|
|
89
|
+
myDatabase.grantConnect(myService.taskRole);
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
`allowDefaultPortFrom` adds the inbound rule on the database's security group and the outbound rule on the service's security group. `grantConnect` grants `rds-db:connect` to the service's task role.
|
|
93
|
+
|
|
94
|
+
</Fragment>
|
|
95
|
+
<Fragment slot="terraform">
|
|
96
|
+
|
|
97
|
+
In the example, `module "my_service"` deploys the service and `module "my_database"` deploys the database. The `data "aws_region" "current"` and `data "aws_caller_identity" "current"` data sources are declared in the generated `providers.tf`:
|
|
98
|
+
|
|
99
|
+
```hcl title="packages/infra/src/main.tf"
|
|
100
|
+
resource "aws_iam_role_policy" "my_service_my_database_connect" {
|
|
101
|
+
name = "my-service-my-database-connect"
|
|
102
|
+
role = module.my_service.task_role_name
|
|
103
|
+
|
|
104
|
+
policy = jsonencode({
|
|
105
|
+
Version = "2012-10-17"
|
|
106
|
+
Statement = [
|
|
107
|
+
{
|
|
108
|
+
Effect = "Allow"
|
|
109
|
+
Action = ["rds-db:connect"]
|
|
110
|
+
Resource = ["arn:aws:rds-db:${data.aws_region.current.region}:${data.aws_caller_identity.current.account_id}:dbuser:${module.my_database.connect_resource_id}/${module.my_database.database_runtime_user}"]
|
|
111
|
+
},
|
|
112
|
+
]
|
|
113
|
+
})
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
resource "aws_vpc_security_group_ingress_rule" "my_service_to_my_database" {
|
|
117
|
+
description = "Allow the my-service container service to connect to the database"
|
|
118
|
+
security_group_id = module.my_database.security_group_id
|
|
119
|
+
referenced_security_group_id = module.my_service.security_group_id
|
|
120
|
+
from_port = module.my_database.cluster_port
|
|
121
|
+
to_port = module.my_database.cluster_port
|
|
122
|
+
ip_protocol = "tcp"
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
resource "aws_vpc_security_group_egress_rule" "my_service_to_my_database" {
|
|
126
|
+
description = "Allow the my-service container service to reach the database"
|
|
127
|
+
security_group_id = module.my_service.security_group_id
|
|
128
|
+
referenced_security_group_id = module.my_database.security_group_id
|
|
129
|
+
from_port = module.my_database.cluster_port
|
|
130
|
+
to_port = module.my_database.cluster_port
|
|
131
|
+
ip_protocol = "tcp"
|
|
132
|
+
}
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
</Fragment>
|
|
136
|
+
</Infrastructure>
|
|
137
|
+
|
|
138
|
+
### SSL Requirements When Connecting Without RDS Proxy
|
|
139
|
+
|
|
140
|
+
When the service connects to the Aurora cluster directly, without RDS Proxy, it must trust the Amazon RDS certificate authority. The generator adds these lines to the service's Dockerfile, before its `EXPOSE` line, to install the Amazon RDS CA bundle into the system trust store:
|
|
141
|
+
|
|
142
|
+
```dockerfile
|
|
143
|
+
ADD https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem /usr/local/share/ca-certificates/rds-global-bundle.crt
|
|
144
|
+
RUN update-ca-certificates
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
When using RDS Proxy, you do not need the RDS CA bundle.
|
|
148
|
+
|
|
149
|
+
## Local Development
|
|
150
|
+
|
|
151
|
+
<NxCommands commands={["<service-name>-serve <project-name>"]} />
|
|
152
|
+
|
|
153
|
+
This starts the service and the database's local Docker database. With `LOCAL_DEV=true` set, the database client connects to the local database instead of Aurora.
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: TypeScript Container Service to DynamoDB
|
|
3
|
+
description: Connect a TypeScript container service to a TypeScript DynamoDB project
|
|
4
|
+
when:
|
|
5
|
+
sourceType: ts#container
|
|
6
|
+
targetType: ts#dynamodb
|
|
7
|
+
---
|
|
8
|
+
import Link from '@components/link.astro';
|
|
9
|
+
import RunGenerator from '@components/run-generator.astro';
|
|
10
|
+
import GeneratorParameters from '@components/generator-parameters.astro';
|
|
11
|
+
import NxCommands from '@components/nx-commands.astro';
|
|
12
|
+
import Infrastructure from '@components/infrastructure.astro';
|
|
13
|
+
|
|
14
|
+
The `connection` generator lets a <Link path="guides/ts-container">TypeScript container service</Link> use a <Link path="guides/ts-dynamodb">TypeScript DynamoDB</Link> project. It configures local development so the service and DynamoDB Local start together, and prints the grants the service needs.
|
|
15
|
+
|
|
16
|
+
## How this works
|
|
17
|
+
|
|
18
|
+
A container service with `infra=ecs` runs as tasks on Amazon ECS (Elastic Container Service). Each task runs as the service's task role, an IAM role that can only read runtime configuration until you grant more. To read and write the table, the task role needs the DynamoDB actions on the table and its indexes, and use of the table's AWS KMS encryption key.
|
|
19
|
+
|
|
20
|
+
The generator rejects a service with `infra=none`, because such a service has no task role to grant access to.
|
|
21
|
+
|
|
22
|
+
## Prerequisites
|
|
23
|
+
|
|
24
|
+
Before using this generator, ensure you have:
|
|
25
|
+
|
|
26
|
+
1. A container service with `infra=ecs` (generated using the <Link path="guides/ts-container">`ts#container`</Link> generator)
|
|
27
|
+
2. A <Link path="guides/ts-dynamodb">`ts#dynamodb`</Link> project
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
### Run the Generator
|
|
32
|
+
|
|
33
|
+
<RunGenerator generator="connection" />
|
|
34
|
+
|
|
35
|
+
Select the project containing your container service as the source and your DynamoDB project as the target. If the source project holds several components, set `sourceComponent` to the service's name.
|
|
36
|
+
|
|
37
|
+
### Options
|
|
38
|
+
|
|
39
|
+
<GeneratorParameters generator="connection" />
|
|
40
|
+
|
|
41
|
+
## Generator Output
|
|
42
|
+
|
|
43
|
+
The generator updates the service's `<service-name>-serve` target in `project.json` to depend on the DynamoDB project's `dev` target. No source files are modified.
|
|
44
|
+
|
|
45
|
+
## Using DynamoDB in the Service
|
|
46
|
+
|
|
47
|
+
Import entity factories from the DynamoDB package and use them where you handle a request:
|
|
48
|
+
|
|
49
|
+
```ts title="packages/my-project/src/my-service/examples.ts"
|
|
50
|
+
import { createExampleEntity } from '@my-scope/my-table';
|
|
51
|
+
|
|
52
|
+
export const listExamples = async () => {
|
|
53
|
+
const entity = await createExampleEntity();
|
|
54
|
+
const result = await entity.scan.go();
|
|
55
|
+
return result.data;
|
|
56
|
+
};
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Infrastructure
|
|
60
|
+
|
|
61
|
+
The generator does not change your stack or root Terraform module. Instead it prints the code to add, which is shown below for a service named `my-service` and a table named `my-table`. Until you add it, the service's requests to the table are denied.
|
|
62
|
+
|
|
63
|
+
<Infrastructure>
|
|
64
|
+
<Fragment slot="cdk">
|
|
65
|
+
|
|
66
|
+
In the example, `myService` is your `MyService` construct and `myTable` is your `MyTable` construct:
|
|
67
|
+
|
|
68
|
+
```ts title="packages/infra/src/stacks/application-stack.ts"
|
|
69
|
+
myTable.grantReadWriteData(myService.taskRole);
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
`grantReadWriteData` grants both the DynamoDB and the KMS permissions to the service's task role.
|
|
73
|
+
|
|
74
|
+
</Fragment>
|
|
75
|
+
<Fragment slot="terraform">
|
|
76
|
+
|
|
77
|
+
In the example, `module "my_service"` deploys the service and `module "my_table"` deploys the table:
|
|
78
|
+
|
|
79
|
+
```hcl title="packages/infra/src/main.tf"
|
|
80
|
+
resource "aws_iam_role_policy" "my_service_my_table_access" {
|
|
81
|
+
name = "my-service-my-table-access"
|
|
82
|
+
role = module.my_service.task_role_name
|
|
83
|
+
|
|
84
|
+
policy = jsonencode({
|
|
85
|
+
Version = "2012-10-17"
|
|
86
|
+
Statement = [
|
|
87
|
+
{
|
|
88
|
+
Effect = "Allow"
|
|
89
|
+
Action = [
|
|
90
|
+
"dynamodb:GetItem", "dynamodb:PutItem", "dynamodb:UpdateItem",
|
|
91
|
+
"dynamodb:DeleteItem", "dynamodb:Query", "dynamodb:Scan",
|
|
92
|
+
"dynamodb:BatchGetItem", "dynamodb:BatchWriteItem",
|
|
93
|
+
]
|
|
94
|
+
Resource = [
|
|
95
|
+
module.my_table.table_arn,
|
|
96
|
+
"${module.my_table.table_arn}/index/*",
|
|
97
|
+
]
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
Effect = "Allow"
|
|
101
|
+
Action = [
|
|
102
|
+
"kms:Encrypt",
|
|
103
|
+
"kms:Decrypt",
|
|
104
|
+
"kms:ReEncrypt*",
|
|
105
|
+
"kms:GenerateDataKey*",
|
|
106
|
+
"kms:DescribeKey",
|
|
107
|
+
]
|
|
108
|
+
Resource = [module.my_table.kms_key_arn]
|
|
109
|
+
},
|
|
110
|
+
]
|
|
111
|
+
})
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
</Fragment>
|
|
116
|
+
</Infrastructure>
|
|
117
|
+
|
|
118
|
+
## Local Development
|
|
119
|
+
|
|
120
|
+
<NxCommands commands={["<service-name>-serve <project-name>"]} />
|
|
121
|
+
|
|
122
|
+
This starts the service and DynamoDB Local. With `LOCAL_DEV=true` set, `getDynamoDBClient()` and `resolveTableName()` connect to DynamoDB Local instead of AWS.
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: TypeScript Container Service to Relational Database
|
|
3
|
+
description: Connect a TypeScript container service to a Relational Database
|
|
4
|
+
when:
|
|
5
|
+
sourceType: ts#container
|
|
6
|
+
targetType: ts#rdb
|
|
7
|
+
---
|
|
8
|
+
import { FileTree } from '@astrojs/starlight/components';
|
|
9
|
+
import Link from '@components/link.astro';
|
|
10
|
+
import RunGenerator from '@components/run-generator.astro';
|
|
11
|
+
import GeneratorParameters from '@components/generator-parameters.astro';
|
|
12
|
+
import NxCommands from '@components/nx-commands.astro';
|
|
13
|
+
import Infrastructure from '@components/infrastructure.astro';
|
|
14
|
+
|
|
15
|
+
The `connection` generator lets a <Link path="guides/ts-container">TypeScript container service</Link> use a <Link path="guides/ts-rdb">Relational Database</Link> project. It adds a module that returns the database's Prisma client, and prepares the service's Docker image for SSL connections to Amazon Aurora.
|
|
16
|
+
|
|
17
|
+
## How this works
|
|
18
|
+
|
|
19
|
+
A container service with `infra=ecs` runs as tasks on Amazon ECS (Elastic Container Service) in your VPC. Each task runs as the service's task role, an IAM role that can only read runtime configuration until you grant more, and sits in the service's security group, which allows outbound traffic on port 443 only. To use the database, the service needs two things that it does not have by default:
|
|
20
|
+
|
|
21
|
+
- IAM permission for `rds-db:connect`, so it can sign in to the database with IAM authentication.
|
|
22
|
+
- A security group rule pair on the database port: an inbound rule on the database's security group, and an outbound rule on the service's security group. Without the outbound rule the service cannot reach the database, because its security group allows only port 443 out.
|
|
23
|
+
|
|
24
|
+
The generator rejects a service with `infra=none`, because such a service has no task role to grant access to.
|
|
25
|
+
|
|
26
|
+
## Prerequisites
|
|
27
|
+
|
|
28
|
+
Before using this generator, ensure you have:
|
|
29
|
+
|
|
30
|
+
1. A container service with `infra=ecs` (generated using the <Link path="guides/ts-container">`ts#container`</Link> generator)
|
|
31
|
+
2. A <Link path="guides/ts-rdb">`ts#rdb`</Link> project
|
|
32
|
+
|
|
33
|
+
## Usage
|
|
34
|
+
|
|
35
|
+
### Run the Generator
|
|
36
|
+
|
|
37
|
+
<RunGenerator generator="connection" />
|
|
38
|
+
|
|
39
|
+
Select the project containing your container service as the source and your relational database project as the target. If the source project holds several components, set `sourceComponent` to the service's name.
|
|
40
|
+
|
|
41
|
+
### Options
|
|
42
|
+
|
|
43
|
+
<GeneratorParameters generator="connection" />
|
|
44
|
+
|
|
45
|
+
## Generator Output
|
|
46
|
+
|
|
47
|
+
The generator changes the following files in your service's source directory:
|
|
48
|
+
|
|
49
|
+
<FileTree>
|
|
50
|
+
|
|
51
|
+
- packages/my-project/src/my-service
|
|
52
|
+
- my-database.ts Exports `getMyDatabase`, which returns the database's Prisma client
|
|
53
|
+
- Dockerfile Installs the Amazon RDS CA bundle for SSL connections to Aurora
|
|
54
|
+
|
|
55
|
+
</FileTree>
|
|
56
|
+
|
|
57
|
+
Additionally, the service's `<service-name>-serve` target is updated to depend on the database's `dev` target.
|
|
58
|
+
|
|
59
|
+
## Using the Database in the Service
|
|
60
|
+
|
|
61
|
+
Import the getter in your service's code and call it where you handle a request:
|
|
62
|
+
|
|
63
|
+
```ts title="packages/my-project/src/my-service/orders.ts"
|
|
64
|
+
import { getMyDatabase } from './my-database.js';
|
|
65
|
+
|
|
66
|
+
export const listOrders = async () => {
|
|
67
|
+
const myDatabase = await getMyDatabase();
|
|
68
|
+
return myDatabase.order.findMany();
|
|
69
|
+
};
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
The database client reads the database's connection details from <Link path="guides/runtime-config">runtime configuration</Link>, using the `RUNTIME_CONFIG_APP_ID` environment variable.
|
|
73
|
+
|
|
74
|
+
The generated container service sets `RUNTIME_CONFIG_APP_ID` and grants its task role read access to runtime configuration, so you do not add either yourself. In Terraform, the service's module block must pass `appconfig_application_id` and `appconfig_application_arn`, as shown in the <Link path="guides/ts-container">container guide</Link>.
|
|
75
|
+
|
|
76
|
+
## Infrastructure
|
|
77
|
+
|
|
78
|
+
The generator does not change your stack or root Terraform module. Instead it prints the code to add, which is shown below for a service named `my-service` and a database named `my-database`. Until you add it, the service cannot reach the database port and the database refuses its IAM authentication.
|
|
79
|
+
|
|
80
|
+
<Infrastructure>
|
|
81
|
+
<Fragment slot="cdk">
|
|
82
|
+
|
|
83
|
+
In the example, `myService` is your `MyService` construct and `myDatabase` is your `MyDatabase` construct:
|
|
84
|
+
|
|
85
|
+
```ts title="packages/infra/src/stacks/application-stack.ts"
|
|
86
|
+
myDatabase.allowDefaultPortFrom(myService);
|
|
87
|
+
myDatabase.grantConnect(myService.taskRole);
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
`allowDefaultPortFrom` adds the inbound rule on the database's security group and the outbound rule on the service's security group. `grantConnect` grants `rds-db:connect` to the service's task role.
|
|
91
|
+
|
|
92
|
+
</Fragment>
|
|
93
|
+
<Fragment slot="terraform">
|
|
94
|
+
|
|
95
|
+
In the example, `module "my_service"` deploys the service and `module "my_database"` deploys the database. The `data "aws_region" "current"` and `data "aws_caller_identity" "current"` data sources are declared in the generated `providers.tf`:
|
|
96
|
+
|
|
97
|
+
```hcl title="packages/infra/src/main.tf"
|
|
98
|
+
resource "aws_iam_role_policy" "my_service_my_database_connect" {
|
|
99
|
+
name = "my-service-my-database-connect"
|
|
100
|
+
role = module.my_service.task_role_name
|
|
101
|
+
|
|
102
|
+
policy = jsonencode({
|
|
103
|
+
Version = "2012-10-17"
|
|
104
|
+
Statement = [
|
|
105
|
+
{
|
|
106
|
+
Effect = "Allow"
|
|
107
|
+
Action = ["rds-db:connect"]
|
|
108
|
+
Resource = ["arn:aws:rds-db:${data.aws_region.current.region}:${data.aws_caller_identity.current.account_id}:dbuser:${module.my_database.connect_resource_id}/${module.my_database.database_runtime_user}"]
|
|
109
|
+
},
|
|
110
|
+
]
|
|
111
|
+
})
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
resource "aws_vpc_security_group_ingress_rule" "my_service_to_my_database" {
|
|
115
|
+
description = "Allow the my-service container service to connect to the database"
|
|
116
|
+
security_group_id = module.my_database.security_group_id
|
|
117
|
+
referenced_security_group_id = module.my_service.security_group_id
|
|
118
|
+
from_port = module.my_database.cluster_port
|
|
119
|
+
to_port = module.my_database.cluster_port
|
|
120
|
+
ip_protocol = "tcp"
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
resource "aws_vpc_security_group_egress_rule" "my_service_to_my_database" {
|
|
124
|
+
description = "Allow the my-service container service to reach the database"
|
|
125
|
+
security_group_id = module.my_service.security_group_id
|
|
126
|
+
referenced_security_group_id = module.my_database.security_group_id
|
|
127
|
+
from_port = module.my_database.cluster_port
|
|
128
|
+
to_port = module.my_database.cluster_port
|
|
129
|
+
ip_protocol = "tcp"
|
|
130
|
+
}
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
</Fragment>
|
|
134
|
+
</Infrastructure>
|
|
135
|
+
|
|
136
|
+
### SSL Requirements When Connecting Without RDS Proxy
|
|
137
|
+
|
|
138
|
+
When the service connects to the Aurora cluster directly, without RDS Proxy, Node.js must trust the Amazon RDS certificate authority. The generator adds this step to the service's Dockerfile, before its `EXPOSE` line:
|
|
139
|
+
|
|
140
|
+
```dockerfile
|
|
141
|
+
RUN apt-get update && apt-get install -y --no-install-recommends curl ca-certificates && \
|
|
142
|
+
rm -rf /var/lib/apt/lists/* && \
|
|
143
|
+
curl -fsSL "https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem" \
|
|
144
|
+
-o /usr/local/share/ca-certificates/rds-bundle.crt && \
|
|
145
|
+
update-ca-certificates
|
|
146
|
+
ENV NODE_EXTRA_CA_CERTS=/usr/local/share/ca-certificates/rds-bundle.crt
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
`update-ca-certificates` writes the system certificate store, which Node.js does not read, so the generator also sets `NODE_EXTRA_CA_CERTS` in the Dockerfile. You do not need to configure it yourself, and with RDS Proxy it is unused. For more details, see the [Amazon RDS SSL/TLS documentation](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.SSL.html).
|
|
150
|
+
|
|
151
|
+
## Local Development
|
|
152
|
+
|
|
153
|
+
<NxCommands commands={["<service-name>-serve <project-name>"]} />
|
|
154
|
+
|
|
155
|
+
This starts the service and the database's local Docker database. With `LOCAL_DEV=true` set, the Prisma client connects to the local database instead of Aurora.
|
|
@@ -34,6 +34,8 @@ export const trivyTarget = `{
|
|
|
34
34
|
|
|
35
35
|
Several generators (such as <Link path="/guides/ts-agent">`ts#agent`</Link> and <Link path="/guides/py-agent">`py#agent`</Link>) produce a Docker image that is pushed to Amazon ECR and consumed by AWS infrastructure. This guide describes the pattern they follow so that you can apply it to other use cases — for example, running a <Link path="/guides/fastapi">FastAPI</Link> project on Amazon ECS, or deploying a containerised Express server.
|
|
36
36
|
|
|
37
|
+
To run a containerised service privately on Amazon ECS without wiring this pattern yourself, use <Link path="/guides/ts-container">`ts#container`</Link> or <Link path="/guides/py-container">`py#container`</Link>.
|
|
38
|
+
|
|
37
39
|
:::tip[Docker or Finch]
|
|
38
40
|
The container engine used to build images is chosen at workspace creation time via the `--containers` flag (`docker`, `finch`, or `infer` — the default — which auto-detects what's installed). [Finch](https://runfinch.com/) is an open-source, drop-in alternative to Docker. The selection is recorded in `aws-nx-plugin.config.mts` and applied to every generator that emits container build commands. CDK image asset builds honour the choice via the `CDK_DOCKER` environment variable.
|
|
39
41
|
:::
|
|
@@ -397,6 +399,8 @@ CDK does not run the `bundle`/`docker` targets automatically — you must run `n
|
|
|
397
399
|
:::
|
|
398
400
|
|
|
399
401
|
Use the `DockerImageAsset` with any AWS construct that accepts a container image, for example `aws_ecs.ContainerImage.fromDockerImageAsset(image)`.
|
|
402
|
+
|
|
403
|
+
<Link path="/guides/ts-container">`ts#container`</Link> and <Link path="/guides/py-container">`py#container`</Link> use this pattern to run their image as an Amazon ECS service.
|
|
400
404
|
</Fragment>
|
|
401
405
|
<Fragment slot="terraform">
|
|
402
406
|
Terraform's AWS provider does not have a first-class "build and push a Docker image" resource. The pattern used by the generators is:
|
|
@@ -406,6 +410,8 @@ Terraform's AWS provider does not have a first-class "build and push a Docker im
|
|
|
406
410
|
3. A `null_resource` with a `local-exec` provisioner that authenticates to ECR, re-tags the locally-built image with its content digest, and pushes it.
|
|
407
411
|
4. The downstream resource (e.g. `aws_ecs_task_definition`) references the image by its immutable, digest-based tag.
|
|
408
412
|
|
|
413
|
+
<Link path="/guides/ts-container">`ts#container`</Link> and <Link path="/guides/py-container">`py#container`</Link> use this pattern in their Terraform module to run their image as an Amazon ECS service.
|
|
414
|
+
|
|
409
415
|
Because one repository holds every image, each tag is namespaced by the image's owner as well as its digest, so images sharing the registry never collide.
|
|
410
416
|
|
|
411
417
|
```d2
|