3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

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

Last updated at Posted at 2020-03-12

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

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

準備

Cloud9の環境を作成し、以下をEnvironmentディレクトリ下に置きます。

setup.sh
mkdir $THING_NAME
cd $THING_NAME
POLICY_NAME=${THING_NAME}_Policy

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

git clone https://github.com/aws/aws-iot-device-sdk-python-v2.git
pip install ./aws-iot-device-sdk-python-v2
cd aws-iot-device-sdk-python-v2/samples/

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": ["*"]
  }]
}

実行


python3 -m pip install awsiotsdk

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

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

3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?