GithubHelp home page GithubHelp logo

kyopark2014 / iot-greengrass-with-ipc-client-v2 Goto Github PK

View Code? Open in Web Editor NEW
11.0 3.0 0.0 153 KB

It shows how to deploy ipc client v2 in IoT Greengrass using AWS CDK.

JavaScript 10.38% TypeScript 58.73% Python 28.01% Shell 2.87%
cdk greengrassv2 iot ipc pubsub

iot-greengrass-with-ipc-client-v2's Introduction

CDK로 IPC Client V2를 Greengrass에 Component로 배포하기

오픈 소스인 IoT Greengrass를 이용하여 디바이스에 edge runtime을 구성하고, AWS Greengrass에서 component를 배포할 수 있습니다.

  • AWS CDK는 대표적인 IaC (Infrastructure as Code) 툴(Tool)로서 Greengrass의 component를 code로 설치하고 관리할 수 있습니다.

  • Use the AWS IoT Device SDK to communicate에 소개된 IPC Client V2는 기존 IPC Client V1에 비하여 callback으로 handler를 구현할 수 있고, 동기(synchronous) 방식도 제공할 수 있습니다.

여기에서는 IPC Client V2에 기반한 component들을 CDK로 정의하고 배포하는 방법에 대해 설명합니다. 이를 통해, Greengrass 디바이스에 설치된 Components 또는 IoT Core 사이에서 PUBSUB으로 메시지를 교환할 수 있습니다.

전체적인 구조는 아래와 같습니다. AWS IoT Device management는 S3에 저장되어 있는 Recipe, Artifact를 이용하여 IoT Greengrass에 Local Component들을 배포할 수 있습니다. 각 component는 서로 IPC 통신방식으로 메시지를 교환합니다. 또한, AWS CDK를 이용해 S3에 Recipe, Artifact를 복사하고, Device Management를 통해 배포할 수 있습니다.

image

Greengrass 통신 방법

Greengrass V2부터는 Nucleus component의 PubSub service, IotMqttClient service를 이용하여 component와 IoT Core들이 PUBSUB 메시지 방식으로 통신을 할 수 있습니다. Greengrass Communication에서는 compnent들 간의 통신과 compoennt와 IoT Core간의 통신 방법에 대해 설명하고 있습니다.

Greengrass 디바이스에 설치된 component들은 IPC 통신을 이용하여 PubSub service에 메시지를 보내는 방식으로 통신합니다. 이때 receipe의 ComponentConfiguration는 "aws.greengrass.ipc.pubsub"을 이용합니다.

Greengrass의 Component는 IPC 통신으로 Neucleus에 메시지를 보내면, IoT Core로 메시지를 보낼 수 있습니다. recipe의 ComponentConfiguration에서 "aws.greengrass.ipc.mqttproxy"을 설정합니다.

Greengrass 설치

greengrass-installation에 따라 greengrass 디바이스에 greengrass를 설치하고 core device로 등록합니다.

Local에서 component 설치 및 시험

Local에서 IPC Client들 설치후 테스트에서는 greengrass 디바이스에 접속해서 local에서 com.example.publisher와 com.example.subscriber를 설치하고 정상적으로 동작하는것을 로그로 확인하는 방법을 설명합니다.

CDK로 component 설치 및 시험

CDK를 이용하여 Greengrass에 IPC Client(V2) 설치에서는 CDK에서 각 compoent를 구성하고 배포하는 방식에 대해 설명하고 있습니다.

CDK 준비 및 설치

cdk를 신규로 생성하는 경우

아래와 같이 적절한 폴더에 cdk-iot-client를 생성하고 cdk 초기화를 수행합니다.

mkdir cdk-iot-client && cd cdk-iot-client
cdk init app --language typescript

이후 cdk-ipc-client-stack.ts의 body 부분을 복사한후 저장합니다.

아래와 같이 설치합니다.

cdk deploy --all

github 코드를 이용하는 경우

cdk 폴더로 이동하여 아래와 같이 설치 합니다.

git clone https://github.com/kyopark2014/iot-greengrass-with-ipc-client-v2
cd cdk-ipc-client
cdk deploy --all

실행결과 확인

CDK로 배포가 잘되면, Component Console에서 "com.example.publisher"와 "com.example.subscriber"가 설치된것을 확인할 수 있습니다.

image

또한, Deployment Console에서 "deployment-local-pubsub" deployment가 아래와 같이 설치됩니다.

image

실행중인 Component 확인

Greengrass 디바이스 접속하여 아래와 같이 동작을 확인합니다.

