Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 9235

AWS IoT のクライアントデバイス環境を簡単に作るスクリプト(Node.js v2 版)

$
0
0

ちょこっとテストするための、Node.js版のAWS IoT Device SDK v2のPubSubをすぐに試すスクリプトです。CLI上で数行でできます。
Python版はこちらだいたい同じです

Cloud9上での設定を想定しています。マネコンで作成するのが手間な場合に使います。
何をやっているか等は説明しません。。
あと、手動で何発かメッセージを送るだけであれば、IoT Coreのテスト機能を使うのがよいです。

準備

Cloud9 の環境にはUbuntuを選びます。Amazon Linuxだと、GLIBC_2.25が無いというエラーが出ております。
Cloud9の環境を作成し、以下をEnvironmentディレクトリ下に置きます。

setup-node-v2.sh
mkdir$THING_NAMEcd$THING_NAMEPOLICY_NAME=${THING_NAME}_Policy

aws iot create-thing --thing-name${THING_NAME}

git clone https://github.com/aws/aws-iot-device-sdk-js-v2.git
cd aws-iot-device-sdk-js-v2/samples/node/pub_sub/
npm install

wget -O rootca.pem \
    https://www.amazontrust.com/repository/AmazonRootCA1.pem

aws iot create-keys-and-certificate --set-as-active\--certificate-pem-outfile    certificate.pem \--public-key-outfile         public_key.pem  \--private-key-outfile        private_key.pem \--query certificateArn

echo-n CERTIFICATE_ARN: 
read str 
CERTIFICATE_ARN=$str

aws iot create-policy                     \--policy-name${POLICY_NAME}\--policy-document file://../../../../../policy.json

aws iot attach-thing-principal             \--thing-name$THING_NAME\--principal$CERTIFICATE_ARN

aws iot attach-principal-policy            \--policy-name$POLICY_NAME\--principal$CERTIFICATE_ARN

ポリシーは適宜修正すべきですが、一旦なんでも有りで。

policy.json
{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":["iot:*"],"Resource":["*"]}]}

実行

npm install aws-iot-device-sdk-v2

aws iot describe-endpoint --endpoint-type iot:Data-ATS 
export ENDPOINT=yourendpoint-ats.iot.ap-northeast-1.amazonaws.com
export THING_NAME=mything
./setup-node-v2.sh
cd$THING_NAME/aws-iot-device-sdk-js-v2/samples/node/pub_sub
node dist/index.js --endpoint$ENDPOINT--root-ca rootca.pem --cert certificate.pem --key private_key.pem

別のThingで試すときは、THING_NAMEを書き換えて実行します。


Viewing all articles
Browse latest Browse all 9235

Trending Articles