LoginSignup
1
0

【AWS】IoT Greengrass v2備忘録

Last updated at Posted at 2024-02-29

 AWS IoT Greengrassとは、IoTエッジデバイスにアプリケーションをデプロイ・管理できるクラウドプラットフォームです。

今回は、Greengrass v2のハンズオンチュートリアルを参考に、実際にやってみる。

エッジデバイスへGreengrassのインストール

エッジデバイスへのGreengrassのインストールは非常に簡単で、コンソール画面を開いて、数コマンド打ち込むだけ。

  1. Grrengrass v2のコンソール画面を開く
  2. Greengrassデバイス -> 1つのCoreデバイスをセットアップを押す
    ※ここに非常に丁寧に手順が示されている。下記はその手順。
  3. JavaのJRE(Javaランタイム)をインストールする
    sudo apt-get update
    sudo apt-get upgrade
    sudo apt-get install default-jre 
    
  4. AWSアクセスキーの環境変数とインストールコマンドをデバイスで打ち込む
    export AWS_ACCESS_KEY_ID=<AWS_ACCESS_KEY_ID>
    export AWS_SECRET_ACCESS_KEY=<AWS_SECRET_ACCESS_KEY>
    export AWS_SESSION_TOKEN=<AWS_SESSION_TOKEN>
    
    # インストーラーのダウンロード
    curl -s https://d2s8p88vqu9w66.cloudfront.net/releases/greengrass-nucleus-latest.zip > greengrass-nucleus-latest.zip && unzip greengrass-nucleus-latest.zip -d GreengrassInstaller
    
    # インストーラーの実行
    sudo -E java -Droot="/greengrass/v2" -Dlog.store=FILE -jar ./GreengrassInstaller/lib/Greengrass.jar --aws-region ap-northeast-1 --thing-name GreengrassQuickStartCore-18df36c1764 --thing-group-name GreengrassQuickStartGroup --component-default-user ggc_user:ggc_group --provision true --setup-system-service true --deploy-dev-tools true
    
  5. 正常に実行されているか確認する
    sudo systemctl status greengrass.service
    sudo systemctl enable greengrass.service
    

これで、コンソール画面上から、コンポーネントをデバイスへアップロードできる

  • 詳細設定は、下記ファイルに記載する
sudo vim greengrass.service
sudo systemctl enable greengrass.service

安全なトンネル接続を設定する

デバイスのポートを開く

  1. Linuxのパーソナルファイアウォール ufwをインストールする
    sudo apt install apt-file
    sudo apt-file update
    sudo apt-file search apr-add-repository
    sudo apt install uwf
    
  2. ufwを有効化して、ポート443を開く
    sudo ufw enable
    sudo ufw allow 443
    
  3. 再起動する
    reboot
    
  4. 解放されているか確認する
    sudo ufw status
    

SSHデーモンをインストールする

  1. SSHデーモンをインストールする
    sudo apt-get install avahi-daemon
    
  2. SSHを有効化する
    sudo systemctl enable ssh
    

コンソール画面からSSH接続する

  1. Greengrass v2 コンソール画面を開く
  2. Greengrass デバイス -> コンポーネント - パブリックコンポーネントを開く
  3. aws.greengrass.SecureTunnelingをクリックする
  4. 右上のデプロイで、対象のデバイスにデプロイする
    ※正常にデプロイされ、実行されれば、コンポーネント画面のステータスで実行中になります。
    そうでなければ、再デプロイする
  5. すべてのデバイス -> モノ -> 対象の物理デバイスを選択する
  6. 右上の安全なトンネルを作成するをクリックする
  7. 新しいトンネルを作成を選択
  8. クイックセットアップ (SSH)を選択
  9. 次へを押す
  10. 確認して作成を押す
  11. リモートアクション -> 安全なトンネルをクリックする
  12. 新しいアクセストークンを生成を押す
    ※時間が経過するとアクセストークンが無効になり、接続ボタンが押せなくなります。
    再度、アクセストークンを生成すると、接続可能になります。
    13.接続を押して、接続先デバイスのユーザー名とパスワードを入力する

コンポーネントの作成

コンポーネントはレシピとアーチファクトからなり、下記のフォルダ構成で開発する。

