Script to easily create a client device environment for AWS IoT (Python v2 version)

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.

Preparation

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

Run


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

Script to easily create a client device environment for AWS IoT (Python v2 version)
I wrote a script to create a Twitter Bot development environment quickly with AWS Lambda + Python 2.7
Let's create a virtual environment for Python
How to create a Python virtual environment (venv)
Python mock to try AWS IoT Device Shadow
I want to create a nice Python development environment for my new Mac
Create a Python environment
Fleet provisioning with AWS IoT SDK for Python v2
Create a Layer for AWS Lambda Python with Docker
How to create a simple TCP server / client script
Python script to create a JSON file from a CSV file
[AWS] Development environment version that tried to build a Python environment with eb [Elastic Beanstalk]
How to use NUITKA-Utilities hinted-compilation to easily create an executable file from a Python script
Run AWS IoT Device SDK for Python on Raspberry Pi
How to launch AWS Batch from a python client app
Create a Python script for Wake on LAN (NAT traversal Wake on LAN [5])
How to create a SAS token for Azure IoT Hub
Create a virtual environment for python on mac [Very easy]
Create a Python environment for professionals in VS Code on Windows
Create a Python execution environment for Windows with VScode + Remote WSL
[AWS] Flask application deployment version that tried to build a Python environment with eb [Elastic Beanstalk]
Create a Python environment on Mac (2017/4)
Create a shell script to run the python file multiple times
Create a virtual environment with Python!
Try to create a python environment with Visual Studio Code & WSL
From building a Python environment for inexperienced people to Hello world
Create a python environment on centos
AWS Layer Creation Script for python
Python vba to create a date string for creating a file name
5 Ways to Create a Python Chatbot
[For Python] Quickly create an upload file to AWS Lambda Layer
[AWS SAM] Introduction to Python version
Create a USB boot Ubuntu with a Python environment for data analysis
Building a virtual environment for Mayavi dedicated to Python 3.6, Anaconda, Spyder users
Things to watch out for when creating a Python environment on a Mac
I easily built an operating environment for Python3 + Tornado on AWS EC2.
[AWS] Create a Python Lambda environment with CodeStar and do Hello World
Create a Python development environment locally at the fastest speed (for beginners)
Build a version control environment for Python, Ruby, Perl, Node.js on UNIX
Steps to create a Python virtual environment with VS Code on Windows
[Python / Tkinter] Search for Pandas DataFrame → Create a simple search form to display
Try to set up a Vim test environment quite seriously (for Python)
A guidebook for doing IoT with MicroPython easily to the last minute
[Python] Create a virtual environment with Anaconda
[Python] Create a Batch environment using AWS-CDK
I want to build a Python environment
[Mac] Building a virtual environment for Python
A tool for easily entering Python code
Script to create a Mac dictionary file
Building a Python development environment for AI development
Add a Python virtual environment to VSCode
How to run a Maya Python script
I tried to create a class that can easily serialize Json in Python
I tried to explain what a Python generator is for as easily as possible.
When I tried to create a virtual environment with Python, it didn't work
I tried to easily create a fully automatic attendance system with Selenium + Python
Python script to get a list of input examples for the AtCoder contest
Create a setting in terraform to send a message from AWS Lambda Python3.8 to Slack
[Python] How to create a local web server environment with SimpleHTTPServer and CGIHTTPServer
[Python] List Comprehension Various ways to create a list
Edit Excel from Python to create a PivotTable