실행상태를 확인합니다.

sudo /greengrass/v2/bin/greengrass-cli component list

아래와 깉이 com.example.publisher와 com.example.subscriber가 정상적으로 설치되고 실행중임을 알 수 있습니다.

Nov 13, 2022 8:54:18 AM software.amazon.awssdk.eventstreamrpc.EventStreamRPCConnection$1 onConnectionSetup
INFO: Socket connection /greengrass/v2/ipc.socket:8033 to server result [AWS_ERROR_SUCCESS]
Nov 13, 2022 8:54:18 AM software.amazon.awssdk.eventstreamrpc.EventStreamRPCConnection$1 onProtocolMessage
INFO: Connection established with event stream RPC server
Components currently running in Greengrass:
Component Name: com.example.publisher
    Version: 1.0.0
    State: RUNNING
    Configuration: {"accessControl":{"aws.greengrass.ipc.pubsub":{"com.example.publisher:pubsub:1":{"operations":["aws.greengrass#PublishToTopic"],"policyDescription":"Allows access to publish to all topics.","resources":["*"]}}}}
Component Name: com.example.subscriber
    Version: 1.0.0
    State: RUNNING
    Configuration: {"accessControl":{"aws.greengrass.ipc.pubsub":{"com.example.subscriber:pubsub:1":{"operations":["aws.greengrass#SubscribeToTopic"],"policyDescription":"Allows access to subscribe to all topics.","resources":["*"]}}}}

메시지 수신 확인

아래와 같이 메시지가 수신되는것을 로그로 확인할 수 있습니다.

sudo cat /greengrass/v2/logs/com.example.subscriber.log

이때의 결과는 아래와 같습니다.

2022-11-13T08:54:19.679Z [INFO] (Copier) com.example.subscriber: stdout. Received new message on topic local/topic: hello. {scriptName=services.com.example.subscriber.lifecycle.Run, serviceName=com.example.subscriber, currentState=RUNNING}
2022-11-13T08:54:24.686Z [INFO] (Copier) com.example.subscriber: stdout. Received new message on topic local/topic: hello. {scriptName=services.com.example.subscriber.lifecycle.Run, serviceName=com.example.subscriber, currentState=RUNNING}
2022-11-13T08:54:29.692Z [INFO] (Copier) com.example.subscriber: stdout. Received new message on topic local/topic: hello. {scriptName=services.com.example.subscriber.lifecycle.Run, serviceName=com.example.subscriber, currentState=RUNNING}
2022-11-13T08:54:34.697Z [INFO] (Copier) com.example.subscriber: stdout. Received new message on topic local/topic: hello. {scriptName=services.com.example.subscriber.lifecycle.Run, serviceName=com.example.subscriber, currentState=RUNNING}

Component 삭제

CDK로 인프라를 삭제할때에는 "cdk destory"로 진행할 수 있으나, Greengrass은 디바이스에 설치되므로, cdk-ipc-client-stack.ts의 greengrassv2.CfnDeployment에서 아래의 "com.example.publisher"와 "com.example.subscriber"을 삭제한 후에 재배포하거나 console의 deployment에서 삭제하여야 합니다.

// deployments
const cfnDeployment = new greengrassv2.CfnDeployment(this, 'MyCfnDeployment', {
  targetArn: `arn:aws:iot:ap-northeast-2:`+accountId+`:thing/`+deviceName,    
  components: {
    "com.example.publisher": {
      componentVersion: version, 
    },
    "com.example.subscriber": {
      componentVersion: version, 
    },
    "aws.greengrass.Cli": {
      componentVersion: "2.8.1", 
    }
  },
  deploymentName: 'deployment-local-pubsub',
  deploymentPolicies: {
    componentUpdatePolicy: {
      action: 'NOTIFY_COMPONENTS', // NOTIFY_COMPONENTS | SKIP_NOTIFY_COMPONENTS
      timeoutInSeconds: 60,
    },
    failureHandlingPolicy: 'ROLLBACK',  // ROLLBACK | DO_NOTHING
  },
});

이후, AWS Greengrass의 Component 정보들을 삭제하기 위해 아래와 같은 명령어를 사용합니다.

cdk destroy --all

Greengrass Commands와 Memo

유용한 Greengrass 명령어와 중요한 메모들를 정리하였습니다.

Reference

Use the AWS IoT Device SDK to communicate with the Greengrass nucleus, other components, and AWS IoT Core

iot-greengrass-with-ipc-client-v2's People

Contributors

kyopark2014 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.