GreengrassCore/
├ artifacts/
│         └com.example.HelloWorld/
│                                └1.0.0/hello_world.py
│
└ recipes/com.example.HelloWorld-1.0.0.json
  1. まずは、上記フォルダ構成を作成するコマンドを叩く
    mkdir -p ~/environment/GreengrassCore/artifacts/com.example.HelloWorld/1.0.0 && touch ~/environment/GreengrassCore/artifacts/com.example.HelloWorld/1.0.0/hello_world.py
    mkdir -p ~/environment/GreengrassCore/recipes && touch ~/environment/GreengrassCore/recipes/com.example.HelloWorld-1.0.0.json
    
  2. アーチファクトファイルであるhello_world.pyに以下をコピペする
    hello_world.py
    # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
    # SPDX-License-Identifier: MIT-0
    import sys
    import datetime
    import time
    
    while True:
    
        message = f"Hello, {sys.argv[1]}! Current time: {str(datetime.datetime.now())}."
    
        # Print the message to stdout.
        print(message)
    
        # Append the message to the log file.
        with open('/tmp/Greengrass_HelloWorld.log', 'a') as f:
             print(message, file=f)
    
        time.sleep(1)
    
  3. レシピであるcom.example.HelloWorld-1.0.0.jsonに以下をコピペする
    com.example.HelloWorld-1.0.0.json
    {
      "RecipeFormatVersion": "2020-01-25",
      "ComponentName": "com.example.HelloWorld",
      "ComponentVersion": "1.0.0",
      "ComponentDescription": "My first AWS IoT Greengrass component.",
      "ComponentPublisher": "Amazon",
      "ComponentConfiguration": {
        "DefaultConfiguration": {
          "Message": "world"
        }
      },
      "Manifests": [
        {
          "Platform": {
            "os": "linux"
          },
          "Lifecycle": {
            "Run": "python3 -u {artifacts:path}/hello_world.py '{configuration:/Message}'\n"
          }
        }
      ]
    }
    
  4. 下記コマンドでコンポーネントをデプロイする
    sudo /greengrass/v2/bin/greengrass-cli deployment create \
       --recipeDir ~/environment/GreengrassCore/recipes \
       --artifactDir ~/environment/GreengrassCore/artifacts \
       --merge "com.example.HelloWorld=1.0.0"
    
    Local deployment submitted! Deployment Id: *****fc1-8503-401a-817b-**************
    
    ログファイルに1行ごとに書きこみがされていれば、成功している
    tail -F /tmp/Greengrass_HelloWorld.log
    
    
    Hello, world! Current time: 2024-02-29 14:38:51.461737.
    Hello, world! Current time: 2024-02-29 14:38:52.466139.
    Hello, world! Current time: 2024-02-29 14:38:53.470553.
    Hello, world! Current time: 2024-02-29 14:38:54.475245.
    Hello, world! Current time: 2024-02-29 14:38:55.479950.
    Hello, world! Current time: 2024-02-29 14:38:56.484553.
    

作成したカスタムコンポーネントをGreengrassへ登録する

  1. aws認証情報を設定する
    aws configure
    
  2. S3バケットを作成する
    EPOCH_TIME=$(date +"%s") && S3_BUCKET=ggcv2-workshop-$HOSTNAME-$EPOCH_TIME && aws s3 mb s3://$S3_BUCKET --region ap-northeast-1
    
  3. レシピを変更する
    {
      "RecipeFormatVersion": "2020-01-25",
      "ComponentName": "com.example.HelloWorld",
      "ComponentVersion": "1.0.0",
      "ComponentDescription": "My first AWS IoT Greengrass component.",
      "ComponentPublisher": "Amazon",
      "ComponentConfiguration": {
        "DefaultConfiguration": {
          "Message": "world"
        }
      },
      "Manifests": [
        {
          "Platform": {
            "os": "linux"
          },
          "Lifecycle": {
            "Run": "python3 -u {artifacts:path}/hello_world.py '{configuration:/Message}'\n"
          },
          "Artifacts": [
            {
              "URI": "s3://[YOUR BUCKET NAME]/artifacts/com.example.HelloWorld/1.0.0/hello_world.py"
            }
          ]
        }
      ]
    }
    
  4. S3へアーティファクトファイルをアップロードする
    aws s3 cp --recursive /home/ubuntu/environment/GreengrassCore/ s3://$S3_BUCKET/
    
  5. カスタムコンポーネントをGreengrassへ登録する
    cd /home/ubuntu/environment/GreengrassCore/recipes && aws greengrassv2 create-component-version  --inline-recipe fileb://com.example.HelloWorld-1.0.0.json --region $AWS_DEFAULT_REGION
    

まとめ

今回は、IoT Greengrass v2の使い方を紹介した。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0