@aws/nx-plugin-mcp 1.5.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 +254 -65
- package/docs/get_started/tutorials/dungeon-game/4.mdx +5 -1
- package/docs/get_started/tutorials/dungeon-game/greengrass-dice.mdx +290 -0
- package/docs/get_started/tutorials/dungeon-game/wrap-up.mdx +66 -3
- package/docs/get_started/tutorials/greengrass/1.mdx +116 -0
- package/docs/get_started/tutorials/greengrass/2.mdx +198 -0
- package/docs/get_started/tutorials/greengrass/3.mdx +131 -0
- package/docs/get_started/tutorials/greengrass/cleanup.mdx +137 -0
- package/docs/get_started/tutorials/greengrass/overview.mdx +65 -0
- 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-agent-rdb.mdx +1 -1
- package/docs/guides/connection/ts-container-dynamodb.mdx +122 -0
- package/docs/guides/connection/ts-container-rdb.mdx +155 -0
- package/docs/guides/connection/ts-mcp-server-rdb.mdx +1 -1
- package/docs/guides/docker-bundling.mdx +6 -0
- package/docs/guides/greengrass-component.mdx +7 -1
- package/docs/guides/observability-langfuse.mdx +146 -0
- package/docs/guides/py-container.mdx +457 -0
- package/docs/guides/ts-container.mdx +457 -0
- package/docs/guides/ts-rdb.mdx +7 -1
- package/docs/snippets/connection/strands-agent-rdb-ssl-requirements.mdx +3 -28
- package/docs/snippets/connection/ts-mcp-server-rdb-ssl-requirements.mdx +3 -28
- package/generators.json +57 -1
- package/package.json +1 -1
- package/src/observability/langfuse/schema.json +38 -0
- 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,198 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Provision the core device
|
|
3
|
+
description: Create an owner-managed instance profile, select the Ubuntu 26.04 x86_64 image, and run the setup script that provisions an EC2 core device with the classic Greengrass nucleus
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import { Aside, Code } from '@astrojs/starlight/components';
|
|
7
|
+
import Link from '@components/link.astro';
|
|
8
|
+
|
|
9
|
+
export const setupScriptUrl = `${import.meta.env.SITE}${import.meta.env.BASE_URL.replace(/\/$/, '')}/greengrass/setup-tutorial.sh`;
|
|
10
|
+
|
|
11
|
+
The setup script creates the AWS IoT identity for one core device, launches an Ubuntu 26.04 x86_64 EC2 instance, and installs the classic Greengrass nucleus on it through AWS Systems Manager. It records what it created in `.greengrass-tutorial.env`, bound to your account, Region and prefix, so its `cleanup` operation deletes exactly those resources later.
|
|
12
|
+
|
|
13
|
+
| Resource | Name | Created by |
|
|
14
|
+
| --- | --- | --- |
|
|
15
|
+
| IoT thing group | `<prefix>-group` | Script |
|
|
16
|
+
| IoT thing | `<prefix>-core` | Script |
|
|
17
|
+
| Token exchange role and its access policy | `<prefix>-token-exchange`, `<prefix>-token-exchangeAccess` | Script |
|
|
18
|
+
| IoT role alias | `<prefix>-alias` | Script |
|
|
19
|
+
| Device IoT policy | `<prefix>-device-policy` | Script |
|
|
20
|
+
| EC2 instance | Tagged `Name=<prefix>-core` | Script |
|
|
21
|
+
| Device certificate and private key | Generated on the instance | Nucleus installer |
|
|
22
|
+
| IoT policy for the role alias | `GreengrassTESCertificatePolicy<prefix>-alias` | Nucleus installer |
|
|
23
|
+
|
|
24
|
+
The installer runs on the instance with the instance profile's temporary credentials. The device private key is created on the instance and stays under `/greengrass/v2`. The remote command that the script sends through Systems Manager contains resource names only, so no key or credential enters the Systems Manager command history.
|
|
25
|
+
|
|
26
|
+
## Task 1: Create the instance profile
|
|
27
|
+
|
|
28
|
+
The script does not create the instance profile. You own it, so you control what the installer is allowed to do. Its role needs `AmazonSSMManagedInstanceCore`, so Systems Manager can run the install command, plus a temporary policy for the installer's `--provision true` path.
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
export TUTORIAL_ACCOUNT_ID=$(aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" \
|
|
32
|
+
sts get-caller-identity --query Account --output text)
|
|
33
|
+
|
|
34
|
+
cat > /tmp/greengrass-tutorial-ec2-trust.json <<'EOF'
|
|
35
|
+
{
|
|
36
|
+
"Version": "2012-10-17",
|
|
37
|
+
"Statement": [
|
|
38
|
+
{ "Effect": "Allow", "Principal": { "Service": "ec2.amazonaws.com" }, "Action": "sts:AssumeRole" }
|
|
39
|
+
]
|
|
40
|
+
}
|
|
41
|
+
EOF
|
|
42
|
+
|
|
43
|
+
aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" iam create-role \
|
|
44
|
+
--role-name "${TUTORIAL_PREFIX}-instance" \
|
|
45
|
+
--assume-role-policy-document file:///tmp/greengrass-tutorial-ec2-trust.json
|
|
46
|
+
aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" iam attach-role-policy \
|
|
47
|
+
--role-name "${TUTORIAL_PREFIX}-instance" \
|
|
48
|
+
--policy-arn arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
The temporary installer policy follows the [minimal IAM policy for the installer](https://docs.aws.amazon.com/greengrass/v2/developerguide/provision-minimal-iam-policy.html). The `CreateIoTResources` statement is the official one. The IAM statement is narrower than the official `CreateTokenExchangeRole` statement: the script creates the token exchange role and its `<role>Access` policy before the installer runs, so the installer only reads them and never needs `iam:CreateRole`, `iam:CreatePolicy` or `iam:AttachRolePolicy`.
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
cat > /tmp/greengrass-tutorial-installer.json <<EOF
|
|
55
|
+
{
|
|
56
|
+
"Version": "2012-10-17",
|
|
57
|
+
"Statement": [
|
|
58
|
+
{
|
|
59
|
+
"Sid": "ReuseTokenExchangeRole",
|
|
60
|
+
"Effect": "Allow",
|
|
61
|
+
"Action": ["iam:GetRole", "iam:PassRole", "iam:GetPolicy"],
|
|
62
|
+
"Resource": [
|
|
63
|
+
"arn:aws:iam::${TUTORIAL_ACCOUNT_ID}:role/${TUTORIAL_PREFIX}-token-exchange",
|
|
64
|
+
"arn:aws:iam::${TUTORIAL_ACCOUNT_ID}:policy/${TUTORIAL_PREFIX}-token-exchangeAccess",
|
|
65
|
+
"arn:aws:iam::aws:policy/${TUTORIAL_PREFIX}-token-exchangeAccess"
|
|
66
|
+
]
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
"Sid": "CreateIoTResources",
|
|
70
|
+
"Effect": "Allow",
|
|
71
|
+
"Action": [
|
|
72
|
+
"iot:AddThingToThingGroup",
|
|
73
|
+
"iot:AttachPolicy",
|
|
74
|
+
"iot:AttachThingPrincipal",
|
|
75
|
+
"iot:CreateKeysAndCertificate",
|
|
76
|
+
"iot:CreatePolicy",
|
|
77
|
+
"iot:CreateRoleAlias",
|
|
78
|
+
"iot:CreateThing",
|
|
79
|
+
"iot:CreateThingGroup",
|
|
80
|
+
"iot:DescribeEndpoint",
|
|
81
|
+
"iot:DescribeRoleAlias",
|
|
82
|
+
"iot:DescribeThingGroup",
|
|
83
|
+
"iot:GetPolicy"
|
|
84
|
+
],
|
|
85
|
+
"Resource": "*"
|
|
86
|
+
}
|
|
87
|
+
]
|
|
88
|
+
}
|
|
89
|
+
EOF
|
|
90
|
+
|
|
91
|
+
aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" iam put-role-policy \
|
|
92
|
+
--role-name "${TUTORIAL_PREFIX}-instance" \
|
|
93
|
+
--policy-name greengrass-installer-bootstrap \
|
|
94
|
+
--policy-document file:///tmp/greengrass-tutorial-installer.json
|
|
95
|
+
aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" iam create-instance-profile \
|
|
96
|
+
--instance-profile-name "${TUTORIAL_PREFIX}-instance"
|
|
97
|
+
aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" iam add-role-to-instance-profile \
|
|
98
|
+
--instance-profile-name "${TUTORIAL_PREFIX}-instance" \
|
|
99
|
+
--role-name "${TUTORIAL_PREFIX}-instance"
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Task 4 removes `greengrass-installer-bootstrap` again once the device is provisioned.
|
|
103
|
+
|
|
104
|
+
## Task 2: Select the Ubuntu 26.04 x86_64 image
|
|
105
|
+
|
|
106
|
+
Canonical publishes its current image IDs as public Systems Manager parameters. Read the one for Ubuntu 26.04 on amd64, then confirm the owner, architecture and name before you use it:
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
export TUTORIAL_AMI_ID=$(aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" ssm get-parameter \
|
|
110
|
+
--name /aws/service/canonical/ubuntu/server/26.04/stable/current/amd64/hvm/ebs-gp3/ami-id \
|
|
111
|
+
--query Parameter.Value --output text)
|
|
112
|
+
|
|
113
|
+
aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" ec2 describe-images \
|
|
114
|
+
--image-ids "$TUTORIAL_AMI_ID" \
|
|
115
|
+
--query 'Images[0].[OwnerId,Architecture,Name]' --output text
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
The output must show owner `099720109477` (Canonical), architecture `x86_64`, and a name containing `ubuntu-resolute-26.04-amd64-server`. The script checks the same three values and refuses any other image.
|
|
119
|
+
|
|
120
|
+
Ubuntu 26.04 is used for two reasons. It ships `python3.14`, which is the interpreter the generated recipe's `Run` step pins. It also ships `sudo-rs` as the default `sudo`, which can prevent IPC components from starting under Nucleus 2.18.3 because the IPC environment is lost. The script installs `openjdk-21-jre-headless`, `unzip`, `python3.14` and the classic `sudo` package, which provides `/usr/bin/sudo.ws`, then selects `/usr/bin/sudo.ws` as `sudo` and verifies the selection before it installs the nucleus. On the Canonical image both `sudo` and `sudo-rs` are preinstalled, so the alternatives switch is the effective step. See the <Link path="guides/greengrass-component#troubleshooting-and-service-limits">component troubleshooting notes</Link>.
|
|
121
|
+
|
|
122
|
+
## Task 3: Download and run the setup script
|
|
123
|
+
|
|
124
|
+
Download the script from this documentation site, read it, then run it from the workspace root:
|
|
125
|
+
|
|
126
|
+
<Code lang="bash" code={`curl -fsSLo setup-greengrass-tutorial.sh ${setupScriptUrl}\nchmod +x setup-greengrass-tutorial.sh\nless setup-greengrass-tutorial.sh`} />
|
|
127
|
+
|
|
128
|
+
The same file is checked into the repository at `docs/public/greengrass/setup-tutorial.sh`.
|
|
129
|
+
|
|
130
|
+
```bash
|
|
131
|
+
./setup-greengrass-tutorial.sh setup \
|
|
132
|
+
--profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" --prefix "$TUTORIAL_PREFIX" \
|
|
133
|
+
--ami-id "$TUTORIAL_AMI_ID" --instance-profile "${TUTORIAL_PREFIX}-instance"
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
Pass `--subnet-id <subnet-id>` when the Region has no default VPC. Pass `--instance-type` to change the default `t3.small`.
|
|
137
|
+
|
|
138
|
+
The script performs these steps in order, and writes the state file after each created resource:
|
|
139
|
+
|
|
140
|
+
1. Resolves the caller identity, validates the image, and checks that the instance profile exists.
|
|
141
|
+
2. Checks that none of the resources in the table above exist yet. An existing thing group, thing, role, policy or role alias under the prefix stops the script before it creates anything.
|
|
142
|
+
3. Creates the thing group and thing, the token exchange role with its access policy, the role alias, and the device IoT policy. The IoT policy is the [minimal core device policy](https://docs.aws.amazon.com/greengrass/v2/developerguide/device-auth.html#greengrass-core-minimal-iot-policy) from the developer guide, plus `iot:Publish` on `arn:aws:iot:<region>:<account-id>:topic/tutorial/greengrass/heartbeat`.
|
|
143
|
+
4. Launches the instance with the instance profile and IMDSv2 required, waits for it to register with Systems Manager, and sends one `AWS-RunShellScript` command that installs the prerequisites, switches `sudo`, downloads Greengrass nucleus 2.18.3, and runs the installer with `--provision true --setup-system-service true`, the thing, group, policy, role and alias names above, and `--component-default-user ggc_user:ggc_group`.
|
|
144
|
+
5. Polls the command until it finishes, then records the certificate the installer attached to the thing and waits for the core device to appear in Greengrass.
|
|
145
|
+
|
|
146
|
+
Setup takes about ten minutes. When it finishes, load the recorded values into your shell and keep the state file out of Git:
|
|
147
|
+
|
|
148
|
+
```bash
|
|
149
|
+
source .greengrass-tutorial.env
|
|
150
|
+
echo .greengrass-tutorial.env >> .gitignore
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
The state file exports `TUTORIAL_THING_NAME`, `TUTORIAL_THING_GROUP_ARN`, `TUTORIAL_TOKEN_EXCHANGE_ROLE_ARN`, `TUTORIAL_INSTANCE_ID`, `TUTORIAL_CERTIFICATE_ARN` and the identity it is bound to. Module 3 passes two of them to the CDK stack.
|
|
154
|
+
|
|
155
|
+
<Aside type="note" title="Reruns and failures">
|
|
156
|
+
Running `setup` again after a successful run reports the existing instance and changes nothing. If a run stops part way, for example because the install command fails, the state file records how far it got and a rerun refuses to continue. Run `cleanup` with the same profile, Region and prefix, fix the cause, then run `setup` again. Cleanup only touches the resources the state file records, tolerates objects that are already gone, and stops on any other error.
|
|
157
|
+
</Aside>
|
|
158
|
+
|
|
159
|
+
## Task 4: Remove the temporary installer permissions
|
|
160
|
+
|
|
161
|
+
The installer has finished. Remove the bootstrap policy so the instance keeps Systems Manager access only:
|
|
162
|
+
|
|
163
|
+
```bash
|
|
164
|
+
aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" iam delete-role-policy \
|
|
165
|
+
--role-name "${TUTORIAL_PREFIX}-instance" \
|
|
166
|
+
--policy-name greengrass-installer-bootstrap
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
## Task 5: Check the device
|
|
170
|
+
|
|
171
|
+
The nucleus registers the core device on its first start. Confirm it from the cloud:
|
|
172
|
+
|
|
173
|
+
```bash
|
|
174
|
+
aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" greengrassv2 get-core-device \
|
|
175
|
+
--core-device-thing-name "$TUTORIAL_THING_NAME" \
|
|
176
|
+
--query '{status: status, coreVersion: coreVersion, platform: platform, architecture: architecture}'
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
Expect `HEALTHY`, `linux`, and `amd64`. To read a log on the instance, send a Systems Manager command with a parameters file. The first line is a shebang, which `AWS-RunShellScript` honours:
|
|
180
|
+
|
|
181
|
+
```bash
|
|
182
|
+
cat > /tmp/greengrass-tutorial-command.json <<'EOF'
|
|
183
|
+
{ "commands": ["#!/bin/bash", "tail -n 20 /greengrass/v2/logs/greengrass.log"] }
|
|
184
|
+
EOF
|
|
185
|
+
|
|
186
|
+
TUTORIAL_COMMAND_ID=$(aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" ssm send-command \
|
|
187
|
+
--instance-ids "$TUTORIAL_INSTANCE_ID" --document-name AWS-RunShellScript \
|
|
188
|
+
--parameters file:///tmp/greengrass-tutorial-command.json \
|
|
189
|
+
--query Command.CommandId --output text)
|
|
190
|
+
sleep 5
|
|
191
|
+
aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" ssm get-command-invocation \
|
|
192
|
+
--command-id "$TUTORIAL_COMMAND_ID" --instance-id "$TUTORIAL_INSTANCE_ID" \
|
|
193
|
+
--query StandardOutputContent --output text
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
Module 3 uses the same pattern to read the component's own log.
|
|
197
|
+
|
|
198
|
+
Continue with <Link path="get_started/tutorials/greengrass/3">Deploy, observe and update</Link>.
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Deploy, observe and update
|
|
3
|
+
description: Compose the artifact repository, read grant, component version and deployment in CDK, watch the heartbeat arrive in AWS IoT Core, then publish a NEXT_PATCH update
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import { Aside } from '@astrojs/starlight/components';
|
|
7
|
+
import E2EDiff from '@components/e2e-diff.astro';
|
|
8
|
+
import InstallCommand from '@components/install-command.astro';
|
|
9
|
+
import Link from '@components/link.astro';
|
|
10
|
+
import NxCommands from '@components/nx-commands.astro';
|
|
11
|
+
|
|
12
|
+
## Task 1: Compose the stack
|
|
13
|
+
|
|
14
|
+
The generator vended building blocks, not a deployment. Your stack decides where artifacts live, which role may read them, and which fleet receives the component. Replace `packages/infra/src/stacks/application-stack.ts`:
|
|
15
|
+
|
|
16
|
+
<E2EDiff before="greengrass-tutorial/application-stack.ts.original.template" after="greengrass-tutorial/application-stack.ts.template" lang="typescript" />
|
|
17
|
+
|
|
18
|
+
What each part does:
|
|
19
|
+
|
|
20
|
+
- `GreengrassArtifactBucket` creates the retained, versioned, TLS-only artifact repository. `grantReadTo` gives the token exchange role `s3:GetObject` on `com.example.Pulse/*`, which covers every artifact key the resolver writes for this component.
|
|
21
|
+
- `Pulse` extends `GreengrassComponentVersion`. It reads the recipe and `pulse.zip` that `pulse-artifact` built, uploads the archive under a content-hashed key, and publishes the component version. Its `bucket` property takes the `GreengrassArtifactBucket` construct itself.
|
|
22
|
+
- `GreengrassDeployment` with `targetArn` deploys to the thing group the setup script created. `addComponent(pulse)` puts the resolved version into the deployment's component map and adds a CloudFormation dependency on the component version resource, so the IoT job cannot reach the device before the version exists.
|
|
23
|
+
- The two `CfnParameter` values come from `.greengrass-tutorial.env`. Passing them as parameters keeps the stack free of account-specific literals and lets the e2e suite synthesize the same file.
|
|
24
|
+
|
|
25
|
+
## Task 2: Bootstrap and deploy
|
|
26
|
+
|
|
27
|
+
Load the recorded values, synchronize project references and dependency manifests, install the updated workspace dependencies, and bootstrap CDK in the account and Region if you have not done so before:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
source .greengrass-tutorial.env
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
<NxCommands commands={['sync']} />
|
|
34
|
+
<InstallCommand />
|
|
35
|
+
<NxCommands env={{ AWS_PROFILE: '"$TUTORIAL_AWS_PROFILE"', AWS_REGION: '"$TUTORIAL_AWS_REGION"' }} commands={['run @greengrass-tutorial/infra:bootstrap']} />
|
|
36
|
+
|
|
37
|
+
Deploy the stack. `packages/infra/src/main.ts` places the stack in the stage `greengrass-tutorial-infra-sandbox`, and the `deploy` target runs `cdk deploy` without a stage of its own, so the command names that stage and passes the role and thing group ARNs as CloudFormation parameters:
|
|
38
|
+
|
|
39
|
+
<NxCommands env={{ AWS_PROFILE: '"$TUTORIAL_AWS_PROFILE"', AWS_REGION: '"$TUTORIAL_AWS_REGION"' }} commands={['run @greengrass-tutorial/infra:deploy -- "greengrass-tutorial-infra-sandbox/**" --parameters TokenExchangeRoleArn="$TUTORIAL_TOKEN_EXCHANGE_ROLE_ARN" --parameters ThingGroupArn="$TUTORIAL_THING_GROUP_ARN"']} />
|
|
40
|
+
|
|
41
|
+
`deploy` depends on `^assemble`, and the shared constructs project depends on the telemetry project's `assemble`, which runs `pulse-artifact`. Every deploy therefore publishes the artifact that matches your current source. The target runs `cdk deploy` without express mode, so it accepts a later deployment revision that replaces the deployment resource. Read <Link path="guides/greengrass-component#deployment-revisions">deployment revisions</Link> before using `deploy-sandbox` for Greengrass.
|
|
42
|
+
|
|
43
|
+
On the first deploy the version resolver finds no published version of `com.example.Pulse` and publishes `1.0.0`. The deployment then reaches the thing group, and the core device downloads the artifact with the token exchange role and starts the component. Allow one to two minutes after the stack completes.
|
|
44
|
+
|
|
45
|
+
## Task 3: Watch the heartbeat
|
|
46
|
+
|
|
47
|
+
Open the AWS IoT console in `$TUTORIAL_AWS_REGION`, choose **MQTT test client**, and subscribe to the topic:
|
|
48
|
+
|
|
49
|
+
```text
|
|
50
|
+
tutorial/greengrass/heartbeat
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Every 30 seconds a message arrives:
|
|
54
|
+
|
|
55
|
+
```json
|
|
56
|
+
{"sequence": 3, "greeting": "hello from pulse"}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
The AWS CLI cannot subscribe to MQTT topics. If you prefer your own MQTT client, connect it to the account's data endpoint with its own certificate and policy, which this tutorial does not create:
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" iot describe-endpoint \
|
|
63
|
+
--endpoint-type iot:Data-ATS --query endpointAddress --output text
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Task 4: Check the device
|
|
67
|
+
|
|
68
|
+
A successful deployment proves the device applied the deployment document. It does not prove the component runs. Check the installed component's lifecycle state:
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" greengrassv2 list-installed-components \
|
|
72
|
+
--core-device-thing-name "$TUTORIAL_THING_NAME" \
|
|
73
|
+
--query 'installedComponents[?componentName==`com.example.Pulse`].[componentVersion,lifecycleState]' \
|
|
74
|
+
--output text
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Expect `1.0.0` and `RUNNING`. Then read the component's own log on the instance with the Systems Manager pattern from module 2:
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
cat > /tmp/greengrass-tutorial-command.json <<'EOF'
|
|
81
|
+
{ "commands": ["#!/bin/bash", "tail -n 20 /greengrass/v2/logs/com.example.Pulse.log"] }
|
|
82
|
+
EOF
|
|
83
|
+
|
|
84
|
+
TUTORIAL_COMMAND_ID=$(aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" ssm send-command \
|
|
85
|
+
--instance-ids "$TUTORIAL_INSTANCE_ID" --document-name AWS-RunShellScript \
|
|
86
|
+
--parameters file:///tmp/greengrass-tutorial-command.json \
|
|
87
|
+
--query Command.CommandId --output text)
|
|
88
|
+
sleep 5
|
|
89
|
+
aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" ssm get-command-invocation \
|
|
90
|
+
--command-id "$TUTORIAL_COMMAND_ID" --instance-id "$TUTORIAL_INSTANCE_ID" \
|
|
91
|
+
--query StandardOutputContent --output text
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
The log shows the IPC connection and one line per heartbeat:
|
|
95
|
+
|
|
96
|
+
```text
|
|
97
|
+
INFO:awsiot.eventstreamrpc:<Connection ...> /greengrass/v2/ipc.socket:0> connected
|
|
98
|
+
INFO:com.example.Pulse:Published heartbeat 0 to tutorial/greengrass/heartbeat
|
|
99
|
+
INFO:com.example.Pulse:Published heartbeat 1 to tutorial/greengrass/heartbeat
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
If the deployment reports `SUCCEEDED` but the component is `BROKEN`, or `get-core-device` reports `UNHEALTHY`, follow the <Link path="guides/greengrass-component#troubleshooting-and-service-limits">component troubleshooting steps</Link>.
|
|
103
|
+
|
|
104
|
+
## Task 5: Update the component
|
|
105
|
+
|
|
106
|
+
Change the greeting in `packages/telemetry/greengrass/pulse/main.py`:
|
|
107
|
+
|
|
108
|
+
<E2EDiff before="greengrass-tutorial/main.py.template" after="greengrass-tutorial/main.py.updated.template" lang="python" />
|
|
109
|
+
|
|
110
|
+
Deploy again with the same command:
|
|
111
|
+
|
|
112
|
+
<NxCommands env={{ AWS_PROFILE: '"$TUTORIAL_AWS_PROFILE"', AWS_REGION: '"$TUTORIAL_AWS_REGION"' }} commands={['run @greengrass-tutorial/infra:deploy -- "greengrass-tutorial-infra-sandbox/**" --parameters TokenExchangeRoleArn="$TUTORIAL_TOKEN_EXCHANGE_ROLE_ARN" --parameters ThingGroupArn="$TUTORIAL_THING_GROUP_ARN"']} />
|
|
113
|
+
|
|
114
|
+
Nx sees the changed source and rebuilds `pulse-artifact`. The archive's content hash changes, so the resolver allocates `1.0.1`, the artifact is uploaded under a new key, a new component version is published, and a new deployment revision points the thing group at it. The recipe still says `NEXT_PATCH`; nothing in the workspace records the number.
|
|
115
|
+
|
|
116
|
+
Verify both the installed version and the payload:
|
|
117
|
+
|
|
118
|
+
```bash
|
|
119
|
+
aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" greengrassv2 list-installed-components \
|
|
120
|
+
--core-device-thing-name "$TUTORIAL_THING_NAME" \
|
|
121
|
+
--query 'installedComponents[?componentName==`com.example.Pulse`].[componentVersion,lifecycleState]' \
|
|
122
|
+
--output text
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
Expect `1.0.1` and `RUNNING`. In the MQTT test client the greeting changes to `hello from the updated pulse`, and the sequence restarts from `0` because the component process was restarted.
|
|
126
|
+
|
|
127
|
+
<Aside type="tip" title="Unchanged redeploys">
|
|
128
|
+
Deploying again without a source or recipe change reuses `1.0.1`. The resolver compares the content hash with the tag on the highest published version and returns that version instead of allocating a new one. Read <Link path="guides/greengrass-component#automatic-versioning">automatic versioning</Link> for the rules and their limits.
|
|
129
|
+
</Aside>
|
|
130
|
+
|
|
131
|
+
Continue with <Link path="get_started/tutorials/greengrass/cleanup">Remove and clean up</Link>.
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Remove and clean up
|
|
3
|
+
description: Remove the component from the device with an empty deployment revision, destroy the stack, then delete the retained Greengrass resources and everything the setup script created
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import { Aside } from '@astrojs/starlight/components';
|
|
7
|
+
import E2EDiff from '@components/e2e-diff.astro';
|
|
8
|
+
import Link from '@components/link.astro';
|
|
9
|
+
import NxCommands from '@components/nx-commands.astro';
|
|
10
|
+
|
|
11
|
+
Cleanup runs in the reverse order of creation: first the device stops running the component, then the stack goes, then the resources the stack retained, then the device and its identity, and finally the instance profile.
|
|
12
|
+
|
|
13
|
+
## Task 1: Remove the component from the device
|
|
14
|
+
|
|
15
|
+
Deleting a deployment resource removes deployment management state. It does not uninstall anything from a device. The device only removes a component when it receives a deployment revision that no longer lists it. Change the stack so the deployment lists no components while the published version stays managed:
|
|
16
|
+
|
|
17
|
+
<E2EDiff before="greengrass-tutorial/application-stack.ts.template" after="greengrass-tutorial/application-stack.ts.removed.template" lang="typescript" />
|
|
18
|
+
|
|
19
|
+
Deploy the revision:
|
|
20
|
+
|
|
21
|
+
<NxCommands env={{ AWS_PROFILE: '"$TUTORIAL_AWS_PROFILE"', AWS_REGION: '"$TUTORIAL_AWS_REGION"' }} commands={['run @greengrass-tutorial/infra:deploy -- "greengrass-tutorial-infra-sandbox/**" --parameters TokenExchangeRoleArn="$TUTORIAL_TOKEN_EXCHANGE_ROLE_ARN" --parameters ThingGroupArn="$TUTORIAL_THING_GROUP_ARN"']} />
|
|
22
|
+
|
|
23
|
+
Wait until the component disappears from the device and the device stays healthy:
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" greengrassv2 list-installed-components \
|
|
27
|
+
--core-device-thing-name "$TUTORIAL_THING_NAME" \
|
|
28
|
+
--query 'installedComponents[].componentName' --output text
|
|
29
|
+
aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" greengrassv2 get-core-device \
|
|
30
|
+
--core-device-thing-name "$TUTORIAL_THING_NAME" --query status --output text
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
The first command must not list `com.example.Pulse` any more. The second must print `HEALTHY`. The MQTT test client stops receiving heartbeats.
|
|
34
|
+
|
|
35
|
+
## Task 2: Destroy the stack
|
|
36
|
+
|
|
37
|
+
<NxCommands env={{ AWS_PROFILE: '"$TUTORIAL_AWS_PROFILE"', AWS_REGION: '"$TUTORIAL_AWS_REGION"' }} commands={['run @greengrass-tutorial/infra:destroy -- "greengrass-tutorial-infra-sandbox/**" --force']} />
|
|
38
|
+
|
|
39
|
+
The stack is gone, but three kinds of resources are retained on purpose, because component versions are immutable and outlive any single deployment:
|
|
40
|
+
|
|
41
|
+
- Both component versions of `com.example.Pulse`, `1.0.0` and `1.0.1`.
|
|
42
|
+
- The three deployment revisions to the thing group.
|
|
43
|
+
- The artifact bucket, with one archive per published version.
|
|
44
|
+
|
|
45
|
+
The CDK bootstrap stack `CDKToolkit` also remains. It is shared by every CDK application in the account and Region, so leave it unless you created it for this tutorial.
|
|
46
|
+
|
|
47
|
+
CDK leaves the Lambda log groups for component version resolution, the
|
|
48
|
+
custom-resource framework, and `BucketDeployment` after stack deletion; delete
|
|
49
|
+
them separately if you no longer need their logs.
|
|
50
|
+
|
|
51
|
+
## Task 3: Delete the retained Greengrass resources
|
|
52
|
+
|
|
53
|
+
List the deployments to the thing group. Cancel the one still `ACTIVE`, then delete each of them:
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" greengrassv2 list-deployments \
|
|
57
|
+
--target-arn "$TUTORIAL_THING_GROUP_ARN" --history-filter ALL \
|
|
58
|
+
--query 'deployments[].[deploymentId,deploymentStatus]' --output text
|
|
59
|
+
|
|
60
|
+
aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" greengrassv2 cancel-deployment \
|
|
61
|
+
--deployment-id <active-deployment-id>
|
|
62
|
+
aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" greengrassv2 delete-deployment \
|
|
63
|
+
--deployment-id <deployment-id>
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Delete every published version of the component:
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" greengrassv2 list-component-versions \
|
|
70
|
+
--arn "arn:aws:greengrass:${TUTORIAL_AWS_REGION}:${TUTORIAL_ACCOUNT_ID}:components:com.example.Pulse" \
|
|
71
|
+
--query 'componentVersions[].arn' --output text
|
|
72
|
+
|
|
73
|
+
aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" greengrassv2 delete-component \
|
|
74
|
+
--arn <component-version-arn>
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Find the artifact bucket through the ownership tag the construct applies, confirm that the list holds only this tutorial's bucket, then delete its object versions and the bucket:
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" resourcegroupstaggingapi get-resources \
|
|
81
|
+
--tag-filters Key=greengrass-artifact-repository,Values=true \
|
|
82
|
+
--resource-type-filters s3:bucket \
|
|
83
|
+
--query 'ResourceTagMappingList[].ResourceARN' --output text
|
|
84
|
+
|
|
85
|
+
export TUTORIAL_BUCKET=<bucket-name>
|
|
86
|
+
aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" s3api list-object-versions \
|
|
87
|
+
--bucket "$TUTORIAL_BUCKET" \
|
|
88
|
+
--query '{Objects: Versions[].{Key: Key, VersionId: VersionId}}' --output json \
|
|
89
|
+
> /tmp/greengrass-tutorial-versions.json
|
|
90
|
+
aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" s3api delete-objects \
|
|
91
|
+
--bucket "$TUTORIAL_BUCKET" --delete file:///tmp/greengrass-tutorial-versions.json
|
|
92
|
+
aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" s3api delete-bucket \
|
|
93
|
+
--bucket "$TUTORIAL_BUCKET"
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
The bucket is versioned, so `delete-objects` must name each version. If `list-object-versions` also reports `DeleteMarkers`, repeat the delete with those entries before deleting the bucket.
|
|
97
|
+
|
|
98
|
+
## Task 4: Remove the core device and its identity
|
|
99
|
+
|
|
100
|
+
The script's `cleanup` operation reads `.greengrass-tutorial.env`, checks that it belongs to the same account, Region and prefix, and then terminates the instance, detaches and deletes the certificate with every policy attached to it, deletes the core device record, the thing and the thing group, both IoT policies, the role alias, the token exchange role with its attachments, and the access policy.
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
./setup-greengrass-tutorial.sh cleanup \
|
|
104
|
+
--profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" --prefix "$TUTORIAL_PREFIX"
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Each step prints `deleted` or `absent`. An object that is already gone is skipped. Any other error stops the run and keeps the state file, so you can fix the cause and rerun the same command.
|
|
108
|
+
|
|
109
|
+
<Aside type="note" title="Order matters for the role">
|
|
110
|
+
Destroy the stack before running cleanup. The stack's read grant is an inline policy on the token exchange role. Cleanup deletes inline policies it finds so the role can be removed, but a stack that still references the role would then fail its next update.
|
|
111
|
+
</Aside>
|
|
112
|
+
|
|
113
|
+
## Task 5: Remove the instance profile
|
|
114
|
+
|
|
115
|
+
The instance profile was yours, so the script leaves it alone:
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" iam remove-role-from-instance-profile \
|
|
119
|
+
--instance-profile-name "${TUTORIAL_PREFIX}-instance" --role-name "${TUTORIAL_PREFIX}-instance"
|
|
120
|
+
aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" iam delete-instance-profile \
|
|
121
|
+
--instance-profile-name "${TUTORIAL_PREFIX}-instance"
|
|
122
|
+
aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" iam detach-role-policy \
|
|
123
|
+
--role-name "${TUTORIAL_PREFIX}-instance" \
|
|
124
|
+
--policy-arn arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore
|
|
125
|
+
aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" iam delete-role \
|
|
126
|
+
--role-name "${TUTORIAL_PREFIX}-instance"
|
|
127
|
+
rm -f .greengrass-tutorial.env
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## What you learned
|
|
131
|
+
|
|
132
|
+
- A generated component publishes over local IPC by default. Cloud MQTT needs `publish_to_iot_core`, a `aws.greengrass.ipc.mqttproxy` authorization in the recipe, and `iot:Publish` in the device's IoT policy.
|
|
133
|
+
- The generator vends building blocks. Your CDK stack owns the artifact repository, the read grant, and the deployment, and `addComponent` carries the resolved version and the ordering edge.
|
|
134
|
+
- `NEXT_PATCH` allocates a version from content. A source change publishes the next patch version through the normal `deploy`.
|
|
135
|
+
- Removing a component is a deployment revision. Deleting infrastructure is a separate, later step that leaves retained versions, deployments and the bucket for you to delete.
|
|
136
|
+
|
|
137
|
+
For versioning and artifact retention, read the <Link path="guides/greengrass-component#automatic-versioning">versioning</Link> and <Link path="guides/greengrass-component#artifact-storage-and-retention">artifact storage</Link> sections of the component guide. The same guide lists the service limits to consider for larger deployments.
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Greengrass MQTT tutorial
|
|
3
|
+
description: Build a Python Greengrass component, run it on an EC2 core device, watch its heartbeat arrive in AWS IoT Core over MQTT, publish a NEXT_PATCH update, then remove everything
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import Link from '@components/link.astro';
|
|
7
|
+
import Snippet from '@components/snippet.astro';
|
|
8
|
+
|
|
9
|
+
In this tutorial you build a Python [AWS IoT Greengrass v2](https://docs.aws.amazon.com/greengrass/v2/developerguide/what-is-iot-greengrass.html) component with `@aws/nx-plugin`, run it on an Ubuntu 26.04 x86_64 EC2 instance that acts as the core device, and watch its heartbeat arrive in AWS IoT Core over MQTT. You then change the source, publish the next version with `NEXT_PATCH`, remove the component from the device, and delete every resource you created.
|
|
10
|
+
|
|
11
|
+
The tutorial uses CDK and the classic Greengrass nucleus. For TypeScript components, Terraform, or an existing fleet, read the <Link path="guides/greengrass-component">Greengrass component guide</Link>.
|
|
12
|
+
|
|
13
|
+
## What you build
|
|
14
|
+
|
|
15
|
+
| Piece | Where | Created by |
|
|
16
|
+
| --- | --- | --- |
|
|
17
|
+
| Python project `greengrass_tutorial.telemetry` | `packages/telemetry` | `py#project` |
|
|
18
|
+
| Component `com.example.Pulse` | `packages/telemetry/greengrass/pulse` | `greengrass#component` |
|
|
19
|
+
| Greengrass core constructs and the `Pulse` app construct | `packages/common/constructs/src/core/greengrass`, `packages/common/constructs/src/app/greengrass` | `greengrass#component` |
|
|
20
|
+
| CDK application `@greengrass-tutorial/infra` | `packages/infra` | `ts#infra` |
|
|
21
|
+
|
|
22
|
+
The generated component publishes over local Greengrass IPC. Module 1 changes it to publish to AWS IoT Core, which needs a different IPC operation, a different recipe authorization, and an IoT policy statement on the device. The API and access-control names come from the [AWS IoT Core MQTT IPC documentation](https://docs.aws.amazon.com/greengrass/v2/developerguide/ipc-iot-core-mqtt.html).
|
|
23
|
+
|
|
24
|
+
## What it creates in AWS
|
|
25
|
+
|
|
26
|
+
| Resource | Created by | Deleted by |
|
|
27
|
+
| --- | --- | --- |
|
|
28
|
+
| IoT thing group, thing, certificate, IoT policy, token exchange role, role alias, IAM access policy | The setup script and the nucleus installer | The script's `cleanup` operation |
|
|
29
|
+
| EC2 `t3.small` instance with an SSM instance profile | The setup script (instance) and you (instance profile) | The script's `cleanup` operation and you |
|
|
30
|
+
| Artifact bucket, component versions, deployment, artifact read grant | Your CDK stack | `destroy`, then manual deletion of the retained bucket, versions and deployments |
|
|
31
|
+
|
|
32
|
+
The instance runs until you clean up. Use an AWS account and Region you can clean up completely.
|
|
33
|
+
|
|
34
|
+
## Before you start
|
|
35
|
+
|
|
36
|
+
<Snippet name="required-prerequisites" />
|
|
37
|
+
|
|
38
|
+
You also need:
|
|
39
|
+
|
|
40
|
+
- [AWS CLI v2](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) with a named profile for the target account.
|
|
41
|
+
- Permissions in that account to create IAM roles and policies, IoT things and policies, EC2 instances, Systems Manager commands, S3 buckets, CloudFormation stacks and Greengrass resources.
|
|
42
|
+
- A default VPC in the Region, or a subnet with outbound internet access that you pass to the setup script.
|
|
43
|
+
|
|
44
|
+
Every AWS CLI command in this tutorial passes `--profile` and `--region` explicitly, and every CDK command receives `AWS_PROFILE` and `AWS_REGION`. Set the values once in your shell:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
export TUTORIAL_AWS_PROFILE=<your-profile>
|
|
48
|
+
export TUTORIAL_AWS_REGION=us-east-1
|
|
49
|
+
export TUTORIAL_PREFIX=gg-tutorial
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
`TUTORIAL_PREFIX` names every resource the setup script creates. Pick a value that nothing else in the account uses.
|
|
53
|
+
|
|
54
|
+
## What is verified automatically
|
|
55
|
+
|
|
56
|
+
The commands, source edits, build and CDK synthesis of this tutorial are covered by the `greengrass-tutorial` smoke test in CI. The setup script's identity, refusal and cleanup behaviour runs against a recorded AWS CLI in the docs test suite.
|
|
57
|
+
|
|
58
|
+
Provisioning, MQTT delivery, device updates and cleanup require an AWS account and remain outside the automated lane. Follow the device checks in each module to verify the deployment. The <Link path="guides/greengrass-component#hardware-and-operating-systems">hardware support table</Link> records the supported device platforms.
|
|
59
|
+
|
|
60
|
+
## Modules
|
|
61
|
+
|
|
62
|
+
1. <Link path="get_started/tutorials/greengrass/1">Create the component</Link>: workspace, Python project, component, CDK application, and the switch to AWS IoT Core MQTT.
|
|
63
|
+
2. <Link path="get_started/tutorials/greengrass/2">Provision the core device</Link>: instance profile, image selection, the setup script, and what it creates.
|
|
64
|
+
3. <Link path="get_started/tutorials/greengrass/3">Deploy, observe and update</Link>: the CDK stack, the first deployment, the MQTT heartbeat, and a `NEXT_PATCH` update.
|
|
65
|
+
4. <Link path="get_started/tutorials/greengrass/cleanup">Remove and clean up</Link>: an empty deployment revision, stack destruction, and deletion of the retained and script-created resources.
|
|
@@ -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.
|