A quick-try script for the Python version of AWS IoT Device SDK v2 PubSub for a quick test. You can do it in a few lines on the CLI. Click here for Node.js version (https://qiita.com/tatsuhiroiida/items/8620ca1019ddf7a6f114)
It is assumed to be set on Cloud9. It is used when it is troublesome to create with a mannequin. I will not explain what you are doing. .. Also, if you just want to send a few messages manually, it's better to use the test function of IoT Core.
Create a Cloud9 environment and place the following under the Environment directory.
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
The policy should be modified accordingly, but once there is anything.
policy.json
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action":["iot:*"],
"Resource": ["*"]
}]
}
sudo 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 aws-iot-device-sdk-python-v2/samples/
python pubsub.py --endpoint $ENDPOINT --root-ca rootca.pem --cert certificate.pem --key private_key.pem
When trying with another thing, rewrite THING_NAME and execute.
Recommended Posts