Bỏ qua để đến nội dung

Triển khai, quan sát và cập nhật

Generator cung cấp các khối xây dựng, không phải deployment. Stack của bạn quyết định nơi lưu trữ artifacts, role nào có thể đọc chúng, và fleet nào nhận component. Thay thế packages/infra/src/stacks/application-stack.ts:

import {
GreengrassArtifactBucket,
GreengrassDeployment,
Pulse,
} from '@greengrass-tutorial/common-constructs';
import { CfnParameter, Stack, type StackProps } from 'aws-cdk-lib';
import { Role } from 'aws-cdk-lib/aws-iam';
import type { Construct } from 'constructs';
export class ApplicationStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
// Both values are written to .greengrass-tutorial.env by the setup script
// and passed with --parameters on every deploy.
const tokenExchangeRoleArn = new CfnParameter(
this,
'TokenExchangeRoleArn',
{
type: 'String',
description: 'ARN of the core device token exchange role',
},
);
const thingGroupArn = new CfnParameter(this, 'ThingGroupArn', {
type: 'String',
description: 'ARN of the thing group that holds the core device',
});
// The retained artifact repository, and the read grant the core device
// needs to fetch this component's artifacts from it.
const artifacts = new GreengrassArtifactBucket(this, 'Artifacts');
const tokenExchangeRole = Role.fromRoleArn(
this,
'TokenExchangeRole',
tokenExchangeRoleArn.valueAsString,
);
artifacts.grantReadTo(tokenExchangeRole, 'com.example.Pulse/*');
// Publishes the artifact built by pulse-artifact, then deploys it to the
// existing thing group. addComponent carries the resolved NEXT_PATCH
// version and orders the deployment after the component version.
const pulse = new Pulse(this, 'Pulse', { bucket: artifacts });
const fleet = new GreengrassDeployment(this, 'Fleet', {
targetArn: thingGroupArn.valueAsString,
});
fleet.addComponent(pulse);
}
}

