Created
October 1, 2017 22:01
-
-
Save reisjr/a1a6bfdc33f870ab640fb4b4f7e46c60 to your computer and use it in GitHub Desktop.
AWS IoT / Snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
SCRIPT=`basename "$0"` | |
if [ $# -eq 0 ] | |
then | |
echo "No arguments supplied" | |
echo "$SCRIPT thing-name" | |
exit | |
fi | |
THING_NAME=$1 | |
echo "Creating thing $THING_NAME ..." | |
THING_ARN=`aws iot create-thing \ | |
--thing-name $THING_NAME \ | |
--query "thingArn"` | |
#--thing-type-name <value> \ | |
echo "Creating cert and key for $THING_NAME ..." | |
CERT_ARN=`aws iot create-keys-and-certificate \ | |
--certificate-pem-outfile $THING_NAME-cert.pem \ | |
--public-key-outfile $THING_NAME-public-key.pem \ | |
--private-key-outfile $THING_NAME-private-key.pem \ | |
--set-as-active \ | |
--query "certificateArn" | tr -d '"'` | |
#--query "[certificateArn, certificateId]" | |
echo "Downloading root cert..." | |
wget "https://www.symantec.com/content/en/us/enterprise/verisign/roots/VeriSign-Class%203-Public-Primary-Certification-Authority-G5.pem" -O rootCA.pem | |
echo "Creating a new policy..." | |
POLICY_ARN=`aws iot create-policy \ | |
--policy-name "PermissivePolicyFor-$THING_NAME" \ | |
--policy-document '{"Version":"2012-10-17","Statement":[{"Action":["iot:Publish","iot:Subscribe","iot:Connect","iot:Receive"],"Effect":"Allow","Resource":["*"]}]}' \ | |
--query "policyArn"` | |
echo "Attaching certificate to your thing..." | |
aws iot attach-thing-principal \ | |
--thing-name $THING_NAME \ | |
--principal $CERT_ARN | |
echo "Attaching policy to your thing..." | |
aws iot attach-principal-policy \ | |
--policy-name "PermissivePolicyFor-$THING_NAME" \ | |
--principal $CERT_ARN | |
echo "Checking AWS IoT endpoint..." | |
ENDPOINT=`aws iot describe-endpoint --query "endpointAddress"` | |
echo "DONE!" | |
echo -e "\n#########\n" | |
echo " Your Thing NAME : $THING_NAME" | |
echo " Your Thing ARN : $THING_ARN" | |
echo " Your Policy ARN : $POLICY_ARN" | |
echo " Your Cert ARN : $CERT_ARN" | |
echo " Cert file : $THING_NAME-cert.pem" | |
echo " Private key file : $THING_NAME-private-key.pem" | |
echo " Root Cert file : rootCA.pem" | |
echo "Your AWS IoT endpoint : $ENDPOINT" | |
echo -e "\n#########\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment