削除とクリーンアップ
クリーンアップは作成の逆順で実行されます。まずデバイスがコンポーネントの実行を停止し、次にスタックが削除され、その後スタックが保持したリソース、デバイスとそのアイデンティティ、最後にインスタンスプロファイルが削除されます。
タスク1: デバイスからコンポーネントを削除する
Section titled “タスク1: デバイスからコンポーネントを削除する”デプロイメントリソースを削除すると、デプロイメント管理状態が削除されます。デバイスから何もアンインストールされません。デバイスは、コンポーネントがリストされなくなったデプロイメントリビジョンを受信したときにのみ、コンポーネントを削除します。公開されたバージョンは管理されたままで、デプロイメントがコンポーネントをリストしないようにスタックを変更します。
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/*');
// The published version stays managed. This deployment revision lists no // components, so the core device uninstalls com.example.Pulse. new Pulse(this, 'Pulse', { bucket: artifacts }); new GreengrassDeployment(this, 'Fleet', { targetArn: thingGroupArn.valueAsString, }); }}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', { // The published version stays managed. This deployment revision lists no // components, so the core device uninstalls com.example.Pulse. new Pulse(this, 'Pulse', { bucket: artifacts }); new GreengrassDeployment(this, 'Fleet', { targetArn: thingGroupArn.valueAsString, }); fleet.addComponent(pulse); }}リビジョンをデプロイします。
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"AWS_PROFILE="$TUTORIAL_AWS_PROFILE" AWS_REGION="$TUTORIAL_AWS_REGION" yarn nx run @greengrass-tutorial/infra:deploy -- "greengrass-tutorial-infra-sandbox/**" --parameters TokenExchangeRoleArn="$TUTORIAL_TOKEN_EXCHANGE_ROLE_ARN" --parameters ThingGroupArn="$TUTORIAL_THING_GROUP_ARN"AWS_PROFILE="$TUTORIAL_AWS_PROFILE" AWS_REGION="$TUTORIAL_AWS_REGION" npx nx run @greengrass-tutorial/infra:deploy -- "greengrass-tutorial-infra-sandbox/**" --parameters TokenExchangeRoleArn="$TUTORIAL_TOKEN_EXCHANGE_ROLE_ARN" --parameters ThingGroupArn="$TUTORIAL_THING_GROUP_ARN"AWS_PROFILE="$TUTORIAL_AWS_PROFILE" AWS_REGION="$TUTORIAL_AWS_REGION" bunx nx run @greengrass-tutorial/infra:deploy -- "greengrass-tutorial-infra-sandbox/**" --parameters TokenExchangeRoleArn="$TUTORIAL_TOKEN_EXCHANGE_ROLE_ARN" --parameters ThingGroupArn="$TUTORIAL_THING_GROUP_ARN"コンポーネントがデバイスから消え、デバイスが正常な状態を維持するまで待ちます。
aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" greengrassv2 list-installed-components \ --core-device-thing-name "$TUTORIAL_THING_NAME" \ --query 'installedComponents[].componentName' --output textaws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" greengrassv2 get-core-device \ --core-device-thing-name "$TUTORIAL_THING_NAME" --query status --output text最初のコマンドはcom.example.Pulseをリストしなくなる必要があります。2番目のコマンドはHEALTHYを出力する必要があります。MQTTテストクライアントはハートビートの受信を停止します。
タスク2: スタックを破棄する
Section titled “タスク2: スタックを破棄する”AWS_PROFILE="$TUTORIAL_AWS_PROFILE" AWS_REGION="$TUTORIAL_AWS_REGION" pnpm nx run @greengrass-tutorial/infra:destroy -- "greengrass-tutorial-infra-sandbox/**" --forceAWS_PROFILE="$TUTORIAL_AWS_PROFILE" AWS_REGION="$TUTORIAL_AWS_REGION" yarn nx run @greengrass-tutorial/infra:destroy -- "greengrass-tutorial-infra-sandbox/**" --forceAWS_PROFILE="$TUTORIAL_AWS_PROFILE" AWS_REGION="$TUTORIAL_AWS_REGION" npx nx run @greengrass-tutorial/infra:destroy -- "greengrass-tutorial-infra-sandbox/**" --forceAWS_PROFILE="$TUTORIAL_AWS_PROFILE" AWS_REGION="$TUTORIAL_AWS_REGION" bunx nx run @greengrass-tutorial/infra:destroy -- "greengrass-tutorial-infra-sandbox/**" --forceスタックは削除されましたが、コンポーネントバージョンは不変であり、単一のデプロイメントよりも長く存続するため、3種類のリソースが意図的に保持されます。
com.example.Pulseの両方のコンポーネントバージョン、1.0.0と1.0.1。- モノグループへの3つのデプロイメントリビジョン。
- 公開されたバージョンごとに1つのアーカイブを含むアーティファクトバケット。
CDKブートストラップスタックCDKToolkitも残ります。これはアカウントとリージョン内のすべてのCDKアプリケーションで共有されるため、このチュートリアルのために作成した場合を除き、そのままにしておきます。
CDKは、コンポーネントバージョン解決、カスタムリソースフレームワーク、およびBucketDeploymentのLambdaログループをスタック削除後に残します。ログが不要になった場合は、個別に削除してください。
タスク3: 保持されたGreengrassリソースを削除する
Section titled “タスク3: 保持されたGreengrassリソースを削除する”モノグループへのデプロイメントをリストします。まだACTIVEなものをキャンセルしてから、それぞれを削除します。
aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" greengrassv2 list-deployments \ --target-arn "$TUTORIAL_THING_GROUP_ARN" --history-filter ALL \ --query 'deployments[].[deploymentId,deploymentStatus]' --output text
aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" greengrassv2 cancel-deployment \ --deployment-id <active-deployment-id>aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" greengrassv2 delete-deployment \ --deployment-id <deployment-id>コンポーネントの公開されたすべてのバージョンを削除します。
aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" greengrassv2 list-component-versions \ --arn "arn:aws:greengrass:${TUTORIAL_AWS_REGION}:${TUTORIAL_ACCOUNT_ID}:components:com.example.Pulse" \ --query 'componentVersions[].arn' --output text
aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" greengrassv2 delete-component \ --arn <component-version-arn>コンストラクトが適用する所有権タグを通じてアーティファクトバケットを見つけ、リストにこのチュートリアルのバケットのみが含まれていることを確認してから、そのオブジェクトバージョンとバケットを削除します。
aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" resourcegroupstaggingapi get-resources \ --tag-filters Key=greengrass-artifact-repository,Values=true \ --resource-type-filters s3:bucket \ --query 'ResourceTagMappingList[].ResourceARN' --output text
export TUTORIAL_BUCKET=<bucket-name>aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" s3api list-object-versions \ --bucket "$TUTORIAL_BUCKET" \ --query '{Objects: Versions[].{Key: Key, VersionId: VersionId}}' --output json \ > /tmp/greengrass-tutorial-versions.jsonaws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" s3api delete-objects \ --bucket "$TUTORIAL_BUCKET" --delete file:///tmp/greengrass-tutorial-versions.jsonaws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" s3api delete-bucket \ --bucket "$TUTORIAL_BUCKET"バケットはバージョン管理されているため、delete-objectsは各バージョンを指定する必要があります。list-object-versionsがDeleteMarkersも報告する場合は、バケットを削除する前にそれらのエントリで削除を繰り返します。
タスク4: コアデバイスとそのアイデンティティを削除する
Section titled “タスク4: コアデバイスとそのアイデンティティを削除する”スクリプトのcleanup操作は.greengrass-tutorial.envを読み取り、同じアカウント、リージョン、プレフィックスに属していることを確認してから、インスタンスを終了し、証明書にアタッチされたすべてのポリシーとともに証明書をデタッチして削除し、コアデバイスレコード、モノとモノグループ、両方のIoTポリシー、ロールエイリアス、トークン交換ロールとそのアタッチメント、およびアクセスポリシーを削除します。
./setup-greengrass-tutorial.sh cleanup \ --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" --prefix "$TUTORIAL_PREFIX"各ステップはdeletedまたはabsentを出力します。すでに削除されているオブジェクトはスキップされます。その他のエラーは実行を停止し、状態ファイルを保持するため、原因を修正して同じコマンドを再実行できます。
タスク5: インスタンスプロファイルを削除する
Section titled “タスク5: インスタンスプロファイルを削除する”インスタンスプロファイルはあなたのものだったため、スクリプトはそのままにしておきます。
aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" iam remove-role-from-instance-profile \ --instance-profile-name "${TUTORIAL_PREFIX}-instance" --role-name "${TUTORIAL_PREFIX}-instance"aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" iam delete-instance-profile \ --instance-profile-name "${TUTORIAL_PREFIX}-instance"aws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" iam detach-role-policy \ --role-name "${TUTORIAL_PREFIX}-instance" \ --policy-arn arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCoreaws --profile "$TUTORIAL_AWS_PROFILE" --region "$TUTORIAL_AWS_REGION" iam delete-role \ --role-name "${TUTORIAL_PREFIX}-instance"rm -f .greengrass-tutorial.env- 生成されたコンポーネントは、デフォルトでローカルIPCを介して公開します。クラウドMQTTには、
publish_to_iot_core、レシピ内のaws.greengrass.ipc.mqttproxy認可、およびデバイスのIoTポリシー内のiot:Publishが必要です。 - ジェネレーターはビルディングブロックを提供します。CDKスタックはアーティファクトリポジトリ、読み取り許可、デプロイメントを所有し、
addComponentは解決されたバージョンと順序エッジを運びます。 NEXT_PATCHはコンテンツからバージョンを割り当てます。ソースの変更により、通常のdeployを通じて次のパッチバージョンが公開されます。- コンポーネントの削除はデプロイメントリビジョンです。インフラストラクチャの削除は別の後続のステップであり、保持されたバージョン、デプロイメント、およびバケットを削除するために残します。
バージョン管理とアーティファクトの保持については、コンポーネントガイドのバージョン管理およびアーティファクトストレージセクションをお読みください。同じガイドには、より大規模なデプロイメントで考慮すべきサービス制限がリストされています。