Chức năng của từng phần:

  • GreengrassArtifactBucket tạo kho lưu trữ artifact được giữ lại, có phiên bản, chỉ dùng TLS. grantReadTo cấp cho token exchange role quyền s3:GetObject trên com.example.Pulse/*, bao gồm mọi artifact key mà resolver ghi cho component này.
  • Pulse mở rộng GreengrassComponentVersion. Nó đọc recipe và pulse.zip mà pulse-artifact đã build, tải lên archive dưới một key được hash nội dung, và xuất bản phiên bản component. Thuộc tính bucket của nó nhận chính construct GreengrassArtifactBucket.
  • GreengrassDeployment với targetArn triển khai đến thing group mà script thiết lập đã tạo. addComponent(pulse) đặt phiên bản đã resolve vào component map của deployment và thêm dependency CloudFormation vào tài nguyên phiên bản component, do đó IoT job không thể đến thiết bị trước khi phiên bản tồn tại.
  • Hai giá trị CfnParameter đến từ .greengrass-tutorial.env. Truyền chúng dưới dạng parameters giữ cho stack không có literals cụ thể theo tài khoản và cho phép bộ e2e suite tổng hợp cùng một file.

Tải các giá trị đã ghi lại, đồng bộ hóa các tham chiếu dự án và dependency manifests, cài đặt các dependencies workspace đã cập nhật, và bootstrap CDK trong tài khoản và Region nếu bạn chưa làm trước đó:

Terminal window
source .greengrass-tutorial.env
Terminal window
pnpm nx sync
Terminal window
pnpm install
Terminal window
AWS_PROFILE="$TUTORIAL_AWS_PROFILE" AWS_REGION="$TUTORIAL_AWS_REGION" pnpm nx run @greengrass-tutorial/infra:bootstrap

Triển khai stack. packages/infra/src/main.ts đặt stack trong stage greengrass-tutorial-infra-sandbox, và target deploy chạy cdk deploy mà không có stage riêng, do đó lệnh đặt tên cho stage đó và truyền các ARN của role và thing group dưới dạng parameters CloudFormation:

Terminal window
AWS_PROFILE="$TUTORIAL_AWS_PROFILE" AWS_REGION="$TUTORIAL_AWS_REGION" pnpm nx run @greengrass-tutorial/infra:deploy -- "greengrass-tutorial-infra-sandbox/**" --parameters TokenExchangeRoleArn="$TUTORIAL_TOKEN_EXCHANGE_ROLE_ARN" --parameters ThingGroupArn="$TUTORIAL_THING_GROUP_ARN"

deploy phụ thuộc vào ^assemble, và dự án shared constructs phụ thuộc vào assemble của dự án telemetry, chạy pulse-artifact. Do đó, mỗi lần deploy đều xuất bản artifact khớp với source hiện tại của bạn. Target chạy cdk deploy mà không có express mode, do đó nó chấp nhận revision deployment sau này thay thế tài nguyên deployment. Đọc deployment revisions trước khi sử dụng deploy-sandbox cho Greengrass.

Ở lần deploy đầu tiên, version resolver không tìm thấy phiên bản đã xuất bản nào của com.example.Pulse và xuất bản 1.0.0. Deployment sau đó đến thing group, và core device tải xuống artifact với token exchange role và khởi động component. Cho phép một đến hai phút sau khi stack hoàn thành.

Mở AWS IoT console trong $TUTORIAL_AWS_REGION, chọn MQTT test client, và đăng ký topic:

tutorial/greengrass/heartbeat

Cứ mỗi 30 giây một message đến:

{"sequence": 3, "greeting": "hello from pulse"}

AWS CLI không thể đăng ký các MQTT topics. Nếu bạn thích MQTT client của riêng mình, kết nối nó với data endpoint của tài khoản với certificate và policy riêng của nó, mà tutorial này không tạo:

Terminal window
aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" iot describe-endpoint \
--endpoint-type iot:Data-ATS --query endpointAddress --output text

Một deployment thành công chứng minh thiết bị đã áp dụng deployment document. Nó không chứng minh component chạy. Kiểm tra lifecycle state của component đã cài đặt:

Terminal window
aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" greengrassv2 list-installed-components \
--core-device-thing-name "$TUTORIAL_THING_NAME" \
--query 'installedComponents[?componentName==`com.example.Pulse`].[componentVersion,lifecycleState]' \
--output text

Mong đợi 1.0.0 và RUNNING. Sau đó đọc log riêng của component trên instance với mẫu Systems Manager từ module 2:

Terminal window
cat > /tmp/greengrass-tutorial-command.json <<'EOF'
{ "commands": ["#!/bin/bash", "tail -n 20 /greengrass/v2/logs/com.example.Pulse.log"] }
EOF
TUTORIAL_COMMAND_ID=$(aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" ssm send-command \
--instance-ids "$TUTORIAL_INSTANCE_ID" --document-name AWS-RunShellScript \
--parameters file:///tmp/greengrass-tutorial-command.json \
--query Command.CommandId --output text)
sleep 5
aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" ssm get-command-invocation \
--command-id "$TUTORIAL_COMMAND_ID" --instance-id "$TUTORIAL_INSTANCE_ID" \
--query StandardOutputContent --output text

Log hiển thị kết nối IPC và một dòng cho mỗi heartbeat:

INFO:awsiot.eventstreamrpc:<Connection ...> /greengrass/v2/ipc.socket:0> connected
INFO:com.example.Pulse:Published heartbeat 0 to tutorial/greengrass/heartbeat
INFO:com.example.Pulse:Published heartbeat 1 to tutorial/greengrass/heartbeat

Nếu deployment báo cáo SUCCEEDED nhưng component là BROKEN, hoặc get-core-device báo cáo UNHEALTHY, hãy làm theo các bước khắc phục sự cố component.

Thay đổi greeting trong packages/telemetry/greengrass/pulse/main.py:

import json
import logging
import time
from awsiot.greengrasscoreipc.clientv2 import GreengrassCoreIPCClientV2
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("com.example.Pulse")
TOPIC = "tutorial/greengrass/heartbeat"
PUBLISH_INTERVAL_SECONDS = 30
GREETING = "hello from the updated pulse"
def publish_heartbeat(client: GreengrassCoreIPCClientV2, sequence: int) -> None:
payload = json.dumps({"sequence": sequence, "greeting": GREETING}).encode("utf-8")
client.publish_to_iot_core(topic_name=TOPIC, qos="1", payload=payload)
logger.info("Published heartbeat %s to %s", sequence, TOPIC)
def main() -> None:
client = GreengrassCoreIPCClientV2()
sequence = 0
while True:
publish_heartbeat(client, sequence)
sequence += 1
time.sleep(PUBLISH_INTERVAL_SECONDS)
if __name__ == "__main__":
main()

Triển khai lại với cùng lệnh:

Terminal window
AWS_PROFILE="$TUTORIAL_AWS_PROFILE" AWS_REGION="$TUTORIAL_AWS_REGION" pnpm nx run @greengrass-tutorial/infra:deploy -- "greengrass-tutorial-infra-sandbox/**" --parameters TokenExchangeRoleArn="$TUTORIAL_TOKEN_EXCHANGE_ROLE_ARN" --parameters ThingGroupArn="$TUTORIAL_THING_GROUP_ARN"

Nx thấy source đã thay đổi và rebuild pulse-artifact. Content hash của archive thay đổi, do đó resolver phân bổ 1.0.1, artifact được tải lên dưới một key mới, một phiên bản component mới được xuất bản, và một revision deployment mới hướng thing group đến nó. Recipe vẫn nói NEXT_PATCH; không có gì trong workspace ghi lại số.

Xác minh cả phiên bản đã cài đặt và payload:

Terminal window
aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" greengrassv2 list-installed-components \
--core-device-thing-name "$TUTORIAL_THING_NAME" \
--query 'installedComponents[?componentName==`com.example.Pulse`].[componentVersion,lifecycleState]' \
--output text

Mong đợi 1.0.1 và RUNNING. Trong MQTT test client, greeting thay đổi thành hello from the updated pulse, và sequence khởi động lại từ 0 vì process component đã được khởi động lại.

Tiếp tục với Remove and clean up.