AWS CLIでGreengrassのコンポーネントを更新してデプロイしたい
こちらの記事で作成したコンポーネントとデプロイの設定をAWS CLIから実行してみました。
参考:https://docs.aws.amazon.com/cli/latest/reference/greengrassv2/index.html
やったことのまとめ
- コンソールで作ったコンポーネントとデプロイ設定のレシピを取得してjsonファイル化する
- AWS CLIでJSONファイルを指定してコンポーネントの更新、デプロイ設定を実行する
- コンポーネントの更新とデプロイ設定をシェルスクリプト化する
前提
- コンポーネントとデプロイ設定を作成済みであること
- 本記事の対象は更新作業です
コンポーネントのレシピの確認
作成済みのコンポーネントのレシピは、コンポーネントの詳細画面の「レシピを表示」から確認できます。
確認したレシピをファイル(component_recipe.json)に書き出します。
ComponentVersion
は作成済みのバージョンと異なるように1.0.1
としておきます。
component_recipe.json
{
"RecipeFormatVersion": "2020-01-25",
"ComponentName": "com.example.MyPrivateDockerComponent",
"ComponentVersion": "1.0.1",
"ComponentType": "aws.greengrass.generic",
"ComponentDescription": "A component that runs a Docker container from a private Amazon ECR image.",
"ComponentPublisher": "Amazon",
"ComponentDependencies": {
"aws.greengrass.DockerApplicationManager": {
"VersionRequirement": ">=2.0.0 <2.1.0",
"DependencyType": "HARD"
},
"aws.greengrass.TokenExchangeService": {
"VersionRequirement": ">=2.0.0 <2.1.0",
"DependencyType": "HARD"
}
},
"Manifests": [
{
"Platform": {
"os": "all"
},
"Lifecycle": {
"Run": "docker run -p 80:80 {accountId}.dkr.ecr.ap-northeast-1.amazonaws.com/greengrass-test:latest"
},
"Artifacts": [
{
"Uri": "docker:{accountId}.dkr.ecr.ap-northeast-1.amazonaws.com/greengrass-test:latest",
"Unarchive": "NONE",
"Permission": {
"Read": "OWNER",
"Execute": "NONE"
}
}
]
}
],
"Lifecycle": {}
}
コンポーネントの更新
現在のコンポーネントを確認しておきます。
% aws greengrassv2 list-components
{
"components": [
{
"arn": "arn:aws:greengrass:ap-northeast-1:{accountId}:components:com.example.MyPrivateDockerComponent",
"componentName": "com.example.MyPrivateDockerComponent",
"latestVersion": {
"arn": "arn:aws:greengrass:ap-northeast-1:{accountId}:components:com.example.MyPrivateDockerComponent:versions:1.0.0",
"componentVersion": "1.0.0",
"creationTimestamp": "2022-09-12T23:21:01.973000+09:00",
"description": "A component that runs a Docker container from a private Amazon ECR image.",
"publisher": "Amazon",
"platforms": [
{
"attributes": {
"os": "all"
}
}
]
}
}
]
}
com.example.MyPrivateDockerComponent
のコンポーネントが作成されており、componentVersion
は1.0.0
です。
コンポーネントの新しいバージョンを作成します。
% aws greengrassv2 create-component-version --inline-recipe fileb://component_recipe.json
{
"arn": "arn:aws:greengrass:ap-northeast-1:{accountId}:components:com.example.MyPrivateDockerComponent:versions:1.0.1",
"componentName": "com.example.MyPrivateDockerComponent",
"componentVersion": "1.0.1",
"creationTimestamp": "2022-09-12T23:22:04.366000+09:00",
"status": {
"componentState": "REQUESTED",
"message": "NONE",
"errors": {},
"vendorGuidance": "ACTIVE",
"vendorGuidanceMessage": "NONE"
}
}
再度、コンポーネントを確認します。
% aws greengrassv2 list-components
{
"components": [
{
"arn": "arn:aws:greengrass:ap-northeast-1:{accountId}:components:com.example.MyPrivateDockerComponent",
"componentName": "com.example.MyPrivateDockerComponent",
"latestVersion": {
"arn": "arn:aws:greengrass:ap-northeast-1:{accountId}:components:com.example.MyPrivateDockerComponent:versions:1.0.1",
"componentVersion": "1.0.1",
"creationTimestamp": "2022-09-12T23:22:04.366000+09:00",
"description": "A component that runs a Docker container from a private Amazon ECR image.",
"publisher": "Amazon",
"platforms": [
{
"attributes": {
"os": "all"
}
}
]
}
}
]
}
latestVersion.componentVersion
が 1.0.1
に変わっています。更新出来ました。
デプロイ設定のレシピの確認
デプロイ設定も同様に手動で作成したレシピを確認します。
コンソールからの確認方法が分からなかったので、CLIでレシピを確認しました。
デプロイ一覧を取得します。
% aws greengrassv2 list-deployments
{
"deployments": [
{
"targetArn": "arn:aws:iot:ap-northeast-1:{accountId}:thinggroup/group-001",
"revisionId": "1",
"deploymentId": "{デプロイID}",
"deploymentName": "DeploymentGroup01",
"creationTimestamp": "2022-09-12T23:37:33.018000+09:00",
"deploymentStatus": "ACTIVE",
"isLatestForTarget": true
}
]
}
deployment-id
を指定してデプロイ設定を取得
% aws greengrassv2 get-deployment --deployment-id {デプロイID}
{
"targetArn": "arn:aws:iot:ap-northeast-1:{accountId}:thinggroup/group-001",
"revisionId": "1",
"deploymentId": "{デプロイID}",
"deploymentName": "DeploymentGroup01",
"deploymentStatus": "ACTIVE",
"iotJobId": "{IoTジョブID}",
"iotJobArn": "arn:aws:iot:ap-northeast-1:{accountId}:job/{IoTジョブID}",
"components": {
"aws.greengrass.Cli": {
"componentVersion": "2.7.0"
},
"com.example.MyPrivateDockerComponent": {
"componentVersion": "1.0.0"
}
},
"deploymentPolicies": {
"failureHandlingPolicy": "DO_NOTHING",
"componentUpdatePolicy": {
"timeoutInSeconds": 60,
"action": "NOTIFY_COMPONENTS"
},
"configurationValidationPolicy": {
"timeoutInSeconds": 60
}
},
"iotJobConfiguration": {},
"creationTimestamp": "2022-09-12T23:37:33.018000+09:00",
"isLatestForTarget": true,
"tags": {}
}
デプロイ
↑でレシピっぽいものが取得できたので、カスタムコンポーネントのcomponentVersion
を1.0.1
に変更して、ファイル(deployment_recipe.json)に書き出し、create-deployment
を実行したところ、怒られました。
% aws greengrassv2 create-deployment --cli-input-json fileb://deployment_recipe.json
Parameter validation failed:
Unknown parameter in input: "revisionId", must be one of: targetArn, deploymentName, components, iotJobConfiguration, deploymentPolicies, tags, clientToken
Unknown parameter in input: "deploymentId", must be one of: targetArn, deploymentName, components, iotJobConfiguration, deploymentPolicies, tags, clientToken
Unknown parameter in input: "deploymentStatus", must be one of: targetArn, deploymentName, components, iotJobConfiguration, deploymentPolicies, tags, clientToken
Unknown parameter in input: "iotJobId", must be one of: targetArn, deploymentName, components, iotJobConfiguration, deploymentPolicies, tags, clientToken
Unknown parameter in input: "iotJobArn", must be one of: targetArn, deploymentName, components, iotJobConfiguration, deploymentPolicies, tags, clientToken
Unknown parameter in input: "creationTimestamp", must be one of: targetArn, deploymentName, components, iotJobConfiguration, deploymentPolicies, tags, clientToken
Unknown parameter in input: "isLatestForTarget", must be one of: targetArn, deploymentName, components, iotJobConfiguration, deploymentPolicies, tags, clientToken
Unknown parameter
と怒られたパラメーターと、tag: {}
を削除して再度作成したファイルによりcreate-deployment
を再実行することで無事にデプロイできました。
deployment_recipe.json
{
"targetArn": "arn:aws:iot:{region}:{accountId}:thinggroup/group-001",
"deploymentName": "DeploymentGroup01",
"components": {
"aws.greengrass.Cli": {
"componentVersion": "2.7.0"
},
"com.example.MyPrivateDockerComponent": {
"componentVersion": "1.0.1"
}
},
"deploymentPolicies": {
"failureHandlingPolicy": "DO_NOTHING",
"componentUpdatePolicy": {
"timeoutInSeconds": 60,
"action": "NOTIFY_COMPONENTS"
},
"configurationValidationPolicy": {
"timeoutInSeconds": 60
}
},
"iotJobConfiguration": {}
}
デプロイ実行
% aws greengrassv2 create-deployment --cli-input-json fileb://deployment_recipe.json
{
"deploymentId": "{デプロイID}",
"iotJobId": "{IoTジョブID}",
"iotJobArn": "arn:aws:iot:ap-northeast-1:{accountId}:job/{IoTジョブID}"
}
デプロイ自動化
↑で確認したCLIコマンドをシェルスクリプト化しただけです。
ディレクトリ構成
|- component
|- deployment
|- template
| |- component_recipe.json
| |- deployment_recipe.json
|- deployment.sh
component_recipe.json
{
"RecipeFormatVersion": "2020-01-25",
"ComponentName": "com.example.MyPrivateDockerComponent",
"ComponentVersion": "COMPONENT_VERSION",
"ComponentType": "aws.greengrass.generic",
"ComponentDescription": "A component that runs a Docker container from a private Amazon ECR image.",
"ComponentPublisher": "Amazon",
"ComponentDependencies": {
"aws.greengrass.DockerApplicationManager": {
"VersionRequirement": ">=2.0.0 <2.1.0",
"DependencyType": "HARD"
},
"aws.greengrass.TokenExchangeService": {
"VersionRequirement": ">=2.0.0 <2.1.0",
"DependencyType": "HARD"
}
},
"Manifests": [
{
"Platform": {
"os": "all"
},
"Lifecycle": {
"Run": "docker run -p 80:80 {accountId}.dkr.ecr.{region}.amazonaws.com/greengrass-test:latest"
},
"Artifacts": [
{
"Uri": "docker:{accountId}.dkr.ecr.{region}.amazonaws.com/greengrass-test:latest",
"Unarchive": "NONE",
"Permission": {
"Read": "OWNER",
"Execute": "NONE"
}
}
]
}
],
"Lifecycle": {}
}
deployment_recipe
{
"targetArn": "arn:aws:iot:{region}:{accountId}:thinggroup/group-001",
"deploymentName": "DeploymentGroup",
"components": {
"aws.greengrass.Cli": {
"componentVersion": "2.7.0"
},
"com.example.MyPrivateDockerComponent": {
"componentVersion": "COMPONENT_VERSION"
}
},
"deploymentPolicies": {
"failureHandlingPolicy": "DO_NOTHING",
"componentUpdatePolicy": {
"timeoutInSeconds": 60,
"action": "NOTIFY_COMPONENTS"
},
"configurationValidationPolicy": {
"timeoutInSeconds": 60
}
},
"iotJobConfiguration": {}
}
deployment.sh
#!/bin/bash
echo "input component version: "
read component_version
echo "create component_recipe"
cat ./template/component_recipe.json | sed -e "s/COMPONENT_VERSION/$component_version/g" > ./component/component_recipe_$component_version.json
echo "create deployment_recipe"
cat ./template/deployment_recipe.json | sed -e "s/COMPONENT_VERSION/$component_version/g" > ./deployment/deployment_recipe_$component_version.json
echo "create component"
aws greengrassv2 create-component-version --inline-recipe fileb://component/component_recipe_$component_version.json
echo "create deployment"
aws greengrassv2 create-deployment --cli-input-json fileb://deployment/deployment_recipe_$component_version.json
実行権限をつけて./deployment.sh
します。
% ./deployment.sh
input component version:
1.0.2
create component_recipe
create deployment_recipe
create component
{
"arn": "arn:aws:greengrass:ap-northeast-1:{accountId}:components:com.example.MyPrivateDockerComponent:versions:1.0.2",
"componentName": "com.example.MyPrivateDockerComponent",
"componentVersion": "1.0.2",
"creationTimestamp": "2022-09-13T00:13:50.908000+09:00",
"status": {
"componentState": "REQUESTED",
"message": "NONE",
"errors": {},
"vendorGuidance": "ACTIVE",
"vendorGuidanceMessage": "NONE"
}
}
create deployment
{
"deploymentId": "{デプロイID}",
"iotJobId": "{IoTジョブID}",
"iotJobArn": "arn:aws:iot:ap-northeast-1:{accountId}:job/{IoTジョブID}"
}
コンポーネントを更新してデプロイできました。
やったことのまとめ
- 手動でコンポーネントとデプロイ設定を作る
- 作ったコンポーネントとデプロイ設定のレシピを取得してjsonファイル化する
- AWS CLIでJSONファイルを指定してコンポーネントの更新、デプロイ設定を実行する
- コンポーネントの更新とデプロイ設定をシェルスクリプト化する
以上です。
Discussion