{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Multi-Region IoT - Setup\n", "\n", "After you have launched the CloudFormation stacks in the master and slave region you will setup your environment:\n", "\n", "* Enable [IoT registry events](https://docs.aws.amazon.com/iot/latest/developerguide/registry-events.html) in the master region\n", "* Create a global DyanmoDB table which spans the master and slave region" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Libraries" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import boto3\n", "import json\n", "import time\n", "import urllib.request\n", "\n", "from os import makedirs\n", "from shutil import copy\n", "from time import time, gmtime, localtime, strftime" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Global Vars" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%store -r config\n", "\n", "print(\"config: {}\".format(json.dumps(config, indent=4, default=str)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Amazon Root CA\n", "\n", "Get the Amazon Root CA which signed the certificate for IoT Core's MQTT message broker. The CA will be used when connecting a device to AWS IoT Core." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "r = urllib.request.urlopen('https://www.amazontrust.com/repository/AmazonRootCA1.pem')\n", "cert_pem = r.read().decode()\n", "print(cert_pem)\n", "\n", "f = open('AmazonRootCA1.pem','w')\n", "f.write(cert_pem)\n", "f.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Boto3 Clients\n", "Create iot boto3 client for the master region." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "c_iot_m = boto3.client('iot', region_name = config['aws_region_master'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Enable IoT registry events" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "response = c_iot_m.update_event_configurations(\n", " eventConfigurations={\n", " \"THING_TYPE\": {\n", " \"Enabled\": True\n", " }, \n", " \"JOB_EXECUTION\": {\n", " \"Enabled\": True\n", " }, \n", " \"THING_GROUP_HIERARCHY\": {\n", " \"Enabled\": True\n", " }, \n", " \"CERTIFICATE\": {\n", " \"Enabled\": True\n", " }, \n", " \"THING_TYPE_ASSOCIATION\": {\n", " \"Enabled\": True\n", " }, \n", " \"THING_GROUP_MEMBERSHIP\": {\n", " \"Enabled\": True\n", " }, \n", " \"CA_CERTIFICATE\": {\n", " \"Enabled\": True\n", " }, \n", " \"THING\": {\n", " \"Enabled\": True\n", " }, \n", " \"JOB\": {\n", " \"Enabled\": True\n", " }, \n", " \"POLICY\": {\n", " \"Enabled\": True\n", " }, \n", " \"THING_GROUP\": {\n", " \"Enabled\": True\n", " }\n", " }\n", ")\n", "\n", "print(\"response: {}\".format(json.dumps(response, indent=4, default=str)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Verify\n", "Verify that enabling IoT registry events was successfull." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "response = c_iot_m.describe_event_configurations()\n", "print(\"response: {}\".format(json.dumps(response, indent=4, default=str)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create Dynamo DB global table\n", "The global table consists of a table in the master region and one in the slave region.\n", "\n", "Create a dynamodb boto3 client for the master region." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "c_dynamo_m = boto3.client('dynamodb', region_name = config['aws_region_master'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create the global table." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "response = c_dynamo_m.create_global_table(\n", " GlobalTableName=config['dynamo_provisioning_table'],\n", " ReplicationGroup=[\n", " {'RegionName': config['aws_region_master']},\n", " {'RegionName': config['aws_region_slave']}\n", " ]\n", ")\n", "\n", "print(\"response: {}\".format(json.dumps(response, indent=4, default=str)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Verify\n", "Verify that the global table has been created." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "response = c_dynamo_m.describe_global_table(\n", " GlobalTableName=config['dynamo_provisioning_table']\n", ")\n", "\n", "print(\"response: {}\".format(json.dumps(response, indent=4, default=str)))" ] } ], "metadata": { "kernelspec": { "display_name": "conda_python3", "language": "python", "name": "conda_python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.5" } }, "nbformat": 4, "nbformat_minor": 4 }