Skip to content

Instantly share code, notes, and snippets.

@reubenmiller
Created January 25, 2023 13:41
Show Gist options
  • Save reubenmiller/f7510ab5a2551624798169b5f9425c36 to your computer and use it in GitHub Desktop.
Save reubenmiller/f7510ab5a2551624798169b5f9425c36 to your computer and use it in GitHub Desktop.
thin-edge.io bootstrap aws (custom example)
#!/bin/bash
##################################################################################################
# Install and bootstrap thin-edge.io to enable AWS communication
# A custom tedge and tedge-mapper version is used as the AWS functionality has not been
# merged into the main branch.
#
# Usage:
# sudo ./tedge-aws-bootstrap.sh
#
# Checkout the following links for more guidance on how to setup AWS and connect thin-edge.io to it
#
# Links:
# * Pull Request (PR): https://github.com/thin-edge/thin-edge.io/pull/1460
# * Documentation: https://github.com/thin-edge/thin-edge.io/blob/53b8933a440da8ffee7a191f05b0d4a5e45a81fa/docs/src/tutorials/connect-aws.md
# * Example AWS policy: https://github.com/thin-edge/thin-edge.io/blob/53b8933a440da8ffee7a191f05b0d4a5e45a81fa/docs/src/tutorials/aws-example-policy.json
##################################################################################################
# Defaults: Or set via env variables
AWS_URL=${AWS_URL:-example-ats.iot.us-east-1.amazonaws.com}
DEVICE_SN="${DEVICE_SN:-thin-edge-aws-demo01}"
set -e
# Helpers
info() { echo "INFO: $*"; }
error() { echo "ERROR: $*"; }
help() {
echo ""
echo "Usage:"
echo " $0 --url <aws_url> [--device-id <device_id>]"
echo ""
echo "Examples"
echo ""
echo "Example 1: Pass aws url and device id as arguments"
echo " sudo $0 --url a1vto0hq4ubn5m-ats.iot.us-east-1.amazonaws.com --device-id mydevice01"
echo ""
echo "Example 2: Use defaults (defined in the script)"
echo " sudo $0"
echo ""
}
#
# Arg parsing
#
POSITIONAL_ARGS=()
while [[ $# -gt 0 ]]; do
case $1 in
--url)
AWS_URL="$2"
shift
shift
;;
--device-id)
DEVICE_SN="$2"
shift
shift
;;
--help)
help
exit 0
;;
--*|-*)
echo "Unknown flag $1"
exit 1
;;
*)
POSITIONAL_ARGS+=("$1")
shift
;;
esac
done
set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters
if [ $# -gt 1 ]; then
error "Unexpected positional arguments"
help
exit 1
fi
if [ -z "$AWS_URL" ]; then
error "aws.url is not set"
help
exit 3
fi
if [ -z "$DEVICE_SN" ]; then
error "device id is not set"
help
exit 4
fi
check_aws_url() {
if command -v ping &> /dev/null; then
info "Checking if aws.url is reachable using ping"
if ping -c 1 "$AWS_URL"; then
info "aws.url is reachable"
else
error "aws.url is not reachable! Check the above command output for more details."
echo "You may need to check if the DNS is configured correctly. Consider using 8.8.8.8 or 1.1.1.1 if you are still having problems"
echo "Or alternatively set the ip address for the host in /etc/hosts"
exit 2
fi
else
info "Skipping aws.url check as ping is not installed"
fi
}
install_tedge() {
if [ ! -f /etc/apt/keyrings/thin-edge.io.gpg ]; then
mkdir -p /etc/apt/keyrings && curl -fsSL https://thinedgeio.jfrog.io/artifactory/api/security/keypair/thin-edge/public | sudo gpg --dearmor -o /etc/apt/keyrings/thin-edge.io.gpg
fi
sudo sh -c "echo 'deb [signed-by=/etc/apt/keyrings/thin-edge.io.gpg] https://thinedgeio.jfrog.io/artifactory/debian-development tedge-main main' > /etc/apt/sources.list.d/tedge-main.list"
sudo sh -c "echo 'deb [signed-by=/etc/apt/keyrings/thin-edge.io.gpg] https://thinedgeio.jfrog.io/artifactory/debian-development experimental-1460 main' > /etc/apt/sources.list.d/tedge-experimental-1460.list"
sudo apt-get update
sudo apt-get install -y mosquitto
sudo apt-get install -y tedge tedge-mapper tedge-agent tedge-apt-plugin c8y-configuration-plugin c8y-log-plugin tedge-watchdog
echo "Installing tedge PR1460 (tedge and tedge-mapper) to enable AWS functionality"
TEDGE_VERSION=$(dpkg -s tedge | grep "^Version:" | cut -d' ' -f2)
TEDGE_MAPPER_VERSION=$(dpkg -s tedge-mapper | grep "^Version:" | cut -d' ' -f2)
EXPECTED_VERSION="0.7.5-13-g53b8933a"
if [[ "$TEDGE_VERSION" != "$EXPECTED_VERSION" ]]; then
sudo apt-get install -y --allow-downgrades tedge=$EXPECTED_VERSION
fi
if [[ "$TEDGE_MAPPER_VERSION" != "$EXPECTED_VERSION" ]]; then
sudo apt-get install -y --allow-downgrades tedge-mapper=$EXPECTED_VERSION
fi
}
configure_device() {
EXISTING_URL=$(tedge config get aws.url)
if [[ "$EXISTING_URL" != "$AWS_URL" ]]; then
sudo tedge config set aws.url "$AWS_URL"
else
echo "aws.url is already set to ${AWS_URL}"
fi
EXISTING_DEVICEID=$(tedge config get device.id)
if [[ "$EXISTING_DEVICEID" != "$DEVICE_SN" ]]; then
sudo tedge cert create --device-id "${DEVICE_SN}"
else
echo "device.id is already set to ${DEVICE_SN}"
fi
# Show device certificate so user can upload it to AWS
echo "-----------------------------------------------------------------------"
echo "Certificate"
echo ""
echo "DeviceID: $(tedge config get device.id)"
echo ""
sudo cat "$(tedge config get device.cert.path)"
echo "-----------------------------------------------------------------------"
# Don't prompt if not in interactive mode
if [ -t 0 ] ; then
echo ""
echo "1. Open the AWS IoT Console in your web browser"
echo "2. Navigate to Manage > All devices > Things"
echo "3. Click Create things"
echo "4. Click Create single thing"
echo "5. Enter '$DEVICE_SN' as the thing name, then click Next"
echo "6. Select 'Use my certificate' and 'CA is not registered with AWS IoT'"
echo "7. Copy the certificate (shown above) to a local file, then upload it to AWS IoT Console"
echo ""
echo ""
echo -n "Press <ENTER> to continue/connect"
read -r
fi
}
connect_aws() {
if tedge connect aws --test &> /dev/null; then
sudo tedge disconnect aws
fi
sudo tedge connect aws
}
publish_messages() {
#
# Examples how to publish to AWS (either via thin-edge or directly to AWS topic)
#
tedge mqtt pub tedge/measurements '{"temperature": 20}'
tedge mqtt pub tedge/measurements '{ "current": {"L1": 9.5, "L2": 1.3} }'
tedge mqtt pub aws/messages/testme '{ "custom": "Some custom data", "isAlarm": false}'
}
main() {
install_tedge
check_aws_url
configure_device
connect_aws
publish_messages
}
#
# Entrypoint
#
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment