<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>AWS IoT &#8211; IoT Expert</title>
	<atom:link href="https://iotexpert.com/category/iot-cloud/aws-iot/feed/" rel="self" type="application/rss+xml" />
	<link>https://iotexpert.com</link>
	<description>Engineering for the Internet of Things</description>
	<lastBuildDate>Mon, 26 Aug 2019 09:14:11 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://iotexpert.com/wp-content/uploads/2017/01/cropped-Avatar-32x32.jpg</url>
	<title>AWS IoT &#8211; IoT Expert</title>
	<link>https://iotexpert.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>The Creek 2.0: AWS IoT Actions &#038; Rules</title>
		<link>https://iotexpert.com/the-creek-2-0-aws-iot-actions-rules/</link>
					<comments>https://iotexpert.com/the-creek-2-0-aws-iot-actions-rules/#comments</comments>
		
		<dc:creator><![CDATA[Alan Hawse]]></dc:creator>
		<pubDate>Mon, 26 Aug 2019 09:14:10 +0000</pubDate>
				<category><![CDATA[AWS IoT]]></category>
		<category><![CDATA[Elkhorn Creek 2.0]]></category>
		<guid isPermaLink="false">https://iotexpert.com/?p=7388</guid>

					<description><![CDATA[Summary In this article, I will show you how to use the AWS IoT rules engine to make the last connection required in the chain of data from the Creek Sensor all the way to the AWS RDS Server.  I will also show you the AWS CloudWatch console.  At this point I have implemented 4 [&#8230;]]]></description>
										<content:encoded><![CDATA[<h1>Summary</h1>
<p>In this article, I will show you how to use the AWS IoT rules engine to make the last connection required in the chain of data from the Creek Sensor all the way to the AWS RDS Server.  I will also show you the AWS CloudWatch console.  At this point I have implemented</p>
<ul>
<li>4 &#8211; <a href="https://iotexpert.com/2019/07/22/the-creek-2-0-read-sensor-data-send-to-aws-iot-via-mqtt/" target="_blank" rel="noopener noreferrer">The Raspberry Pi connection</a></li>
<li>5 &#8211; <a href="https://iotexpert.com/2019/07/22/the-creek-2-0-read-sensor-data-send-to-aws-iot-via-mqtt/" target="_blank" rel="noopener noreferrer">The AWS IoT MQTT Broker</a></li>
<li>7 &#8211; <a href="https://iotexpert.com/2019/08/19/the-creek-2-0-aws-lambda-function/" target="_blank" rel="noopener noreferrer">The AWS Lambda Function</a></li>
<li>8 &#8211; <a href="https://iotexpert.com/2019/08/12/the-creek-2-0-aws-relational-database-server-rds-mysql/" target="_blank" rel="noopener noreferrer">The AWS RDS MySQL Server</a></li>
</ul>
<p>Let&#8217;s implement the final missing box (6) &#8211; The AWS IoT Rules</p>
<p><a href="https://iotexpert.com/2019/07/08/the-creek-amazon-aws-iot-solution-architecture-2-0/creek20-2/" rel="attachment wp-att-7367"><img fetchpriority="high" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/creek20-2-1024x339.png" alt="" width="1024" height="339" class="alignnone size-large wp-image-7367" srcset="https://iotexpert.com/wp-content/uploads/2019/07/creek20-2-1024x339.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/creek20-2-600x199.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/creek20-2-300x99.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/creek20-2-768x255.png 768w, https://iotexpert.com/wp-content/uploads/2019/07/creek20-2.png 1225w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>The AWS Rules</h1>
<p>Start by going to the AWS IoT Console.  On the bottom left you can see a button named &#8220;Act&#8221;.  If you click Act&#8230;</p>
<p><a href="https://iotexpert.com/2019/08/26/the-creek-2-0-aws-iot-actions-rules/screen-shot-2019-08-25-at-6-55-55-am/" rel="attachment wp-att-7870"><img decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-6.55.55-AM-1024x439.png" alt="" width="1024" height="439" class="alignnone size-large wp-image-7870" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-6.55.55-AM-1024x439.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-6.55.55-AM-600x257.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-6.55.55-AM-300x129.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-6.55.55-AM-768x329.png 768w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>You will land on a screen that looks like this.  Notice, that I have no rules (something that my wife complains about all of the time).  Click on &#8220;Create&#8221; to start the process of making a rule.</p>
<p><a href="https://iotexpert.com/2019/08/26/the-creek-2-0-aws-iot-actions-rules/screen-shot-2019-08-25-at-6-58-35-am/" rel="attachment wp-att-7871"><img decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-6.58.35-AM-1024x408.png" alt="" width="1024" height="408" class="alignnone size-large wp-image-7871" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-6.58.35-AM-1024x408.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-6.58.35-AM-600x239.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-6.58.35-AM-300x120.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-6.58.35-AM-768x306.png 768w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>On the create rule screen I will give it a name and a description.  Then, I need to create a &#8220;<a href="https://docs.aws.amazon.com/iot/latest/developerguide/iot-sql-reference.html" target="_blank" rel="noopener noreferrer">Rule query statement</a>&#8220;.  A rule query statement is an SQL like command that is used to match topics and conditions of the data on the topic.  Below, you can see that I tell it to select &#8220;*&#8221; which is all of the attributes.  And then I give it the name of the topic.  Notice that you are allowed to use the normal MQTT topic wildcards # and + to expand the list to match multiple topics.</p>
<p><a href="https://iotexpert.com/2019/08/26/the-creek-2-0-aws-iot-actions-rules/screen-shot-2019-08-25-at-7-00-49-am/" rel="attachment wp-att-7872"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.00.49-AM-1024x892.png" alt="" width="1024" height="892" class="alignnone size-large wp-image-7872" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.00.49-AM-1024x892.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.00.49-AM-600x523.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.00.49-AM-300x261.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.00.49-AM-768x669.png 768w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.00.49-AM.png 1940w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Scroll down to the &#8220;Set one or more actions&#8221; and click on &#8220;add action&#8221;</p>
<p><a href="https://iotexpert.com/2019/08/26/the-creek-2-0-aws-iot-actions-rules/screen-shot-2019-08-25-at-7-01-05-am/" rel="attachment wp-att-7873"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.01.05-AM-1024x187.png" alt="" width="1024" height="187" class="alignnone size-large wp-image-7873" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.01.05-AM-1024x187.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.01.05-AM-600x109.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.01.05-AM-300x55.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.01.05-AM-768x140.png 768w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.01.05-AM.png 1962w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>This screen is amazing as there are many many many things that you can do.  (I should try some of the others possibilities).  But, for this article just pick &#8220;Send a message to a Lambda function&#8221;</p>
<p><a href="https://iotexpert.com/2019/08/26/the-creek-2-0-aws-iot-actions-rules/screen-shot-2019-08-25-at-7-01-29-am/" rel="attachment wp-att-7874"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.01.29-AM-1024x491.png" alt="" width="1024" height="491" class="alignnone size-large wp-image-7874" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.01.29-AM-1024x491.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.01.29-AM-600x288.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.01.29-AM-300x144.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.01.29-AM-768x368.png 768w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.01.29-AM.png 2016w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Then press &#8220;Select&#8221; to pick out the function.</p>
<p><a href="https://iotexpert.com/2019/08/26/the-creek-2-0-aws-iot-actions-rules/screen-shot-2019-08-25-at-7-01-41-am/" rel="attachment wp-att-7875"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.01.41-AM-1024x499.png" alt="" width="1024" height="499" class="alignnone size-large wp-image-7875" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.01.41-AM-1024x499.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.01.41-AM-600x292.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.01.41-AM-300x146.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.01.41-AM-768x374.png 768w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.01.41-AM.png 2008w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Then you will see all of your Lambda functions.  Ill pick the &#8220;creekWaterLevelInsert&#8221; which is the function I created which takes the json data and inserts it into my AWS RDS MySQL database.</p>
<p><a href="https://iotexpert.com/2019/08/26/the-creek-2-0-aws-iot-actions-rules/screen-shot-2019-08-25-at-7-02-19-am/" rel="attachment wp-att-7877"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.02.19-AM-1024x764.png" alt="" width="1024" height="764" class="alignnone size-large wp-image-7877" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.02.19-AM-1024x764.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.02.19-AM-600x448.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.02.19-AM-300x224.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.02.19-AM-768x573.png 768w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.02.19-AM.png 1992w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Once you press &#8220;Update&#8221;, you will see that you have the newly created rule&#8230;</p>
<p><a href="https://iotexpert.com/2019/08/26/the-creek-2-0-aws-iot-actions-rules/screen-shot-2019-08-25-at-7-02-43-am/" rel="attachment wp-att-7878"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.02.43-AM-1024x452.png" alt="" width="1024" height="452" class="alignnone size-large wp-image-7878" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.02.43-AM-1024x452.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.02.43-AM-600x265.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.02.43-AM-300x132.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.02.43-AM-768x339.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>The Test Console</h1>
<p>Now that the rule is setup.  Let&#8217;s go to the AWS MQTT Test Client and wait for an update to the &#8220;applecreek&#8221;  thing Shadow.  You might recall that when a shadow update message is published to $aws/things/applecreek/shadow/update if that message is accepted then a response will be published by AWS to $aws/things/applecreek/shawdow/update/accepted.</p>
<p>On the test console, I subscribe to that topic.  After a bit of time I see this message get published that at 7:06AM the Apple Creek is 0.08.. feet and the temperature in my barn is 14.889 degrees.</p>
<p><a href="https://iotexpert.com/2019/08/26/the-creek-2-0-aws-iot-actions-rules/screen-shot-2019-08-25-at-7-06-47-am/" rel="attachment wp-att-7879"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.06.47-AM-1024x779.png" alt="" width="1024" height="779" class="alignnone size-large wp-image-7879" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.06.47-AM-1024x779.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.06.47-AM-600x456.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.06.47-AM-300x228.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.06.47-AM-768x584.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>But, did it work?</p>
<h1> AWS Cloud Watch</h1>
<p>There are a couple of ways to figure this out.  But, I start by going to AWS CloudWatch which is the AWS consolidator for all of the error logs etc.  To get there search for &#8220;CloudWatch&#8221; on the AWS Management Console.</p>
<p><a href="https://iotexpert.com/2019/08/26/the-creek-2-0-aws-iot-actions-rules/screen-shot-2019-08-25-at-7-08-40-am/" rel="attachment wp-att-7881"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.08.40-AM-1024x477.png" alt="" width="1024" height="477" class="alignnone size-large wp-image-7881" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.08.40-AM-1024x477.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.08.40-AM-600x279.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.08.40-AM-300x140.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.08.40-AM-768x358.png 768w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.08.40-AM.png 1752w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Then click on &#8220;logs&#8221;.  Notice that the log at the top is called &#8220;&#8230;/creekWaterLevelInsert&#8221;.   As best I can tell, many things in AWS generate debugging or security messages which go to these log files.</p>
<p><a href="https://iotexpert.com/2019/08/26/the-creek-2-0-aws-iot-actions-rules/screen-shot-2019-08-25-at-7-09-24-am/" rel="attachment wp-att-7882"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.09.24-AM-1024x406.png" alt="" width="1024" height="406" class="alignnone size-large wp-image-7882" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.09.24-AM-1024x406.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.09.24-AM-600x238.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.09.24-AM-300x119.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.09.24-AM-768x304.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>If you click on the /aws/lambda/creekWaterLevelInsert you can see that there are a bunch of different log streams for this Lambda Function.  These streams are just ranges of time where events have happened (I have actually been running this rule for  a while)</p>
<p><a href="https://iotexpert.com/2019/08/26/the-creek-2-0-aws-iot-actions-rules/screen-shot-2019-08-25-at-7-09-45-am/" rel="attachment wp-att-7883"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.09.45-AM-1024x408.png" alt="" width="1024" height="408" class="alignnone size-large wp-image-7883" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.09.45-AM-1024x408.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.09.45-AM-600x239.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.09.45-AM-300x120.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.09.45-AM-768x306.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a> <a href="https://iotexpert.com/2019/08/26/the-creek-2-0-aws-iot-actions-rules/screen-shot-2019-08-25-at-7-11-39-am/" rel="attachment wp-att-7884"></a></p>
<p>If I click on the top one, and scroll to the bottom you can see that at &#8220;11:06:23&#8221; the function was run.  And you can see the JSON message which was sent to the function.  You might ask yourself 11:06 &#8230; up above it was 7:06&#8230; why the 4 hours difference.  The answer to that question is that the AWS logs are all recorded in UTC&#8230; but I save my messages in Eastern time which is  current UTC-4.  (In hindsight I think that you should record all time in UTC)</p>
<p><a href="https://iotexpert.com/2019/08/26/the-creek-2-0-aws-iot-actions-rules/screen-shot-2019-08-25-at-7-11-39-am/" rel="attachment wp-att-7884"></a> <a href="https://iotexpert.com/2019/08/26/the-creek-2-0-aws-iot-actions-rules/screen-shot-2019-08-25-at-7-12-00-am/" rel="attachment wp-att-7885"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.12.00-AM-1024x538.png" alt="" width="1024" height="538" class="alignnone size-large wp-image-7885" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.12.00-AM-1024x538.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.12.00-AM-600x315.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.12.00-AM-300x158.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.12.00-AM-768x404.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>The real way to check to make sure that the lambda function worked correctly is to verify that the data was inserted into my RDS MySQL database.  To find this out I open up a connection using MySQL WorkBench (which I wrote about <a href="https://iotexpert.com/2019/08/12/the-creek-2-0-aws-relational-database-server-rds-mysql/" target="_blank" rel="noopener noreferrer">here</a>).  I ask it to give me the most recent data inserted into the database and sure enough I can see that at 7:06 the temperature was 14.9 and the depth was 0.08&#8230; sweet.</p>
<p><a href="https://iotexpert.com/2019/08/26/the-creek-2-0-aws-iot-actions-rules/screen-shot-2019-08-25-at-7-07-13-am/" rel="attachment wp-att-7880"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.07.13-AM-1024x582.png" alt="" width="1024" height="582" class="alignnone size-large wp-image-7880" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.07.13-AM-1024x582.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.07.13-AM-600x341.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.07.13-AM-300x170.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.07.13-AM-768x436.png 768w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-25-at-7.07.13-AM.png 1532w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>For now this series is over.  However, what I really need to do next is write a web server that runs on AWS to display the data&#8230; but that will be for another day.</p>
<p><span><p><div class="table-responsive"><table  style="width:95%; "  class="easy-table easy-table-default " border="1">
<thead>
<tr><th >#</th>
<th >Title</th>
</tr>
</thead>
<tbody>
<tr><td >0</td>
<td ><a href="https://iotexpert.com/2019/07/08/the-creek-amazon-aws-iot-solution-architecture-2-0/" target="_blank" rel="noopener">The Creek 2.0: Amazon AWS IoT Solution Architecture 2.0</a></td>
</tr>

<tr><td >1</td>
<td ><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/" target="_blank" rel="noopener">The Creek 2.0: AWS IoT MQTT Message Broker</a></td>
</tr>

<tr><td >2</td>
<td ><a href="https://iotexpert.com/2019/07/22/the-creek-2-0-read-sensor-data-send-to-aws-iot-via-mqtt/" target="_blank" rel="noopener">The Creek 2.0: Read Sensor Data Send to AWS IoT via MQTT</a></td>
</tr>

<tr><td >3</td>
<td ><a href="https://iotexpert.com/2019/07/29/amazon-aws-virtual-private-cloud-vpc/" target="_blank" rel="noopener">Amazon AWS Virtual Private Cloud (VPC)</a></td>
</tr>

<tr><td >4</td>
<td ><a href="https://iotexpert.com/2019/08/12/the-creek-2-0-aws-relational-database-server-rds-mysql/" target="_blank" rel="noopener">The Creek 2.0: AWS Relational Database Server (RDS) - MySQL</a></td>
</tr>

<tr><td >5</td>
<td ><a href="https://iotexpert.com/2019/08/19/the-creek-2-0-aws-lambda-function/" target="_blank" rel="noopener">The Creek 2.0: AWS Lambda Function</a></td>
</tr>

<tr><td >6</td>
<td ><a href="https://iotexpert.com/the-creek-2-0-aws-iot-actions-rules/">The Creek 2.0: AWS IoT Actions &amp; Rules</a></td>
</tr>
</tbody></table></div></p></span></p>
]]></content:encoded>
					
					<wfw:commentRss>https://iotexpert.com/the-creek-2-0-aws-iot-actions-rules/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>The Creek 2.0: AWS Lambda Function</title>
		<link>https://iotexpert.com/the-creek-2-0-aws-lambda-function/</link>
					<comments>https://iotexpert.com/the-creek-2-0-aws-lambda-function/#comments</comments>
		
		<dc:creator><![CDATA[Alan Hawse]]></dc:creator>
		<pubDate>Mon, 19 Aug 2019 12:00:45 +0000</pubDate>
				<category><![CDATA[AWS IoT]]></category>
		<category><![CDATA[Elkhorn Creek 2.0]]></category>
		<guid isPermaLink="false">https://iotexpert.com/?p=7387</guid>

					<description><![CDATA[Summary At this point in the Creek 2.0 series I have data that is moving from my sensor into the AWS IoT core via MQTT.  I also have a VPC with an AWS RDS MySQL database running.  In order to get the data from the AWS IoT Device Shadow into the database, I am left [&#8230;]]]></description>
										<content:encoded><![CDATA[<h1>Summary</h1>
<p>At this point in the <a href="https://iotexpert.com/category/solutions/elkhorn-creek-2-0/" target="_blank" rel="noopener noreferrer">Creek 2.0 series</a> I have data that is moving from my sensor into the AWS IoT core via MQTT.  I also have a VPC with an AWS RDS MySQL database running.  In order to get the data from the AWS IoT Device Shadow into the database, I am left with a two remaining steps:</p>
<ol>
<li>Create a Lambda Function which can run when asked and store data into the Database (this article)</li>
<li>Connect the IoT MQTT Message Broker to the Lambda Function (the next article)</li>
</ol>
<p>This article addresses the Lambda Function, which unfortunately is best written in Python.  I say &#8216;unfortunately&#8217; because I&#8217;ve always had enough self-respect to avoid programing in Python &#8211; that evil witch&#8217;s brew of a hacker language.  <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" />  But more seriously, I have never written a line of code in Python so it has been a bit of a journey.  As a side note, I am also interested in Machine Learning and the <a href="https://www.tensorflow.org" target="_blank" rel="noopener noreferrer">Google TensorFlow</a> is Python driven, so all is not lost.</p>
<p>For this article, I will address:</p>
<ol>
<li>What is an AWS Lambda Function?</li>
<li>Create a Lambda Function</li>
<li>Run a Simple Test</li>
<li>Install the Python Libraries (Deployment Package)</li>
<li>Create a MySQL Connection and Test</li>
<li>Configure the Lambda Function to Run in your VPC</li>
<li>Create an IAM Role and Assign to the Lambda Function</li>
<li>Update the Lambda Function to Insert Data</li>
<li>The Whole Program</li>
</ol>
<h1>What is an AWS Lambda Function?</h1>
<p>AWS Lambda is a place in the AWS Cloud where you can store a program, called a Lambda Function.  The name came from the &#8220;<a href="https://en.wikipedia.org/wiki/Anonymous_function" target="_blank" rel="noopener noreferrer">anonymous</a>&#8221; function paradigm which is also called a lambda function in some languages (lisp was the first place I used it).  The program can then be triggered to run by a bunch of different things including the AWS IoT MQTT Broker.  The cool part is that you don&#8217;t have to manage a server because it is magically created for you on demand.   You tell AWS what kind of environment you want (Python, Go, Javascript etc), then AWS automatically creates that environment and runs your Lambda function on demand.</p>
<p>In this case, we will trigger the lambda function when the AWS IoT Message Broker accepts a change to the Device Shadow.  I suppose that the easiest way to understand is to actually build a Lambda Function.</p>
<h1>Create a Lambda Function</h1>
<p>To create a Lambda function you will need to go to the Lambda management console.  To get there, start on the AWS Management console and search for &#8220;lambda&#8221;</p>
<p><a href="https://iotexpert.com/?attachment_id=7799" rel="attachment wp-att-7799"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.24.38-AM-1024x495.png" alt="" width="1024" height="495" class="alignnone size-large wp-image-7799" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.24.38-AM-1024x495.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.24.38-AM-600x290.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.24.38-AM-300x145.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.24.38-AM-768x371.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>On the Lambda console, click &#8220;Functions&#8221; then &#8220;Create function&#8221;</p>
<p><a href="https://iotexpert.com/?attachment_id=7800" rel="attachment wp-att-7800"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.24.54-AM-1024x284.png" alt="" width="1024" height="284" class="alignnone size-large wp-image-7800" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.24.54-AM-1024x284.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.24.54-AM-600x166.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.24.54-AM-300x83.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.24.54-AM-768x213.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>We will build this function from scratch&#8230; oh the adventure.  Give the function a name, in this case &#8220;exampleInsertData&#8221;.  Finally, select the Runtime.  You have several choices including &#8220;Python 3.7&#8221; which I suppose was the lesser of evils.</p>
<p><a href="https://iotexpert.com/?attachment_id=7802" rel="attachment wp-att-7802"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.30.58-AM-1024x788.png" alt="" width="1024" height="788" class="alignnone size-large wp-image-7802" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.30.58-AM-1024x788.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.30.58-AM-600x462.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.30.58-AM-300x231.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.30.58-AM-768x591.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Once you click &#8220;Create function&#8221; you will magically arrive at this screen where you can get to work.  Notice that the AWS folks give you a nice starter function.</p>
<p><a href="https://iotexpert.com/?attachment_id=7803" rel="attachment wp-att-7803"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.32.07-AM-1024x823.png" alt="" width="1024" height="823" class="alignnone size-large wp-image-7803" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.32.07-AM-1024x823.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.32.07-AM-600x482.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.32.07-AM-300x241.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.32.07-AM-768x617.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>Run a Simple Test</h1>
<p>Now the we have a simple function let us run a simple test &#8211; simple, eh?  To do this, click on the drop down arrow where it says &#8220;Select a test event&#8221; and then pick out &#8220;Configure test events&#8221;</p>
<p><a href="https://iotexpert.com/?attachment_id=7809" rel="attachment wp-att-7809"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.51.43-AM-1024x214.png" alt="" width="1024" height="214" class="alignnone size-large wp-image-7809" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.51.43-AM-1024x214.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.51.43-AM-600x125.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.51.43-AM-300x63.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.51.43-AM-768x160.png 768w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.51.43-AM.png 1436w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>On the configure test event screen,  just give your event the name &#8220;testEvent1&#8221; and click &#8220;Create&#8221;</p>
<p><a href="https://iotexpert.com/?attachment_id=7805" rel="attachment wp-att-7805"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.32.46-AM-932x1024.png" alt="" width="932" height="1024" class="alignnone size-large wp-image-7805" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.32.46-AM-932x1024.png 932w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.32.46-AM-600x659.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.32.46-AM-273x300.png 273w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.32.46-AM-768x844.png 768w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.32.46-AM.png 1646w" sizes="auto, (max-width: 932px) 100vw, 932px" /></a></p>
<p>Now you can select &#8220;testEvent1&#8221; and then click &#8220;Test&#8221;</p>
<p><a href="https://iotexpert.com/?attachment_id=7806" rel="attachment wp-att-7806"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.33.06-AM-1024x101.png" alt="" width="1024" height="101" class="alignnone size-large wp-image-7806" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.33.06-AM-1024x101.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.33.06-AM-600x59.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.33.06-AM-300x30.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.33.06-AM-768x76.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>This will take the JSON message that you defined above (actually you let it be default) and send it into the Lambda program.  The console will show you the output of the whole mess in the &#8220;Execution result: &#8230;&#8221;  Press the little &#8220;Details arrow&#8221; to see everything.  Notice that the default function sends a JSON keymap with two keys.</p>
<ul>
<li>statusCode</li>
<li>body</li>
</ul>
<p><a href="https://iotexpert.com/?attachment_id=7807" rel="attachment wp-att-7807"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.33.21-AM-1024x609.png" alt="" width="1024" height="609" class="alignnone size-large wp-image-7807" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.33.21-AM-1024x609.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.33.21-AM-600x357.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.33.21-AM-300x178.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.33.21-AM-768x457.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>When you function runs, an object is created inside of your Python program called &#8220;event&#8221; that is the JSON object that was sent to the Lambda function.  When we created the testEvent1 it gave us the option to specify the JSON object which is used as the argument to the function.  The default was a keymap with three keys key1,key2 and key3.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c" ">{
  "key1": "value1",
  "key2": "value2",
  "key3": "value3"
}
</pre>
<p>Instead of having the function return &#8220;Hello from Lambda&#8221; lets have it return the value that goes with &#8220;key1&#8221;.  To do that, make a little modification to the function to &#8220;json.dumps(event[&#8216;key1&#8217;])&#8221;.  Now when you run the test you can see that it returns the &#8220;body&#8221; as &#8220;value1&#8221;.</p>
<p><a href="https://iotexpert.com/?attachment_id=7808" rel="attachment wp-att-7808"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.38.22-AM-1024x672.png" alt="" width="1024" height="672" class="alignnone size-large wp-image-7808" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.38.22-AM-1024x672.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.38.22-AM-600x394.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.38.22-AM-300x197.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-8.38.22-AM-768x504.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>Install Python Libraries</h1>
<p>The default installation of Python 3.7 in Lambda does not have two libraries that I want to use.  Specifically:</p>
<ul>
<li>pymysql &#8211; a MySQL database interface</li>
<li>pytz &#8211; a library for manipulating time (unfortunately it can&#8217;t create more time)</li>
</ul>
<p>I actually don&#8217;t know what libraries are in the default Python3.7 runtime (or actually even how to figure it out?).  In order to use libraries which are not part of the Python installation by default, you need to create a &#8220;<a href="https://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html" target="_blank" rel="noopener noreferrer">Python Deployment Package</a>&#8220;.  If you google this problem, you will find an amazing amount of confusion on this topic.  The humorist <a href="http://www.xkcd.com" target="_blank" rel="noopener noreferrer">XKCD</a> drew a very appropriate cartoon about this topic.  (I think that I&#8217;m allowed to link it?  but if not I&#8217;m sorry and I&#8217;ll remove it)</p>
<p><img decoding="async" src="https://imgs.xkcd.com/comics/python_environment.png" /></p>
<p>Making a deployment package is actually pretty straightforward.  The steps are:</p>
<ol>
<li>Create a directory on your computer</li>
<li>Use PIP3 to install the libraries you need in your LOCAL directory</li>
<li>Zip it all up</li>
<li>Upload the zip file to AWS Lambda</li>
</ol>
<p>Here are the first three steps (notice that I use pip3)</p>
<p><a href="https://iotexpert.com/?attachment_id=7814" rel="attachment wp-att-7814"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-9.28.59-AM-1024x451.png" alt="" width="1024" height="451" class="alignnone size-large wp-image-7814" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-9.28.59-AM-1024x451.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-9.28.59-AM-600x264.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-9.28.59-AM-300x132.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-9.28.59-AM-768x339.png 768w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-9.28.59-AM.png 1352w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>To update your AWS Lambda function, select &#8220;Upload a .zip file&#8221; on the Function code drop down.</p>
<p><a href="https://iotexpert.com/?attachment_id=7815" rel="attachment wp-att-7815"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-9.30.19-AM-1024x664.png" alt="" width="1024" height="664" class="alignnone size-large wp-image-7815" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-9.30.19-AM-1024x664.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-9.30.19-AM-600x389.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-9.30.19-AM-300x195.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-9.30.19-AM-768x498.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Then pick your zip file.</p>
<p><a href="https://iotexpert.com/?attachment_id=7816" rel="attachment wp-att-7816"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-9.30.40-AM-1024x532.png" alt="" width="1024" height="532" class="alignnone size-large wp-image-7816" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-9.30.40-AM-1024x532.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-9.30.40-AM-600x312.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-9.30.40-AM-300x156.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-9.30.40-AM-768x399.png 768w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-9.30.40-AM.png 1598w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Now you need to press the &#8220;Save&#8221; button which will do the actual update.</p>
<p><a href="https://iotexpert.com/?attachment_id=7819" rel="attachment wp-att-7819"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-9.33.09-AM-1024x733.png" alt="" width="1024" height="733" class="alignnone size-large wp-image-7819" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-9.33.09-AM-1024x733.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-9.33.09-AM-600x429.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-9.33.09-AM-300x215.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-9.33.09-AM-768x550.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>After the upload happens you will get an error message like this.  The problem is that you don&#8217;t have a file called &#8220;lambda_function.py&#8221; and/or that file doesn&#8217;t have a function called &#8220;lamda.handler&#8221;.  AWS is right, we don&#8217;t have either of them.</p>
<p><a href="https://iotexpert.com/?attachment_id=7818" rel="attachment wp-att-7818"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-9.31.17-AM-1024x655.png" alt="" width="1024" height="655" class="alignnone size-large wp-image-7818" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-9.31.17-AM-1024x655.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-9.31.17-AM-600x384.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-9.31.17-AM-300x192.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-9.31.17-AM-768x491.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>But you can see that we now have the &#8220;package&#8221; directory with the stuff we need to attach to the MySQL database and to manipulate time.</p>
<p><a href="https://iotexpert.com/?attachment_id=7820" rel="attachment wp-att-7820"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-9.35.15-AM-1024x660.png" alt="" width="1024" height="660" class="alignnone size-large wp-image-7820" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-9.35.15-AM-1024x660.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-9.35.15-AM-600x387.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-9.35.15-AM-300x193.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-9.35.15-AM-768x495.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>The little box that says &#8220;handler&#8221; tells you that you need to have a file called &#8220;lamda_function.py&#8221; and that Python file needs to have a function called &#8220;lambda_handler&#8221;.  So let&#8217;s create that file and function.  Start with &#8220;File-&gt;New File&#8221;</p>
<p><a href="https://iotexpert.com/?attachment_id=7821" rel="attachment wp-att-7821"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-10.42.27-AM.png" alt="" width="970" height="646" class="alignnone size-full wp-image-7821" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-10.42.27-AM.png 970w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-10.42.27-AM-600x400.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-10.42.27-AM-300x200.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-10.42.27-AM-768x511.png 768w" sizes="auto, (max-width: 970px) 100vw, 970px" /></a></p>
<p>The a &#8220;File-&gt;Save As&#8230;&#8221;</p>
<p><a href="https://iotexpert.com/?attachment_id=7822" rel="attachment wp-att-7822"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-10.43.05-AM-1024x651.png" alt="" width="1024" height="651" class="alignnone size-large wp-image-7822" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-10.43.05-AM-1024x651.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-10.43.05-AM-600x381.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-10.43.05-AM-300x191.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-10.43.05-AM-768x488.png 768w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-10.43.05-AM.png 1492w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Give it the name &#8220;lambda_function.py&#8221;</p>
<p><a href="https://iotexpert.com/?attachment_id=7823" rel="attachment wp-att-7823"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-10.43.34-AM-1024x731.png" alt="" width="1024" height="731" class="alignnone size-large wp-image-7823" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-10.43.34-AM-1024x731.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-10.43.34-AM-600x428.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-10.43.34-AM-300x214.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-10.43.34-AM-768x548.png 768w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-10.43.34-AM.png 1110w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Now write the same function as before.  Then press &#8220;save&#8221;.  You could have created the function and file on your computer and then uploaded it as part of the zip file, but I didn&#8217;t.</p>
<pre class="lang:python decode:true ">import json

def lambda_handler(event,context):
    return {
        'statusCode' : 200,
        'body' : json.dumps(event['key1'])
    }
</pre>
<p>OK.  Let&#8217;s test and make sure that everything is still working.  So run the &#8220;testEvent1&#8243;&#8230; and you should see that it returns the same thing.</p>
<p><a href="https://iotexpert.com/?attachment_id=7824" rel="attachment wp-att-7824"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-10.50.08-AM-1024x926.png" alt="" width="1024" height="926" class="alignnone size-large wp-image-7824" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-10.50.08-AM-1024x926.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-10.50.08-AM-600x543.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-10.50.08-AM-300x271.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-10.50.08-AM-768x695.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>The next step is to create and test a MySQL connection.</p>
<h1>Create a MySQL Connection and Test</h1>
<p>This simple bit of Python uses the &#8220;pymysql&#8221; library to open up a connection to the &#8220;rds_host&#8221; with the &#8220;name&#8221; and &#8220;password&#8221;.  Assuming this works, the program goes on and runs the lambda_hander.  Otherwise it spits out an error to the log and exits.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">import json
import logging
from package import pymysql


#rds settings
rds_host  = "your database endpoint goes here.us-east-2.rds.amazonaws.com"
name = "your mysql user name"
password = "your mysql password "
db_name = "your database name"

logger = logging.getLogger()
logger.setLevel(logging.INFO)

try:
    conn = pymysql.connect(rds_host, user=name, passwd=password, db=db_name, connect_timeout=5)
except:
    logger.error("ERROR: Unexpected error: Could not connect to MySQL instance.")
    sys.exit()
    

def lambda_handler(event,context):
    return {
        'statusCode' : 200,
        'body' : json.dumps(event['key1'])
    }</pre>
<p>When I run the test, I get this message which took me a long time to figure out.  Like a stupidly long time.  In order to fix it, you need to configure the Lambda function to run in your VPC.</p>
<p><a href="https://iotexpert.com/?attachment_id=7826" rel="attachment wp-att-7826"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.05.34-AM-669x1024.png" alt="" width="669" height="1024" class="alignnone size-large wp-image-7826" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.05.34-AM-669x1024.png 669w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.05.34-AM-600x918.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.05.34-AM-196x300.png 196w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.05.34-AM-768x1175.png 768w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.05.34-AM.png 1124w" sizes="auto, (max-width: 669px) 100vw, 669px" /></a></p>
<h1>Configure the Lambda Function to Run in your VPC</h1>
<p>The problem is that the AWS Lambda Functions runs on the public Internet which does not have access to your <a href="https://iotexpert.com/2019/08/12/the-creek-2-0-aws-relational-database-server-rds-mysql/" target="_blank" rel="noopener noreferrer">AWS RDS database</a> which you might recalls is on a private subnet in my VPC.  To fix this, you need to tell AWS to run your function INSIDE of your VPC.  Scroll down to the network section.  See where it says &#8220;No VPC&#8221;</p>
<p><a href="https://iotexpert.com/?attachment_id=7827" rel="attachment wp-att-7827"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.08.02-AM-1024x500.png" alt="" width="1024" height="500" class="alignnone size-large wp-image-7827" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.08.02-AM-1024x500.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.08.02-AM-600x293.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.08.02-AM-300x146.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.08.02-AM-768x375.png 768w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.08.02-AM.png 1188w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Pick out your VPC and then pick out two subnets in your VPC.  You probably should pick two subnets from different availability zones.  But it doesn&#8217;t matter if they are public or not as they only talk to the database.</p>
<p><a href="https://iotexpert.com/?attachment_id=7828" rel="attachment wp-att-7828"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.08.37-AM-1024x940.png" alt="" width="1024" height="940" class="alignnone size-large wp-image-7828" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.08.37-AM-1024x940.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.08.37-AM-600x551.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.08.37-AM-300x275.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.08.37-AM-768x705.png 768w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.08.37-AM.png 1190w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>After clicking save I get this message &#8220;Your role does not have VPC permissions&#8221;.  This took forever to figure out as well.  To fix this problem, you need to create the correct IAM role&#8230;.</p>
<p><a href="https://iotexpert.com/?attachment_id=7829" rel="attachment wp-att-7829"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.09.04-AM-1024x573.png" alt="" width="1024" height="573" class="alignnone size-large wp-image-7829" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.09.04-AM-1024x573.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.09.04-AM-600x336.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.09.04-AM-300x168.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.09.04-AM-768x430.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>Create an IAM Role and Assign to the Lambda Function</h1>
<p>To create the role, you need to get to the IAM console and the &#8220;roles&#8221; sub console.  There are several way to get to the screen to create the role.  But I do this by going to the AWS console, searching for IAM, and clicking.</p>
<p><a href="https://iotexpert.com/?attachment_id=7830" rel="attachment wp-att-7830"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.17.51-AM-1024x474.png" alt="" width="1024" height="474" class="alignnone size-large wp-image-7830" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.17.51-AM-1024x474.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.17.51-AM-600x278.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.17.51-AM-300x139.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.17.51-AM-768x355.png 768w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.17.51-AM.png 1374w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>This takes me to the IAM Console.  I don&#8217;t know that much about these options.  Actually looking at this screen shot it looks like I have some &#8220;Security status&#8221; issues (which I will need to figure out).  However in order to get the Lambda function to attach to your VPC, you need to create a role.  Do this by clicking &#8220;Roles&#8221;</p>
<p><a href="https://iotexpert.com/?attachment_id=7832" rel="attachment wp-att-7832"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.22.40-AM-1024x408.png" alt="" width="1024" height="408" class="alignnone size-large wp-image-7832" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.22.40-AM-1024x408.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.22.40-AM-600x239.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.22.40-AM-300x119.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.22.40-AM-768x306.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>When you click on roles you can see that there are several roles, essentially rules that give your identity the ability to do things in the AWS cloud.  There are some that are created by default.  But in order for your Lambda function to attach to your VPC, you need to give it permission.  To do this click &#8220;Create role&#8221;</p>
<p><a href="https://iotexpert.com/?attachment_id=7831" rel="attachment wp-att-7831"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.17.06-AM-1024x614.png" alt="" width="1024" height="614" class="alignnone size-large wp-image-7831" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.17.06-AM-1024x614.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.17.06-AM-600x360.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.17.06-AM-300x180.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.17.06-AM-768x461.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Pick &#8220;AWS service&#8221; and &#8220;Lambda&#8221; then click Next: Permissions</p>
<p><a href="https://iotexpert.com/?attachment_id=7833" rel="attachment wp-att-7833"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.27.31-AM-1024x577.png" alt="" width="1024" height="577" class="alignnone size-large wp-image-7833" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.27.31-AM-1024x577.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.27.31-AM-600x338.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.27.31-AM-300x169.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.27.31-AM-768x433.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Search for the &#8220;AWSLambdaVPCAccessExecutionRole&#8221;.  Pick it and then click Next: Tags</p>
<p><a href="https://iotexpert.com/?attachment_id=7839" rel="attachment wp-att-7839"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.39.43-AM-1024x456.png" alt="" width="1024" height="456" class="alignnone size-large wp-image-7839" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.39.43-AM-1024x456.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.39.43-AM-600x267.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.39.43-AM-300x133.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.39.43-AM-768x342.png 768w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.39.43-AM.png 2014w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>I don&#8217;t have any tags so click Next: Review</p>
<p><a href="https://iotexpert.com/?attachment_id=7835" rel="attachment wp-att-7835"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.27.50-AM-1024x593.png" alt="" width="1024" height="593" class="alignnone size-large wp-image-7835" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.27.50-AM-1024x593.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.27.50-AM-600x348.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.27.50-AM-300x174.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.27.50-AM-768x445.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Give the role a name &#8220;exampleVpcExecution&#8221; then click Create role.</p>
<p><a href="https://iotexpert.com/?attachment_id=7840" rel="attachment wp-att-7840"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.41.33-AM-1024x597.png" alt="" width="1024" height="597" class="alignnone size-large wp-image-7840" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.41.33-AM-1024x597.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.41.33-AM-600x350.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.41.33-AM-300x175.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.41.33-AM-768x448.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>You should get a success message.</p>
<p><a href="https://iotexpert.com/?attachment_id=7837" rel="attachment wp-att-7837"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.28.21-AM-1024x457.png" alt="" width="1024" height="457" class="alignnone size-large wp-image-7837" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.28.21-AM-1024x457.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.28.21-AM-600x268.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.28.21-AM-300x134.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.28.21-AM-768x343.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Now go back to the Lambda function configuration screen.  Move down to &#8220;Execution role&#8221; and pick out the role that you just created.</p>
<p><a href="https://iotexpert.com/?attachment_id=7838" rel="attachment wp-att-7838"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.31.59-AM-1024x579.png" alt="" width="1024" height="579" class="alignnone size-large wp-image-7838" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.31.59-AM-1024x579.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.31.59-AM-600x339.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.31.59-AM-300x170.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.31.59-AM-768x434.png 768w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.31.59-AM.png 1246w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Now when I test things work&#8230;. now let&#8217;s fix up the function to actually do the work of inserting data.</p>
<p><a href="https://iotexpert.com/?attachment_id=7841" rel="attachment wp-att-7841"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.42.11-AM-1024x761.png" alt="" width="1024" height="761" class="alignnone size-large wp-image-7841" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.42.11-AM-1024x761.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.42.11-AM-600x446.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.42.11-AM-300x223.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-18-at-11.42.11-AM-768x570.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>Update the Lambda Function to Insert Data</h1>
<p>You should recall from the article on AWS MQTT that when you update the IoT Device Shadow via MQTT you publish a JSON message like this to the topic &#8220;$aws/things/applecreek/shadow/update&#8221;</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">{
  "state": {
    "reported": {
      "temperature": 37.39998245239258,
      "depth": 0.036337487399578094,
      "thing": "applecreek"
    }
  }
}
</pre>
<p>Which will cause the AWS IoT to update you device shadow and then publish a message to &#8220;$aws/things/applecreek/shadow/update/accepted&#8221; like this:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c" ">{
  "state": {
    "reported": {
      "temperature": 37.39998245239258,
      "depth": 0.036337487399578094,
      "thing": "applecreek"
    }
  },
  "metadata": {
    "reported": {
      "temperature": {
        "timestamp": 1566144733
      },
      "depth": {
        "timestamp": 1566144733
      },
      "thing": {
        "timestamp": 1566144733
      }
    }
  },
  "version": 27323,
  "timestamp": 1566144733
}</pre>
<p>In the next article Im going to show you how to hook up those messages to run Lambda function.  But, for now assume that the JSON that comes out of the &#8220;&#8230;/accepted&#8221; topic will be passed to your function as the &#8220;event&#8221;.</p>
<p>The program has the following sections:</p>
<ol>
<li>Setup the imports</li>
<li>Define some Configuration Variables</li>
<li>Make a logger</li>
<li>Make a connection to the RDS database</li>
<li>Find the name of the thing in the JSON message</li>
<li>Search for the thingId in the table creekdata.things</li>
<li>Find the state key/value</li>
<li>Find the reported key/value</li>
<li>Find the depth key/value</li>
<li>Find the temperature key/value</li>
<li>Find the timestamp key/value</li>
<li>Convert the UTC timestamp to Eastern Time (I should have long ago designed this differently)</li>
<li>Insert the new data point into the Database</li>
</ol>
<h2><strong>Setup the Imports</strong></h2>
<p>The logging import is used to write data to the AWS logging console.</p>
<p>The pymysql is a library that knows how to attach to MySQL databases.</p>
<p>I made the decision years ago to store time in eastern standard time in my database.  That turns out to have been a bad decision and I should have used UTC.  Oh well.  To remedy this problem I use the &#8220;pytz&#8221; to convert between UTC (what AWS uses) and EST (what my system uses)</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">import sys
sys.path.append("./package")
import logging
import pymysql

from pytz import timezone, common_timezones
import pytz
from datetime import datetime</pre>
<h2><strong>Define Some Configuration Variables</strong></h2>
<p>Rather than hardcode the Keys in the JSON message, I setup a number of global variables to hold their definition.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">stateKey ="state"
reportedKey = "reported"
depthKey = "depth"
temperatureKey = "temperature"
timeKey = "time"
deviceKey = "thing"
timeStampKey = "timestamp"</pre>
<h2>Make a connection to the RDS Database</h2>
<p>In order to write data to my RDS MySQL database I create a connection using &#8220;pymysql.connect&#8221;.  Notice that if this fails it will write into the cloud watch log.  If it succeeds then there will be a global variable called &#8220;conn&#8221; with the connection object.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">rds_host  = "creekdata.cycvrc9tai6g.us-east-2.rds.amazonaws.com"
name = "database user"
password = "databasepassword"
db_name = "creekdata"

try:
    conn = pymysql.connect(rds_host, user=name, passwd=password, db=db_name, connect_timeout=5)
except:
    logger.error("ERROR: Unexpected error: Could not connect to MySQL instance.")
    sys.exit()
    
</pre>
<h2>Make a logger</h2>
<p>AWS gives you the ability to write to the AWS CloudWatch logging system.  In order to write there, you need to create a &#8220;logger&#8221;</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">logger = logging.getLogger()
logger.setLevel(logging.INFO)</pre>
<h2>Look for the stateKey and reportKey</h2>
<p>The JSON message &#8220;should&#8221; have key called &#8220;state&#8221;.  The value of that key is another keymap with a value called &#8220;reported&#8221;</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c" ">if stateKey in event:
        if reportedKey in event[stateKey]:</pre>
<h2>Find the Depth</h2>
<p>Assuming that you have state/reported then you need to find the value of the depth</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c" ">if depthKey in event[stateKey][reportedKey]:
                depthValue = event[stateKey][reportedKey][depthKey]</pre>
<h2>Find the Temperature</h2>
<p>It was my intent to send the temperature every time I update the shadow state.  But I put in a provision for the temperature not being there and taking the value -99</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">if temperatureKey in event[stateKey][reportedKey]:
                temperatureValue = event[stateKey][reportedKey][temperatureKey]
            else:
                temperatureValue = -99</pre>
<h2>Look for a Timestamp</h2>
<p>My current sensor system does not keep time, however, I may add that functionality at some point.  So, I put in the ability to have a timeStamp set by the sensor.  If there is no timestamp there, AWS happily makes one for you when you update the device shadow.  I look in</p>
<ul>
<li>The reported state</li>
<li>The overall message</li>
<li>Or I barf</li>
</ul>
<pre class="EnlighterJSRAW" data-enlighter-language="c">if timeStampKey in event[stateKey][reportedKey]:
                timeValue = datetime.fromtimestamp(event[stateKey][reportedKey][timeStampKey],tz=pytz.utc)
#                logger.info("Using state time")
            elif timeStampKey in event:
#                logger.info("using timestamp" + str(event[timeStampKey]))
                timeValue = datetime.fromtimestamp(event[timeStampKey],tz=pytz.utc)
            else:
                raise Exception("JSON Missing time date")</pre>
<h2>Find the name of the thing in the JSON message</h2>
<p>My database has two tables.  The table called &#8220;creekdata&#8221; has columns of id, thingid, depth, temperature, created_at.  The thing id is key into another table called &#8220;things&#8221; which has the columns of thingid and name.  In other words, it has a map of a text name for things to a int value.  This lets me store multiple thing values in the same creekdata table&#8230; which turns out to be an overkill as I only have one sensor.</p>
<p>When I started working on this program I wanted the name of thing to be added automatically as part of the JSON message, but I couldn&#8217;t figure it out.  So, I added the thing name as a field which is put in by the sensor.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c" ">if deviceKey in event[stateKey][reportedKey]:
                deviceValue = event[stateKey][reportedKey][deviceKey]
            else:
                logger.error("JSON Event missing " + deviceKey)
                raise Exception("JSON Event missing " + deviceKey)</pre>
<h2>Search for the thingId in the table creekdata.things</h2>
<p>I wrote a function which takes the name of a thing and returns the thingId.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c" ">def getThingId(deviceName):
    with conn.cursor() as cur:
        cur.execute("select thingid from creekdata.things where name=%s", deviceName)
    results = cur.fetchall()
#    logger.info("Row = " + str(len(results)))
    if len(results) &gt; 0:
#        logger.info("thingid = "+ str(results[0][0]))
        return results[0][0]
    else:
        raise Exception("Device Name Not Found " + deviceName)</pre>
<h2>Convert the UTC timestamp to Eastern Time</h2>
<p>As I pointed out earlier, I should have switched the whole system to store UTC.  But no.  So I use the pytz function to switch my UTC value to EST.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c" ">  tz1 = pytz.timezone('US/Eastern')
    xc = timeValue.astimezone(tz1)
</pre>
<h2>Insert the New Data Point into the Database</h2>
<p>Now we know everything, so insert it into the database.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c" ">with conn.cursor() as cur:
        cur.execute("INSERT into creekdata.creekdata (created_at,depth, thingid,temperature) values (%s,%s,%s,%s)",(xc.strftime("%Y-%m-%d %H:%M:%S"),depthValue,thingIdValue,temperatureValue));
    conn.commit()</pre>
<h1>The Final Program</h1>
<p>Here is the whole program in one place.</p>
<pre class="lang:python decode:true">import json
import sys
import logging
import os

sys.path.append("./package")
import pymysql

from pytz import timezone, common_timezones
import pytz
from datetime import datetime

stateKey ="state"
reportedKey = "reported"
depthKey = "depth"
temperatureKey = "temperature"
timeKey = "time"
deviceKey = "thing"
timeStampKey = "timestamp"

#rds settings
rds_host  = "put your endpoint here.us-east-2.rds.amazonaws.com"
name = "mysecretuser"
password = "mysecretpassword"
db_name = "creekdata"

logger = logging.getLogger()
logger.setLevel(logging.INFO)

try:
    conn = pymysql.connect(rds_host, user=name, passwd=password, db=db_name, connect_timeout=5)
except:
    logger.error("ERROR: Unexpected error: Could not connect to MySQL instance.")
    sys.exit()
    

def lambda_handler(event,context):
    logger.info('## EVENT')
    logger.info(event)
    insertVal = ""
    if stateKey in event:
        if reportedKey in event[stateKey]:
            if depthKey in event[stateKey][reportedKey]:
                depthValue = event[stateKey][reportedKey][depthKey]
            if temperatureKey in event[stateKey][reportedKey]:
                temperatureValue = event[stateKey][reportedKey][temperatureKey]
            else:
                temperatureValue = -99
            if timeStampKey in event[stateKey][reportedKey]:
                timeValue = datetime.fromtimestamp(event[stateKey][reportedKey][timeStampKey],tz=pytz.utc)
#                logger.info("Using state time")
            elif timeStampKey in event:
#                logger.info("using timestamp" + str(event[timeStampKey]))
                timeValue = datetime.fromtimestamp(event[timeStampKey],tz=pytz.utc)
            else:
                raise Exception("JSON Missing time date")
                
            if deviceKey in event[stateKey][reportedKey]:
                deviceValue = event[stateKey][reportedKey][deviceKey]
            else:
                logger.error("JSON Event missing " + deviceKey)
                raise Exception("JSON Event missing " + deviceKey)
        else:
            raise Exception("JSON Event missing " + reportedKey)
    else:
        raise Exception("JSON Event missing " + stateKey)

    thingIdValue = getThingId(deviceValue)
    tz1 = pytz.timezone('US/Eastern')
    xc = timeValue.astimezone(tz1)

    with conn.cursor() as cur:
        cur.execute("INSERT into creekdata.creekdata (created_at,depth, thingid,temperature) values (%s,%s,%s,%s)",(xc.strftime("%Y-%m-%d %H:%M:%S"),depthValue,thingIdValue,temperatureValue));
    conn.commit()
        
    return "return value"  # Echo back the first key value

def getThingId(deviceName):
    with conn.cursor() as cur:
        cur.execute("select thingid from creekdata.things where name=%s", deviceName)
    results = cur.fetchall()
#    logger.info("Row = " + str(len(results)))
    if len(results) &gt; 0:
#        logger.info("thingid = "+ str(results[0][0]))
        return results[0][0]
    else:
        raise Exception("Device Name Not Found " + deviceName)</pre>
<p><span><p><div class="table-responsive"><table  style="width:95%; "  class="easy-table easy-table-default " border="1">
<thead>
<tr><th >#</th>
<th >Title</th>
</tr>
</thead>
<tbody>
<tr><td >0</td>
<td ><a href="https://iotexpert.com/2019/07/08/the-creek-amazon-aws-iot-solution-architecture-2-0/" target="_blank" rel="noopener">The Creek 2.0: Amazon AWS IoT Solution Architecture 2.0</a></td>
</tr>

<tr><td >1</td>
<td ><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/" target="_blank" rel="noopener">The Creek 2.0: AWS IoT MQTT Message Broker</a></td>
</tr>

<tr><td >2</td>
<td ><a href="https://iotexpert.com/2019/07/22/the-creek-2-0-read-sensor-data-send-to-aws-iot-via-mqtt/" target="_blank" rel="noopener">The Creek 2.0: Read Sensor Data Send to AWS IoT via MQTT</a></td>
</tr>

<tr><td >3</td>
<td ><a href="https://iotexpert.com/2019/07/29/amazon-aws-virtual-private-cloud-vpc/" target="_blank" rel="noopener">Amazon AWS Virtual Private Cloud (VPC)</a></td>
</tr>

<tr><td >4</td>
<td ><a href="https://iotexpert.com/2019/08/12/the-creek-2-0-aws-relational-database-server-rds-mysql/" target="_blank" rel="noopener">The Creek 2.0: AWS Relational Database Server (RDS) - MySQL</a></td>
</tr>

<tr><td >5</td>
<td ><a href="https://iotexpert.com/2019/08/19/the-creek-2-0-aws-lambda-function/" target="_blank" rel="noopener">The Creek 2.0: AWS Lambda Function</a></td>
</tr>

<tr><td >6</td>
<td ><a href="https://iotexpert.com/the-creek-2-0-aws-iot-actions-rules/">The Creek 2.0: AWS IoT Actions &amp; Rules</a></td>
</tr>
</tbody></table></div></p></span></p>
]]></content:encoded>
					
					<wfw:commentRss>https://iotexpert.com/the-creek-2-0-aws-lambda-function/feed/</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
			</item>
		<item>
		<title>The Creek 2.0: AWS Relational Database Server (RDS) &#8211; MySQL</title>
		<link>https://iotexpert.com/the-creek-2-0-aws-relational-database-server-rds-mysql/</link>
					<comments>https://iotexpert.com/the-creek-2-0-aws-relational-database-server-rds-mysql/#comments</comments>
		
		<dc:creator><![CDATA[Alan Hawse]]></dc:creator>
		<pubDate>Mon, 12 Aug 2019 12:13:27 +0000</pubDate>
				<category><![CDATA[AWS IoT]]></category>
		<category><![CDATA[Elkhorn Creek 2.0]]></category>
		<guid isPermaLink="false">https://iotexpert.com/?p=7389</guid>

					<description><![CDATA[Summary In the previous articles I showed you the overall Creek 2.0 Architecture (1-8).  Then I explained how AWS MQTT (5) works, and I showed you how to write a Python program to update the device shadow (4).  In this article, I will create an AWS Relational Database Server (RDS) that runs MySQL which will be used [&#8230;]]]></description>
										<content:encoded><![CDATA[<h1>Summary</h1>
<p>In the previous articles I showed you the overall <a href="https://iotexpert.com/2019/07/08/the-creek-amazon-aws-iot-solution-architecture-2-0/">Creek 2.0 Architecture</a> (1-8).  Then I explained how <a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/">AWS MQTT</a> (5) works, and I showed you how to write a Python program to update the device shadow (4).  In this article, I will create an AWS Relational Database Server (RDS) that runs MySQL which will be used to store the data.</p>
<p><a href="https://iotexpert.com/2019/07/08/the-creek-amazon-aws-iot-solution-architecture-2-0/creek20-2/" rel="attachment wp-att-7367"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/creek20-2-1024x339.png" alt="" width="1024" height="339" class="alignnone size-large wp-image-7367" srcset="https://iotexpert.com/wp-content/uploads/2019/07/creek20-2-1024x339.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/creek20-2-600x199.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/creek20-2-300x99.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/creek20-2-768x255.png 768w, https://iotexpert.com/wp-content/uploads/2019/07/creek20-2.png 1225w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>You might ask yourself why would I explain (8) before I explained (6) &amp; (7)?  The answer is that I need a place to send the data before the send the data functions will make any sense.</p>
<p>First, a definition, RDS &#8211; Relational Database Server &#8211; is Amazons name for a service that give you a database server, in your <a href="https://iotexpert.com/2019/07/29/amazon-aws-virtual-private-cloud-vpc/" target="_blank" rel="noopener noreferrer">VPC</a>, running an instance of MySQL, Aurora, DynamoDb, or Postgres.  In their words, RDS &#8220;&#8230;provides cost-efficient and resizable capacity while automating time-consuming administration tasks such as hardware provisioning, database setup, patching and backups.&#8221;   The AWS definition is largely true.  It does not however abdicate your DataBase Administrator (DBA) responsibilities.</p>
<p>For my application I need MySQL, so for this article I will walk you through setting up a MySQL database using AWS RDS.  The specific topics are:</p>
<ol>
<li>Create a Database Using the Amazon Defaults</li>
<li>Create MySQL WorkBench Connection</li>
<li>Examining the Security</li>
<li>Rethinking the Security &amp; Subnet Groups</li>
<li>Configure Security Groups</li>
<li>Create the Database I Really Want</li>
<li>MySQL WorkBench EC2 Tunneling over SSL</li>
</ol>
<h1>Create a Database Using the Amazon Defaults</h1>
<p>It is really easy to create a MySQL database using the default Amazon settings.  The setting will be absolutely fine, except that the Database will be attached to a Public Subnet rather than a private one.   This is probably mostly OK as the subnet settings that AWS creates by default are probably safe enough?  It is certainly easy, so let&#8217;s start there.  Go to your AWS management console.  Then search for RDS.</p>
<p><a href="https://iotexpert.com/?attachment_id=7727" rel="attachment wp-att-7727"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-11.40.54-AM-1024x466.png" alt="" width="1024" height="466" class="alignnone size-large wp-image-7727" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-11.40.54-AM-1024x466.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-11.40.54-AM-600x273.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-11.40.54-AM-300x136.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-11.40.54-AM-768x349.png 768w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-11.40.54-AM.png 1966w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>You will arrive a screen that should look something like this one.  I say should because 1) they like to change things around and 2) I already have some stuff going in my RDS setup. To create a database click  on &#8220;Create database&#8221;</p>
<p><a href="https://iotexpert.com/?attachment_id=7729" rel="attachment wp-att-7729"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-11.53.39-AM-1024x668.png" alt="" width="1024" height="668" class="alignnone size-large wp-image-7729" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-11.53.39-AM-1024x668.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-11.53.39-AM-600x392.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-11.53.39-AM-300x196.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-11.53.39-AM-768x501.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>&nbsp;</p>
<p>When you get to the create database screen it will give you some options.  Notice at the top of my screen shot they are already offering me a new user interface.  For the first database select:</p>
<ul>
<li>Easy Create</li>
<li>MySQL</li>
<li>Free Tier</li>
<li>DB instance identifier (I leave the default database-1)</li>
<li>Master username = admin</li>
<li>Autogenerate password</li>
</ul>
<p>Then press &#8220;Create database&#8221;</p>
<p><a href="https://iotexpert.com/?attachment_id=7730" rel="attachment wp-att-7730"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-11.57.10-AM-681x1024.png" alt="" width="681" height="1024" class="alignnone size-large wp-image-7730" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-11.57.10-AM-681x1024.png 681w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-11.57.10-AM-600x902.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-11.57.10-AM-199x300.png 199w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-11.57.10-AM-768x1155.png 768w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-11.57.10-AM.png 1588w" sizes="auto, (max-width: 681px) 100vw, 681px" /></a></p>
<p>Creating a database takes about 5 minutes.  In the screen shot below you can see that it is &#8220;Creating&#8221; and that I am already running two other databases.  Also you can see at the top of the screen it says &#8220;View credential details&#8221;.  This is where you find out the password that was automatically created for you.  If you leave this screen without the password your database becomes inaccessible and you will need to delete it.</p>
<p><a href="https://iotexpert.com/?attachment_id=7732" rel="attachment wp-att-7732"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.00.53-PM-1024x422.png" alt="" width="1024" height="422" class="alignnone size-large wp-image-7732" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.00.53-PM-1024x422.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.00.53-PM-600x247.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.00.53-PM-300x124.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.00.53-PM-768x316.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>When you click the details screen you will get something like this:</p>
<p><a href="https://iotexpert.com/?attachment_id=7733" rel="attachment wp-att-7733"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.01.52-PM-1024x544.png" alt="" width="1024" height="544" class="alignnone size-large wp-image-7733" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.01.52-PM-1024x544.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.01.52-PM-600x319.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.01.52-PM-300x159.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.01.52-PM-768x408.png 768w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.01.52-PM.png 1390w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Once the database is created your screen will look something like this:</p>
<p><a href="https://iotexpert.com/?attachment_id=7734" rel="attachment wp-att-7734"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.35.34-PM-1024x269.png" alt="" width="1024" height="269" class="alignnone size-large wp-image-7734" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.35.34-PM-1024x269.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.35.34-PM-600x158.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.35.34-PM-300x79.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.35.34-PM-768x202.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>When you click on database-1 (the one we just created) it will show you details about the database.  This screen has a bunch of useful information including the endpoint a.k.a the DNS name of your database.</p>
<p><a href="https://iotexpert.com/?attachment_id=7735" rel="attachment wp-att-7735"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.36.48-PM-1024x756.png" alt="" width="1024" height="756" class="alignnone size-large wp-image-7735" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.36.48-PM-1024x756.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.36.48-PM-600x443.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.36.48-PM-300x221.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.36.48-PM-768x567.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>Create MySQL WorkBench Connection</h1>
<p>I am not a real database administrator so I like to use the <a href="https://www.mysql.com/products/workbench/" target="_blank" rel="noopener noreferrer">MySQL Workbench GUI</a> to access my database.  To make a new connection, press the little plus next to MySQL Connections.</p>
<p><a href="https://iotexpert.com/?attachment_id=7740" rel="attachment wp-att-7740"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.57.18-PM-1024x623.png" alt="" width="1024" height="623" class="alignnone size-large wp-image-7740" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.57.18-PM-1024x623.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.57.18-PM-600x365.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.57.18-PM-300x182.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.57.18-PM-768x467.png 768w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.57.18-PM.png 2020w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>On this screen you need to provide the hostname, which in Amazon terms is the endpoint.  You also need to give the Username (which in my case was default admin) and the crazy generated password.</p>
<p><a href="https://iotexpert.com/?attachment_id=7738" rel="attachment wp-att-7738"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.53.53-PM-1024x641.png" alt="" width="1024" height="641" class="alignnone size-large wp-image-7738" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.53.53-PM-1024x641.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.53.53-PM-600x376.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.53.53-PM-300x188.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.53.53-PM-768x481.png 768w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.53.53-PM.png 1600w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>When I press the &#8220;Test Connection&#8221; I get this lovely message.</p>
<p><a href="https://iotexpert.com/?attachment_id=7741" rel="attachment wp-att-7741"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-1.02.02-PM.png" alt="" width="844" height="410" class="alignnone size-full wp-image-7741" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-1.02.02-PM.png 844w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-1.02.02-PM-600x291.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-1.02.02-PM-300x146.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-1.02.02-PM-768x373.png 768w" sizes="auto, (max-width: 844px) 100vw, 844px" /></a></p>
<p>The problem is that my database is not &#8220;Publicly available&#8221;  To fix this click on &#8220;Modify&#8221;</p>
<p><a href="https://iotexpert.com/?attachment_id=7742" rel="attachment wp-att-7742"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-1.03.49-PM-1024x292.png" alt="" width="1024" height="292" class="alignnone size-large wp-image-7742" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-1.03.49-PM-1024x292.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-1.03.49-PM-600x171.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-1.03.49-PM-300x85.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-1.03.49-PM-768x219.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Then scroll down to &#8220;Network and Security&#8221; and select &#8220;Public accessibility&#8221; and pick yes.</p>
<p><a href="https://iotexpert.com/?attachment_id=7736" rel="attachment wp-att-7736"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.41.58-PM-1024x678.png" alt="" width="1024" height="678" class="alignnone size-large wp-image-7736" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.41.58-PM-1024x678.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.41.58-PM-600x397.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.41.58-PM-300x199.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.41.58-PM-768x509.png 768w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.41.58-PM.png 1878w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Then scroll down some more and pick &#8220;Continue&#8221;</p>
<p><a href="https://iotexpert.com/?attachment_id=7743" rel="attachment wp-att-7743"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-1.05.16-PM-1024x477.png" alt="" width="1024" height="477" class="alignnone size-large wp-image-7743" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-1.05.16-PM-1024x477.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-1.05.16-PM-600x280.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-1.05.16-PM-300x140.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-1.05.16-PM-768x358.png 768w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-1.05.16-PM.png 1034w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>It will then ask you when?  Tell it NOW!!! right NOW!!! I can&#8217;t WAIT!!!  But seriously, it doesn&#8217;t matter because we don&#8217;t have anything in the database and no connections.</p>
<p><a href="https://iotexpert.com/?attachment_id=7737" rel="attachment wp-att-7737"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.42.16-PM-1024x809.png" alt="" width="1024" height="809" class="alignnone size-large wp-image-7737" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.42.16-PM-1024x809.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.42.16-PM-600x474.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.42.16-PM-300x237.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-12.42.16-PM-768x607.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>On my database this takes about a  minute&#8230; so be patient. I wasn&#8217;t and the connection didn&#8217;t work and I went looking to figure out why.  I finally realized that it was because it took a while to make the change.  Now when I test the connection it says:</p>
<p><a href="https://iotexpert.com/?attachment_id=7744" rel="attachment wp-att-7744"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-1.17.34-PM.png" alt="" width="824" height="502" class="alignnone size-full wp-image-7744" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-1.17.34-PM.png 824w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-1.17.34-PM-600x366.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-1.17.34-PM-300x183.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-1.17.34-PM-768x468.png 768w" sizes="auto, (max-width: 824px) 100vw, 824px" /></a></p>
<p>And when I open the connection it works.</p>
<p><a href="https://iotexpert.com/?attachment_id=7745" rel="attachment wp-att-7745"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-1.19.08-PM-1024x750.png" alt="" width="1024" height="750" class="alignnone size-large wp-image-7745" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-1.19.08-PM-1024x750.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-1.19.08-PM-600x439.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-1.19.08-PM-300x220.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-1.19.08-PM-768x562.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Now I can make database and a table.</p>
<p><a href="https://iotexpert.com/?attachment_id=7746" rel="attachment wp-att-7746"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-1.25.27-PM-1024x665.png" alt="" width="1024" height="665" class="alignnone size-large wp-image-7746" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-1.25.27-PM-1024x665.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-1.25.27-PM-600x389.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-1.25.27-PM-300x195.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-1.25.27-PM-768x498.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>Examining the Security</h1>
<p>A couple of things to notice about this database.  First, this database is setup to run on us-east-2a.  And that the database is in the &#8220;Default&#8221; subnet group which is either subnet-d41619bc, subnet-040ba648 or subnet-2b9edb51 (three subnets in the three availability zones in us-east-2).  For some reason which I can&#8217;t figure, they don&#8217;t display which subnet instead they make you figure it out by combining region and you knowledge of the subnets.</p>
<p><a href="https://iotexpert.com/?attachment_id=7750" rel="attachment wp-att-7750"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-6.25.48-PM-1024x817.png" alt="" width="1024" height="817" class="alignnone size-large wp-image-7750" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-6.25.48-PM-1024x817.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-6.25.48-PM-600x479.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-6.25.48-PM-300x239.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-6.25.48-PM-768x613.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>But wait is that subnet public or private?  And which one is it?  If you go to the AWS console for the VPCs and then click on the subnet tab you will find this configuration (at least in my VPC).  I did this work for the <a href="https://iotexpert.com/2019/07/29/amazon-aws-virtual-private-cloud-vpc/" target="_blank" rel="noopener noreferrer">article</a> I did on VPCs where I setup one private and one public subnet for each of the availability zone in the us-east-2.  From the screen above you can see that my RDS is setup in us-east-2a which means that it is on subnet-d41619bc.</p>
<p><a href="https://iotexpert.com/?attachment_id=7751" rel="attachment wp-att-7751"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-6.31.07-PM-1024x279.png" alt="" width="1024" height="279" class="alignnone size-large wp-image-7751" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-6.31.07-PM-1024x279.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-6.31.07-PM-600x163.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-6.31.07-PM-300x82.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-04-at-6.31.07-PM-768x209.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Notice that I gave that network the name us-east-2a-pub because it is a PUBLIC network.  Which you can see when you click on it.  Notice that the Route Table is Public.</p>
<p><a href="https://iotexpert.com/?attachment_id=7762" rel="attachment wp-att-7762"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-09-at-7.13.43-AM-1024x652.png" alt="" width="1024" height="652" class="alignnone size-large wp-image-7762" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-09-at-7.13.43-AM-1024x652.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-09-at-7.13.43-AM-600x382.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-09-at-7.13.43-AM-300x191.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-09-at-7.13.43-AM-768x489.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>When you click on &#8220;Route Table&#8221; you see that it has 0.0.0.0/0 sent to the Internet gateway named igw-9748c9ff</p>
<p><a href="https://iotexpert.com/?attachment_id=7764" rel="attachment wp-att-7764"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-09-at-7.13.50-AM-1024x431.png" alt="" width="1024" height="431" class="alignnone size-large wp-image-7764" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-09-at-7.13.50-AM-1024x431.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-09-at-7.13.50-AM-600x252.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-09-at-7.13.50-AM-300x126.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-09-at-7.13.50-AM-768x323.png 768w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-09-at-7.13.50-AM.png 1812w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>And that the Network ACL allows all traffic to and from the subnet.</p>
<p><a href="https://iotexpert.com/?attachment_id=7763" rel="attachment wp-att-7763"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-09-at-7.13.59-AM-1024x610.png" alt="" width="1024" height="610" class="alignnone size-large wp-image-7763" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-09-at-7.13.59-AM-1024x610.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-09-at-7.13.59-AM-600x357.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-09-at-7.13.59-AM-300x179.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-09-at-7.13.59-AM-768x457.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>Rethinking the Security &amp; Subnet Groups</h1>
<p>Having a MySQL database server directly connected to the public internet may not actually be such a good idea.  Whatever application you develop for sure wants to be able to connect to it, but do you really want the rest of the world hacking at it?  Probably not.  If the database server is attached to a private subnet that only servers that are inside of your VPC are allowed to attach to it.</p>
<p>How do you move a RDS from a public to a private subnet?  Well, unfortunately, there is no good way to do that (there is a way but just not very good) and you actually needed to get it into the correct subnet when you created the database.  But you might ask yourself, there was no place on any of those screens to setup the subnet.  And that is true.  BUT you can tell it which &#8220;subnet group&#8221; to attach to.  A subnet group is literally just a list of subnets with a name.  On the RDS console on the far right there is a link to subnet groups.  In my class the link says &#8220;Subnet Groups (2/50)&#8221;.  It sure seems like this tab should be on the VPC screen and I can&#8217;t think of any reason they wouldn&#8217;t have put it there.  But there it is.  When you click on the &#8220;Subnet Groups&#8230;&#8221;</p>
<p><a href="https://iotexpert.com/?attachment_id=7766" rel="attachment wp-att-7766"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-09-at-7.22.32-AM-1024x351.png" alt="" width="1024" height="351" class="alignnone size-large wp-image-7766" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-09-at-7.22.32-AM-1024x351.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-09-at-7.22.32-AM-600x206.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-09-at-7.22.32-AM-300x103.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-09-at-7.22.32-AM-768x263.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>You see that there are two subnet groups.  One called &#8220;default&#8221; and one called &#8220;test1&#8221; (which I created while I was making all of these screen shots).  If you click on default &#8230;</p>
<p><a href="https://iotexpert.com/?attachment_id=7767" rel="attachment wp-att-7767"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-09-at-7.24.56-AM-1024x279.png" alt="" width="1024" height="279" class="alignnone size-large wp-image-7767" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-09-at-7.24.56-AM-1024x279.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-09-at-7.24.56-AM-600x164.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-09-at-7.24.56-AM-300x82.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-09-at-7.24.56-AM-768x209.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>You will see that this group contains 3 subnets.  In fact this group was created automatically for you and contains ALL of the subnets in your VPC that were automatically created for you when the VPC was created.  Since that time I made some of them private which is the source of confusion.</p>
<p><a href="https://iotexpert.com/?attachment_id=7768" rel="attachment wp-att-7768"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-09-at-7.25.11-AM-1024x745.png" alt="" width="1024" height="745" class="alignnone size-large wp-image-7768" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-09-at-7.25.11-AM-1024x745.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-09-at-7.25.11-AM-600x437.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-09-at-7.25.11-AM-300x218.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-09-at-7.25.11-AM-768x559.png 768w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-09-at-7.25.11-AM.png 1978w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>In order to create a new subnet group you click on the button &#8220;Create DB Subnet Group&#8221;</p>
<p><a href="https://iotexpert.com/?attachment_id=7769" rel="attachment wp-att-7769"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-09-at-7.28.15-AM.png" alt="" width="554" height="148" class="alignnone size-full wp-image-7769" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-09-at-7.28.15-AM.png 554w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-09-at-7.28.15-AM-300x80.png 300w" sizes="auto, (max-width: 554px) 100vw, 554px" /></a></p>
<p>Then set things up:</p>
<ul>
<li>Named the group &#8220;private&#8221;</li>
<li>Made a short description</li>
<li>Clicked &#8220;Add all of the subnets in the group&#8221;</li>
<li>Then I removed the public ones.</li>
<li>Then press create</li>
</ul>
<p>Alternatively, you could just add the private ones by selecting the availability zone, then the private subnets.</p>
<p><a href="https://iotexpert.com/?attachment_id=7770" rel="attachment wp-att-7770"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-09-at-7.30.34-AM-744x1024.png" alt="" width="744" height="1024" class="alignnone size-large wp-image-7770" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-09-at-7.30.34-AM-744x1024.png 744w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-09-at-7.30.34-AM-600x826.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-09-at-7.30.34-AM-218x300.png 218w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-09-at-7.30.34-AM-768x1057.png 768w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-09-at-7.30.34-AM.png 1654w" sizes="auto, (max-width: 744px) 100vw, 744px" /></a></p>
<h1>Configure Security Groups</h1>
<p>The next thing that is goofy in security is that when I click on the VPC security groups I can see the security configuration for that subnet.</p>
<p><a href="https://iotexpert.com/?attachment_id=7772" rel="attachment wp-att-7772"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.31.16-PM-1024x464.png" alt="" width="1024" height="464" class="alignnone size-large wp-image-7772" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.31.16-PM-1024x464.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.31.16-PM-600x272.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.31.16-PM-300x136.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.31.16-PM-768x348.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>When I click on that security group you can see that the Amazon helped me by adding an Inbound rule to the security group to allow connections from 198.37.196.195 (which is the current IP address at my house) on port 3306.  In other words it poked a hole in the firewall that was limited to MySQL connections from my house&#8230; which I suppose is cool until my DHCP address changes.  Oh well.</p>
<p><a href="https://iotexpert.com/?attachment_id=7773" rel="attachment wp-att-7773"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.32.09-PM-1024x495.png" alt="" width="1024" height="495" class="alignnone size-large wp-image-7773" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.32.09-PM-1024x495.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.32.09-PM-600x290.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.32.09-PM-300x145.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.32.09-PM-768x371.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>Create the Database I Really Want</h1>
<p>OK lets create the database that we really want.  First, I will delete the database that I don&#8217;t want because there is not really any way to move it to another subnet.  Well, that actually isn&#8217;t true.  Apparently you can create a new VPC, transfer the RDS to the new VPC, then transfer it back to the original VPC, then delete the temporary VPC.  But that isn&#8217;t what I&#8217;m doing.</p>
<p>If you select the database, then select actions-&gt;delete.</p>
<p><a href="https://iotexpert.com/?attachment_id=7774" rel="attachment wp-att-7774"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.38.20-PM-1024x419.png" alt="" width="1024" height="419" class="alignnone size-large wp-image-7774" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.38.20-PM-1024x419.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.38.20-PM-600x245.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.38.20-PM-300x123.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.38.20-PM-768x314.png 768w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.38.20-PM.png 1908w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>It will ask you if you are SURE!!! Because there is no data in the database I turn off the final snapshot.  I acknowledge that Im really sure&#8230; and then press &#8220;delete me&#8221;</p>
<p><a href="https://iotexpert.com/?attachment_id=7775" rel="attachment wp-att-7775"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.38.40-PM-1024x998.png" alt="" width="1024" height="998" class="alignnone size-large wp-image-7775" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.38.40-PM-1024x998.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.38.40-PM-600x585.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.38.40-PM-300x292.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.38.40-PM-768x749.png 768w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.38.40-PM.png 1198w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Then it takes a bit of time to delete.</p>
<p><a href="https://iotexpert.com/?attachment_id=7776" rel="attachment wp-att-7776"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.39.00-PM-1024x246.png" alt="" width="1024" height="246" class="alignnone size-large wp-image-7776" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.39.00-PM-1024x246.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.39.00-PM-600x144.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.39.00-PM-300x72.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.39.00-PM-768x184.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Now I press Create database. Turn off easy create (so that I get access to the option to place the the new database in the correct subnet group.</p>
<p><a href="https://iotexpert.com/?attachment_id=7777" rel="attachment wp-att-7777"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.42.20-PM-875x1024.png" alt="" width="875" height="1024" class="alignnone size-large wp-image-7777" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.42.20-PM-875x1024.png 875w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.42.20-PM-600x702.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.42.20-PM-256x300.png 256w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.42.20-PM-768x899.png 768w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.42.20-PM.png 1548w" sizes="auto, (max-width: 875px) 100vw, 875px" /></a></p>
<p>Free tier is plenty good for this setup.  And I don&#8217;t really care what the name of the database is.  As before I&#8217;ll let it generate the password.</p>
<p><a href="https://iotexpert.com/?attachment_id=7778" rel="attachment wp-att-7778"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.42.39-PM-1024x932.png" alt="" width="1024" height="932" class="alignnone size-large wp-image-7778" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.42.39-PM-1024x932.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.42.39-PM-600x546.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.42.39-PM-300x273.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.42.39-PM-768x699.png 768w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.42.39-PM.png 1554w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>No choices on the instance size.</p>
<p><a href="https://iotexpert.com/?attachment_id=7779" rel="attachment wp-att-7779"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.42.52-PM-980x1024.png" alt="" width="980" height="1024" class="alignnone size-large wp-image-7779" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.42.52-PM-980x1024.png 980w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.42.52-PM-600x627.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.42.52-PM-287x300.png 287w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.42.52-PM-768x803.png 768w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.42.52-PM.png 1542w" sizes="auto, (max-width: 980px) 100vw, 980px" /></a></p>
<p>Finally in the connectivity section there is something interesting.  You need to expand the &#8220;additional connectivity configuration&#8221; to see these options.  Specifically, I can pick out the subnet group for this RDS instance.  Recall from above I created the private subnet group.  Pick it.</p>
<p><a href="https://iotexpert.com/?attachment_id=7780" rel="attachment wp-att-7780"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.47.48-PM-799x1024.png" alt="" width="799" height="1024" class="alignnone size-large wp-image-7780" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.47.48-PM-799x1024.png 799w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.47.48-PM-600x769.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.47.48-PM-234x300.png 234w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.47.48-PM-768x985.png 768w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.47.48-PM.png 1560w" sizes="auto, (max-width: 799px) 100vw, 799px" /></a></p>
<p>When I press create, I get this screen &#8230; sweet success.</p>
<p><a href="https://iotexpert.com/?attachment_id=7781" rel="attachment wp-att-7781"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.50.13-PM-1024x431.png" alt="" width="1024" height="431" class="alignnone size-large wp-image-7781" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.50.13-PM-1024x431.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.50.13-PM-600x252.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.50.13-PM-300x126.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.50.13-PM-768x323.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>And once again it creates credentials for me.</p>
<p><a href="https://iotexpert.com/?attachment_id=7782" rel="attachment wp-att-7782"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.50.04-PM-1024x555.png" alt="" width="1024" height="555" class="alignnone size-large wp-image-7782" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.50.04-PM-1024x555.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.50.04-PM-600x325.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.50.04-PM-300x163.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.50.04-PM-768x416.png 768w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-10-at-5.50.04-PM.png 1200w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Now I have &#8220;database-2&#8221; which is running in &#8220;us-east-2a&#8221;</p>
<p><a href="https://iotexpert.com/?attachment_id=7784" rel="attachment wp-att-7784"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-11-at-12.58.30-PM-1024x273.png" alt="" width="1024" height="273" class="alignnone size-large wp-image-7784" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-11-at-12.58.30-PM-1024x273.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-11-at-12.58.30-PM-600x160.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-11-at-12.58.30-PM-300x80.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-11-at-12.58.30-PM-768x205.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Click on database-2 and you can see that it is in the &#8220;private&#8221; subnet group.  If you look higher in this article you will find out that it MUST be running on subnet-0081c6f5eeaccdeaf.</p>
<p><a href="https://iotexpert.com/?attachment_id=7785" rel="attachment wp-att-7785"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-11-at-12.58.58-PM-1024x729.png" alt="" width="1024" height="729" class="alignnone size-large wp-image-7785" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-11-at-12.58.58-PM-1024x729.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-11-at-12.58.58-PM-600x427.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-11-at-12.58.58-PM-300x214.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-11-at-12.58.58-PM-768x547.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>When I click on that subnet I find that it is a private subnet in us-east-2a.  Notice that the route table is marked as &#8220;Private&#8221;</p>
<p><a href="https://iotexpert.com/?attachment_id=7787" rel="attachment wp-att-7787"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-11-at-1.03.47-PM-1024x541.png" alt="" width="1024" height="541" class="alignnone size-large wp-image-7787" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-11-at-1.03.47-PM-1024x541.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-11-at-1.03.47-PM-600x317.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-11-at-1.03.47-PM-300x158.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-11-at-1.03.47-PM-768x406.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>MySQL WorkBench EC2 Tunneling over SSL</h1>
<p>All that security is cool and everything.  But, How do I talk to the database?  Well, the answer to that question is that the RDS server is running in my VPC and any computer that is attached to that VPC can talk to the database server.   To make all of this work, I run an EC2 server in my VPC.  You can only attach to this server if you have the RSA keys.  But that still doesn&#8217;t answer the question how do I connect from my computer.   The answer is you need to do MySQL Tunneling over SSL.  To set this up in MySQL Workbench, first create a new connection.</p>
<ul>
<li>Pick the connection method as &#8220;Standard TCP/IP over SSH&#8221;</li>
<li>Set the SSH Hostname to be your EC2 Instance</li>
<li>Set the User (I have the default ubuntu)</li>
<li>Make a link to your keyfile</li>
<li>Give the DNS name of your RDS Server</li>
<li>The user name (remember from above it is admin)</li>
</ul>
<p><a href="https://iotexpert.com/?attachment_id=7788" rel="attachment wp-att-7788"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-11-at-1.14.15-PM-1024x671.png" alt="" width="1024" height="671" class="alignnone size-large wp-image-7788" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-11-at-1.14.15-PM-1024x671.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-11-at-1.14.15-PM-600x393.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-11-at-1.14.15-PM-300x197.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-11-at-1.14.15-PM-768x503.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Now when I test the connection&#8230; sweet success.</p>
<p><a href="https://iotexpert.com/?attachment_id=7789" rel="attachment wp-att-7789"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-11-at-1.14.28-PM.png" alt="" width="846" height="506" class="alignnone size-large wp-image-7789" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-11-at-1.14.28-PM.png 846w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-11-at-1.14.28-PM-600x359.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-11-at-1.14.28-PM-300x179.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-11-at-1.14.28-PM-768x459.png 768w" sizes="auto, (max-width: 846px) 100vw, 846px" /></a></p>
<p>And now I can talk to the MySQL server (and do whatever SQL stuff I want)</p>
<p><a href="https://iotexpert.com/?attachment_id=7790" rel="attachment wp-att-7790"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-11-at-1.14.48-PM-1024x670.png" alt="" width="1024" height="670" class="alignnone size-large wp-image-7790" srcset="https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-11-at-1.14.48-PM-1024x670.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-11-at-1.14.48-PM-600x393.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-11-at-1.14.48-PM-300x196.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/Screen-Shot-2019-08-11-at-1.14.48-PM-768x503.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>In the next article I will create a lambda function to send data onto the RDS database.</p>
<p><span><p><div class="table-responsive"><table  style="width:95%; "  class="easy-table easy-table-default " border="1">
<thead>
<tr><th >#</th>
<th >Title</th>
</tr>
</thead>
<tbody>
<tr><td >0</td>
<td ><a href="https://iotexpert.com/2019/07/08/the-creek-amazon-aws-iot-solution-architecture-2-0/" target="_blank" rel="noopener">The Creek 2.0: Amazon AWS IoT Solution Architecture 2.0</a></td>
</tr>

<tr><td >1</td>
<td ><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/" target="_blank" rel="noopener">The Creek 2.0: AWS IoT MQTT Message Broker</a></td>
</tr>

<tr><td >2</td>
<td ><a href="https://iotexpert.com/2019/07/22/the-creek-2-0-read-sensor-data-send-to-aws-iot-via-mqtt/" target="_blank" rel="noopener">The Creek 2.0: Read Sensor Data Send to AWS IoT via MQTT</a></td>
</tr>

<tr><td >3</td>
<td ><a href="https://iotexpert.com/2019/07/29/amazon-aws-virtual-private-cloud-vpc/" target="_blank" rel="noopener">Amazon AWS Virtual Private Cloud (VPC)</a></td>
</tr>

<tr><td >4</td>
<td ><a href="https://iotexpert.com/2019/08/12/the-creek-2-0-aws-relational-database-server-rds-mysql/" target="_blank" rel="noopener">The Creek 2.0: AWS Relational Database Server (RDS) - MySQL</a></td>
</tr>

<tr><td >5</td>
<td ><a href="https://iotexpert.com/2019/08/19/the-creek-2-0-aws-lambda-function/" target="_blank" rel="noopener">The Creek 2.0: AWS Lambda Function</a></td>
</tr>

<tr><td >6</td>
<td ><a href="https://iotexpert.com/the-creek-2-0-aws-iot-actions-rules/">The Creek 2.0: AWS IoT Actions &amp; Rules</a></td>
</tr>
</tbody></table></div></p></span></p>
]]></content:encoded>
					
					<wfw:commentRss>https://iotexpert.com/the-creek-2-0-aws-relational-database-server-rds-mysql/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>Amazon AWS Virtual Private Cloud (VPC)</title>
		<link>https://iotexpert.com/amazon-aws-virtual-private-cloud-vpc/</link>
					<comments>https://iotexpert.com/amazon-aws-virtual-private-cloud-vpc/#comments</comments>
		
		<dc:creator><![CDATA[Alan Hawse]]></dc:creator>
		<pubDate>Mon, 29 Jul 2019 12:33:09 +0000</pubDate>
				<category><![CDATA[AWS IoT]]></category>
		<guid isPermaLink="false">https://iotexpert.com/?p=7505</guid>

					<description><![CDATA[Summary In order to interact with AWS you need some basic understanding of how the Amazon Virtual Private Cloud (VPC) fits together.  I generally find that writing things down is a huge help in cementing my understanding of a topic.  For me, that is the point of this article, making sure that I understand how [&#8230;]]]></description>
										<content:encoded><![CDATA[<h1>Summary</h1>
<p>In order to interact with AWS you need some basic understanding of how the Amazon Virtual Private Cloud (VPC) fits together.  I generally find that writing things down is a huge help in cementing my understanding of a topic.  For me, that is the point of this article, making sure that I understand how the AWS VPC fits together.  I will preface all of this by saying that I am hardly an AWS networking expert so your mileage may vary but I hope that it helps you understand. For some reason, I mostly dug around inside of the AWS console to figure it out before I realized that there is a huge amount of <a href="https://docs.aws.amazon.com/vpc/latest/userguide/what-is-amazon-vpc.html" target="_blank" rel="noopener noreferrer">documentation</a> and tutorials out there.  At the end of this article I will link to the documentation etc. that I thought was useful.</p>
<p>The sections of this article are:</p>
<ul>
<li>Overview of AWS VPC Architecture</li>
<li>Region</li>
<li>VPC</li>
<li>Availability Zone</li>
<li>Subnet</li>
<li>Internet Gateway</li>
<li>Routing Table</li>
<li>Network ACLs</li>
<li>Security Group</li>
<li>Subnet group</li>
<li><span style="color: #ff0000;">A Stern Warning</span></li>
<li>Documentation and References</li>
</ul>
<h1>Overview of AWS VPC Architecture</h1>
<p>Amazon Web Services (AWS) is divided into 16 <span style="text-decoration: underline;"><strong>Regions</strong></span> (for now).  In any Region, you can create a Virtual Private Cloud (<span style="text-decoration: underline;"><strong>VPC</strong></span>) essentially a private network for you to attach AWS services to e.g. EC2 or RDS.  In each Region there are several <span style="text-decoration: underline;"><strong>Availability Zones</strong></span>, which you can think of as completely independent, physically separate, redundant computer rooms.  Although these Availability Zones are independent, they are also closely linked from a network standpoint.  A <span style="text-decoration: underline;"><strong>subnet</strong></span> is just a IP address range of a related group of servers that must fit completely in one unique Availability Zone.  In your VPC you should have at least one subnet per Availability Zone .   Each subnet is connected to your VPC by a <span style="text-decoration: underline;"><strong>routing table</strong></span> which can be shared by one or more subnets.  In other words, your subnets are connected together via routers and you control the routing tables. Your VPC can be connected to the public internet via up to one <span style="text-decoration: underline;"><strong>Internet Gateway</strong></span>.   In the routing table, you optionally specify a route to the public internet, which creates a <span style="text-decoration: underline;"><strong>public subnet</strong></span>.  If there is no route to the internet then the subnet is considered a <span style="text-decoration: underline;"><strong>private subnet</strong></span>.  Each subnet has an optional Network Access Control List (<span style="text-decoration: underline;"><strong>ACL</strong></span>) which allows you to secure that subnet by IP address and IP Port number.  A <span style="text-decoration: underline;"><strong>security group</strong></span> is an instance (server) level of access control &#8211; just like a ACL but on a server by server level.  It is called security group because you can apply the same list of rules to multiple servers.  Missing from the diagram is a <span style="text-decoration: underline;"><strong>subnet group</strong></span>.  A subnet group is a just a named list of subnets.  Subnet groups are used by some of the AWS systems e.g. RDS to choose which subnets to attach to.  I will talk in more details about them in the RDS article.</p>
<p>The picture below is a LOGICAL diagram to show how data flows inside of the VPC. You can see that I have two availability zones, each with a public and a private subnet.   Each subnet has it&#8217;s own routing table and network access control list.  And there is one Internet gateway which is routed to the two public subnets.  There are 8 servers which are attached two each of the subnets.  Each server has a security group.  The main point of all of this is that for devices to talk all of these things need to be configured correctly.</p>
<ul>
<li>Internet gateway</li>
<li>Routing tables</li>
<li>Network ACLs</li>
<li>Security Groups</li>
</ul>
<p><a href="https://iotexpert.com/2019/07/29/amazon-aws-virtual-private-cloud-vpc/vpc-pic-1-5/" rel="attachment wp-att-7523"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/vpc-pic-1-4-1024x622.png" alt="" width="1024" height="622" class="alignnone size-large wp-image-7523" srcset="https://iotexpert.com/wp-content/uploads/2019/07/vpc-pic-1-4-1024x622.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/vpc-pic-1-4-600x365.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/vpc-pic-1-4-300x182.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/vpc-pic-1-4-768x467.png 768w, https://iotexpert.com/wp-content/uploads/2019/07/vpc-pic-1-4.png 1637w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>I guess the good news is that when you create an AWS account it default configures all of this stuff to a semi-sensible starting point.</p>
<h1>Region</h1>
<p>When you create your AWS account Amazon will select a default region for you.  When you are logged into the console you can see your region in the upper right.  In this case my region is Ohio.</p>
<p><a href="https://iotexpert.com/2019/07/29/amazon-aws-virtual-private-cloud-vpc/screen-shot-2019-07-21-at-12-41-30-pm/" rel="attachment wp-att-7527"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-21-at-12.41.30-PM-1024x361.png" alt="" width="1024" height="361" class="alignnone size-large wp-image-7527" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-21-at-12.41.30-PM-1024x361.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-21-at-12.41.30-PM-600x212.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-21-at-12.41.30-PM-300x106.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-21-at-12.41.30-PM-768x271.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>When you click on the region you can see all of the available regions.  Currently, there are 16 of them.</p>
<p><a href="https://iotexpert.com/2019/07/29/amazon-aws-virtual-private-cloud-vpc/screen-shot-2019-07-21-at-12-41-42-pm/" rel="attachment wp-att-7528"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-21-at-12.41.42-PM-570x1024.png" alt="" width="570" height="1024" class="alignnone size-large wp-image-7528" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-21-at-12.41.42-PM-570x1024.png 570w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-21-at-12.41.42-PM-600x1077.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-21-at-12.41.42-PM-167x300.png 167w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-21-at-12.41.42-PM.png 654w" sizes="auto, (max-width: 570px) 100vw, 570px" /></a></p>
<p>Each region has different services available.  You can see the whole list <a href="https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services/" target="_blank" rel="noopener noreferrer">here</a>.  But this is a snapshot of the top of that page.</p>
<p><a href="https://iotexpert.com/2019/07/29/amazon-aws-virtual-private-cloud-vpc/screen-shot-2019-07-21-at-1-30-26-pm/" rel="attachment wp-att-7529"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-21-at-1.30.26-PM-1024x801.png" alt="" width="1024" height="801" class="alignnone size-large wp-image-7529" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-21-at-1.30.26-PM-1024x801.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-21-at-1.30.26-PM-600x469.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-21-at-1.30.26-PM-300x235.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-21-at-1.30.26-PM-768x601.png 768w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-21-at-1.30.26-PM.png 1764w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>Virtual Private Cloud (VPC)</h1>
<p>The Amazon marketing material has a nice description of the VPC. &#8220;Amazon Virtual Private Cloud (Amazon VPC) lets you provision a logically isolated section of the AWS Cloud where you can launch AWS resources in a virtual network that you define. You have complete control over your virtual networking environment, including selection of your own IP address range, creation of subnets, and configuration of route tables and network gateways. You can use both IPv4 and IPv6 in your VPC for secure and easy access to resources and applications.&#8221;</p>
<p>In any one region you can have up to 5 separate VPCs (at least without special $$s to Amazon).  Each VPC has a control console that lets you edit, update, and manage the configuration of your VPC.  You can get to it from the AWS management console.  Search for VPC.</p>
<p><a href="https://iotexpert.com/2019/07/29/amazon-aws-virtual-private-cloud-vpc/screen-shot-2019-07-21-at-1-48-34-pm/" rel="attachment wp-att-7533"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-21-at-1.48.34-PM-1024x526.png" alt="" width="1024" height="526" class="alignnone size-large wp-image-7533" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-21-at-1.48.34-PM-1024x526.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-21-at-1.48.34-PM-600x308.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-21-at-1.48.34-PM-300x154.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-21-at-1.48.34-PM-768x394.png 768w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-21-at-1.48.34-PM.png 1894w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>When you get there it should look something like the picture below.  Notice down the left side of the screen are the different sections to control the various attributes of your VPC.  In the picture you can see that I have 1 active VPC and it is in Ohio.  My VPC has 4 subnets, 2 routing tables, 1 internet gateway, 1 network ACL, 3 security groups etc.</p>
<p><a href="https://iotexpert.com/2019/07/29/amazon-aws-virtual-private-cloud-vpc/screen-shot-2019-07-21-at-1-48-55-pm/" rel="attachment wp-att-7534"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-21-at-1.48.55-PM-1024x754.png" alt="" width="1024" height="754" class="alignnone size-large wp-image-7534" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-21-at-1.48.55-PM-1024x754.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-21-at-1.48.55-PM-600x442.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-21-at-1.48.55-PM-300x221.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-21-at-1.48.55-PM-768x566.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>In the console when you click on &#8220;Your VPCs&#8221; you will see a screen that looks like the one below.  When you signed up, Amazon automatically created a VPC for you in the region that you selected. Which you probably didn&#8217;t realize &#8211; or at least I didn&#8217;t realize at the time.  The VPC console gives you information about your VPC including</p>
<ul>
<li>CIDR IPV4 Networking information (in CIDR Blocks)</li>
<li>DHCP Options</li>
<li>Routing Table (i.e. the default routing table)</li>
<li>Network ACLs</li>
</ul>
<p><a href="https://iotexpert.com/2019/07/29/amazon-aws-virtual-private-cloud-vpc/screen-shot-2019-07-22-at-11-38-32-am-2/" rel="attachment wp-att-7541"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-11.38.32-AM-1-1024x280.png" alt="" width="1024" height="280" class="alignnone size-large wp-image-7541" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-11.38.32-AM-1-1024x280.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-11.38.32-AM-1-600x164.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-11.38.32-AM-1-300x82.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-11.38.32-AM-1-768x210.png 768w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-11.38.32-AM-1.png 1671w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>If you want to create a new VPC the only information you really need is what network do you want to use.  When the original VPC was created by Amazon, the default picks are a <a href="https://en.wikipedia.org/wiki/Private_network" target="_blank" rel="noopener noreferrer">private network</a> range, 172.31.0.0/16.  I know that you can also create network in the 10.0.0.0. Your network must have 16-bits of network address.</p>
<p>If you decide to create your own VPC you will basically only need the <a href="https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing" target="_blank" rel="noopener noreferrer">IPv4 CIDR</a> (aka network) for your VPC.</p>
<p><a href="https://iotexpert.com/2019/07/29/amazon-aws-virtual-private-cloud-vpc/screen-shot-2019-07-22-at-12-22-50-pm/" rel="attachment wp-att-7542"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-12.22.50-PM-1024x373.png" alt="" width="1024" height="373" class="alignnone size-large wp-image-7542" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-12.22.50-PM-1024x373.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-12.22.50-PM-600x219.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-12.22.50-PM-300x109.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-12.22.50-PM-768x280.png 768w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-12.22.50-PM.png 1229w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>Availability Zone</h1>
<p>The AWS documentation says that  &#8220;&#8230; Availability Zones are the core of our infrastructure architecture and they form the foundation of AWS’s and customers’ reliability and operations. Availability Zones are designed for physical redundancy and provide resilience, enabling uninterrupted performance, even in the event of power outages, Internet downtime, floods, and other natural disasters.&#8221;</p>
<p>Each Region has several availability zone that are closely connected but isolated.  I am using the Ohio Region (aka us-east-2) which has three availability zones:</p>
<ul>
<li>us-east-2a</li>
<li>us-east-2b</li>
<li>us-east-2c</li>
</ul>
<p>There is no sub-menu for Availability Zones.  The way you control the Availability Zones is by creating subnets in the intended Availability Zone and then assigning resources to the intended subnet.  In order to control your subnets click on &#8220;Subnets&#8221;&#8230;</p>
<h1>Subnet</h1>
<p>&#8230; which will put you on a subnet screen that looks like this:</p>
<p><a href="https://iotexpert.com/2019/07/29/amazon-aws-virtual-private-cloud-vpc/screen-shot-2019-07-22-at-1-47-54-pm/" rel="attachment wp-att-7546"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-1.47.54-PM-1024x415.png" alt="" width="1024" height="415" class="alignnone size-large wp-image-7546" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-1.47.54-PM-1024x415.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-1.47.54-PM-600x243.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-1.47.54-PM-300x122.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-1.47.54-PM-768x311.png 768w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-1.47.54-PM.png 1519w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Notice that I have four subnets.  Three of them were created automatically for me by Amazon (the ones with the short Subnet IDs).  The &#8220;name&#8221; of the subnet is assigned by you, or in this case, were assigned by me.  When you hover over the name of the subnet it will put a little pencil icon on the name.  When you click it, you will be able to type a new name for the subnet.  The names don&#8217;t mean anything in the system.  They are for your use only when you are assigning resources etc.</p>
<p><a href="https://iotexpert.com/2019/07/29/amazon-aws-virtual-private-cloud-vpc/screen-shot-2019-07-22-at-2-55-51-pm/" rel="attachment wp-att-7548"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-2.55.51-PM.png" alt="" width="266" height="86" class="alignnone size-full wp-image-7548" /></a></p>
<p>When you create a subnet you will need to specify the network CIDR range.  Notice that all of mine are 20-bit network addresses starting with 172.31.0.0 and going up from there.  To add a new subnet click on &#8220;Create Subnet&#8221; where you will brought to a screen like this.  The two really interesting things on this screen are your ability to specify the network CIDR, and the Availability Zone.  A subnet must reside completely in one Availability Zone.</p>
<p><a href="https://iotexpert.com/2019/07/29/amazon-aws-virtual-private-cloud-vpc/screen-shot-2019-07-22-at-2-58-14-pm/" rel="attachment wp-att-7549"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-2.58.14-PM.png" alt="" width="914" height="541" class="alignnone size-full wp-image-7549" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-2.58.14-PM.png 914w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-2.58.14-PM-600x355.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-2.58.14-PM-300x178.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-2.58.14-PM-768x455.png 768w" sizes="auto, (max-width: 914px) 100vw, 914px" /></a></p>
<h1>Internet Gateway</h1>
<p>You can think of the internet gateway as a device that is attached to your VPC network.  Each of the subnets are allowed to route packets to the Internet Gateway.  When you configure the routing tables for your subnets, you will specify the internet gateway as the destination for packets that you want to go out onto the network e.g. 0.0.0.0/0 (meaning any device).  In terms of configuration, there isn&#8217;t much, just tags which are used only for your searching purposes.  The last thing of note with the internet gateway is that there can be only one attached to your VPC.</p>
<p><a href="https://iotexpert.com/2019/07/29/amazon-aws-virtual-private-cloud-vpc/screen-shot-2019-07-22-at-4-56-19-pm/" rel="attachment wp-att-7553"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-4.56.19-PM-1024x341.png" alt="" width="1024" height="341" class="alignnone size-large wp-image-7553" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-4.56.19-PM-1024x341.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-4.56.19-PM-600x200.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-4.56.19-PM-300x100.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-4.56.19-PM-768x256.png 768w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-4.56.19-PM.png 1069w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>Routing Table</h1>
<p>When you click on Routing Table, you will see a page like this one.  You can see in the picture that I have two routing tables.  The first one is called &#8220;Main&#8221; and is amazingly enough called the &#8220;Main&#8221; routing table, imagine that.  You can see the routes at the bottom of the picture.  The first one say that all of the 172.31.0.0/16 routes are local.  The second route says that any packets going to 0.0.0.0/0 (meaning any device on the network) should be sent to the internet gateway.</p>
<p><a href="https://iotexpert.com/2019/07/29/amazon-aws-virtual-private-cloud-vpc/screen-shot-2019-07-22-at-4-57-57-pm/" rel="attachment wp-att-7554"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-4.57.57-PM-1024x520.png" alt="" width="1024" height="520" class="alignnone size-large wp-image-7554" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-4.57.57-PM-1024x520.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-4.57.57-PM-600x305.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-4.57.57-PM-300x152.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-4.57.57-PM-768x390.png 768w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-4.57.57-PM.png 1118w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>This routing table probably should have been called the default routing table as it is by default attached to all of the subnets in your network.  When you click on the &#8220;Subnet associations&#8221; you can see that it is by default attached to all of the subnets in my network.  When a subnet is not explicitly attached to a network by you then it adopts the Main routing table.</p>
<p><a href="https://iotexpert.com/2019/07/29/amazon-aws-virtual-private-cloud-vpc/screen-shot-2019-07-22-at-5-01-58-pm-2/" rel="attachment wp-att-7557"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-5.01.58-PM-1-1024x723.png" alt="" width="1024" height="723" class="alignnone size-large wp-image-7557" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-5.01.58-PM-1-1024x723.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-5.01.58-PM-1-600x424.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-5.01.58-PM-1-300x212.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-5.01.58-PM-1-768x542.png 768w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-22-at-5.01.58-PM-1.png 1181w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>By definition any subnet that is attached to a routing table that has a route to the internet gateway is called &#8220;Public&#8221; and any subnet that doesn&#8217;t have a route to the internet is called &#8220;Private&#8221;.  Why would you want a private network?  Simple, imagine that a database server should only be accessed by servers that are in your VPC and should not be accessible by devices on the public internet.</p>
<p>If I wanted to create a &#8220;private&#8221; routing table I would first click create and then give the new table a name.  In this case &#8220;private&#8221;</p>
<p><a href="https://iotexpert.com/2019/07/29/amazon-aws-virtual-private-cloud-vpc/screen-shot-2019-07-23-at-5-54-21-am/" rel="attachment wp-att-7558"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-5.54.21-AM-1024x336.png" alt="" width="1024" height="336" class="alignnone size-large wp-image-7558" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-5.54.21-AM-1024x336.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-5.54.21-AM-600x197.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-5.54.21-AM-300x99.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-5.54.21-AM-768x252.png 768w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-5.54.21-AM.png 1954w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>After I click &#8220;Create&#8221; I will have the net routing table.  You can see it in the picture below.</p>
<p><a href="https://iotexpert.com/2019/07/29/amazon-aws-virtual-private-cloud-vpc/screen-shot-2019-07-23-at-5-54-41-am/" rel="attachment wp-att-7559"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-5.54.41-AM-1024x683.png" alt="" width="1024" height="683" class="alignnone size-large wp-image-7559" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-5.54.41-AM-1024x683.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-5.54.41-AM-600x400.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-5.54.41-AM-300x200.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-5.54.41-AM-768x513.png 768w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-5.54.41-AM.png 1786w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>When you click on the routes, you can see that be default it creates a &#8220;local&#8221; only route.  Giving this subnet access to only the local subnets.</p>
<p><a href="https://iotexpert.com/2019/07/29/amazon-aws-virtual-private-cloud-vpc/screen-shot-2019-07-23-at-5-54-55-am/" rel="attachment wp-att-7560"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-5.54.55-AM-1024x867.png" alt="" width="1024" height="867" class="alignnone size-large wp-image-7560" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-5.54.55-AM-1024x867.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-5.54.55-AM-600x508.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-5.54.55-AM-300x254.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-5.54.55-AM-768x650.png 768w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-5.54.55-AM.png 1786w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a> <a href="https://iotexpert.com/2019/07/29/amazon-aws-virtual-private-cloud-vpc/screen-shot-2019-07-23-at-5-55-30-am/" rel="attachment wp-att-7561"></a></p>
<p>By default a subnet is not associated with any routing table.  Which means that by default it uses the &#8220;Main&#8221; routing table. If you want to associate the subnet with a specific routing table then you click on the &#8220;Subnet Associates&#8221; tab then &#8220;Edit subnet associations&#8221;</p>
<p><a href="https://iotexpert.com/2019/07/29/amazon-aws-virtual-private-cloud-vpc/screen-shot-2019-07-23-at-5-55-30-am/" rel="attachment wp-att-7561"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-5.55.30-AM-909x1024.png" alt="" width="909" height="1024" class="alignnone size-large wp-image-7561" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-5.55.30-AM-909x1024.png 909w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-5.55.30-AM-600x676.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-5.55.30-AM-266x300.png 266w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-5.55.30-AM-768x865.png 768w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-5.55.30-AM.png 1768w" sizes="auto, (max-width: 909px) 100vw, 909px" /></a></p>
<p>&nbsp;</p>
<p>Now you can select a subnet to associate with the routing table, then &#8220;Save&#8221;</p>
<p><a href="https://iotexpert.com/2019/07/29/amazon-aws-virtual-private-cloud-vpc/screen-shot-2019-07-23-at-5-55-47-am/" rel="attachment wp-att-7562"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-5.55.47-AM-1024x542.png" alt="" width="1024" height="542" class="alignnone size-large wp-image-7562" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-5.55.47-AM-1024x542.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-5.55.47-AM-600x317.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-5.55.47-AM-300x159.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-5.55.47-AM-768x406.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>When it comes back to the main routing table page you can see that &#8220;private&#8221; is now associated with subnet &#8220;subnet-0081&#8230;&#8221;.  To bad that the interface doesn&#8217;t show the name of the subnet instead of the subnet id.</p>
<p><a href="https://iotexpert.com/2019/07/29/amazon-aws-virtual-private-cloud-vpc/screen-shot-2019-07-23-at-5-56-10-am/" rel="attachment wp-att-7563"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-5.56.10-AM-1024x874.png" alt="" width="1024" height="874" class="alignnone size-large wp-image-7563" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-5.56.10-AM-1024x874.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-5.56.10-AM-600x512.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-5.56.10-AM-300x256.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-5.56.10-AM-768x655.png 768w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-5.56.10-AM.png 1786w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>Network ACLs</h1>
<p>After the routing table, which limits outgoing traffic on a subnet, the next layer of security is the Network Access Control List (ACL or NACL).  The ACL is just a list of IP addresses/Port pairs that are legal (allowed) or illegal (deny).  You can think of the ACL as a firewall for the subnet.  When a packet is inbound or outbound from a subnet, the ACL rules are evaluated one by one, starting with the lowest number, and going until a rule is matched.  The final rule is a &#8220;deny&#8221; meaning, if there isn&#8217;t a match then the packet is by default a deny. Some features of the ACL include:</p>
<ul>
<li>NACLs are optional</li>
<li>NACLs are applied to 0 or more subnets (you can use the same NACL for more than one subnet)</li>
<li>There is a default NACL which is by default associated with every subnet</li>
<li>You can control access with BOTH routing tables and/or NACLs and/or Security Groups</li>
<li>There are separate inbound rules and outbound rules</li>
</ul>
<p>The default has ALLOW for everything on Inbound and Outbound &#8230; here is what it looks like in the control panel.</p>
<p><a href="https://iotexpert.com/2019/07/29/amazon-aws-virtual-private-cloud-vpc/screen-shot-2019-07-23-at-12-01-45-pm/" rel="attachment wp-att-7570"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-12.01.45-PM-1024x583.png" alt="" width="1024" height="583" class="alignnone size-large wp-image-7570" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-12.01.45-PM-1024x583.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-12.01.45-PM-600x342.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-12.01.45-PM-300x171.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-12.01.45-PM-768x437.png 768w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-12.01.45-PM.png 1834w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>You can add/edit/change the rules by clicking on Edit Inbound Rules.</p>
<p><a href="https://iotexpert.com/2019/07/29/amazon-aws-virtual-private-cloud-vpc/screen-shot-2019-07-23-at-12-10-07-pm/" rel="attachment wp-att-7572"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-12.10.07-PM-1024x358.png" alt="" width="1024" height="358" class="alignnone size-large wp-image-7572" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-12.10.07-PM-1024x358.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-12.10.07-PM-600x210.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-12.10.07-PM-300x105.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-12.10.07-PM-768x269.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>You need to specify a:</p>
<ul>
<li>Rule # which much be less than 32768 and is used to specify the execution order (lowest&#8211;&gt;highest)</li>
<li>The Rule #s should increase and you leave gaps so you can come back and add more</li>
<li>The Type &#8211; there is a big list of Types (which will automatically fill out the port) or you can completely specify it</li>
<li>The Source address</li>
</ul>
<p>It is tempting to make these rules very promiscuous.  Don&#8217;t do it.  You should make them as constrained as possible.</p>
<h1>Security Group</h1>
<p>The last level of security in the AWS VPC architecture is the Security Group.  A security group is an instance level firewall.  Meaning you can write inbound and outbound port level rules that apply to a SPECIFIC server instance in your system.  A security group can be applied to more than one server, meaning it can be generic to a function.  For example you might make a security group for MySQL servers that restricts all incoming connections to port 3306.  Every server instance in your VPC belongs to a security group by default when you create it.</p>
<p>The security groups has a a console in the VPC console.  In the picture below you can see that I have three security groups.  The interesting thing is that the first two security groups were created automatically by the Relational Database Server system when I made two MySQL databases.</p>
<p><a href="https://iotexpert.com/2019/07/29/amazon-aws-virtual-private-cloud-vpc/screen-shot-2019-07-23-at-12-48-28-pm/" rel="attachment wp-att-7577"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-12.48.28-PM-1024x412.png" alt="" width="1024" height="412" class="alignnone size-large wp-image-7577" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-12.48.28-PM-1024x412.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-12.48.28-PM-600x241.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-12.48.28-PM-300x121.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-12.48.28-PM-768x309.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>When I looked at all of this originally I wondered what is the difference between Security Group and NACL.  Amazon answers this question nicely in their <a href="https://docs.aws.amazon.com/vpc/latest/userguide/VPC_Security.html" target="_blank" rel="noopener noreferrer">documentation</a>:</p>
<p><a href="https://iotexpert.com/2019/07/29/amazon-aws-virtual-private-cloud-vpc/screen-shot-2019-07-23-at-12-44-08-pm/" rel="attachment wp-att-7575"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-12.44.08-PM-1024x679.png" alt="" width="1024" height="679" class="alignnone size-large wp-image-7575" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-12.44.08-PM-1024x679.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-12.44.08-PM-600x398.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-12.44.08-PM-300x199.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-12.44.08-PM-768x509.png 768w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-12.44.08-PM.png 1684w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>Subnet Group</h1>
<p>A subnet group is just a list of subnets from one up to the total list of subnets in your VPC.   The subnet group is NOT listed as an attribute of the VPC on the console.  However, it is used by the the Relational Database Server (RDS) setup screens.  When you create a new RDS MySQL database it will ask you which subnet group to assign the server to.  RDS will then pick one of the subnets and attach your server.  You need to think about the subnet groups in advance or you will end up with an RDS instance on the wrong subnet.</p>
<p>There is a default subnet group which has all of the subnets in your VPC at the time of creation.  It does NOT add subnets to your &#8220;default&#8221; subnet group when you add new subnets.</p>
<p>To edit the Subnet Groups you need to go to the Amazon RDS dashboard.  Then you click &#8220;Subnet groups&#8221;.  You can see in the picture below that I currently have 2 subnet groups.</p>
<p><a href="https://iotexpert.com/2019/07/29/amazon-aws-virtual-private-cloud-vpc/screen-shot-2019-07-23-at-2-16-38-pm/" rel="attachment wp-att-7580"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-2.16.38-PM-1024x702.png" alt="" width="1024" height="702" class="alignnone size-large wp-image-7580" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-2.16.38-PM-1024x702.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-2.16.38-PM-600x412.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-2.16.38-PM-300x206.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-2.16.38-PM-768x527.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>On the subnet group screen you can edit or create subnet groups.</p>
<p><a href="https://iotexpert.com/2019/07/29/amazon-aws-virtual-private-cloud-vpc/screen-shot-2019-07-23-at-2-23-23-pm/" rel="attachment wp-att-7583"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-2.23.23-PM-1024x263.png" alt="" width="1024" height="263" class="alignnone size-large wp-image-7583" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-2.23.23-PM-1024x263.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-2.23.23-PM-600x154.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-2.23.23-PM-300x77.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-2.23.23-PM-768x197.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>On the Edit screen you can Add or Remove subnets (or potentially filter the list to regions)</p>
<p><a href="https://iotexpert.com/2019/07/29/amazon-aws-virtual-private-cloud-vpc/screen-shot-2019-07-23-at-2-18-25-pm/" rel="attachment wp-att-7582"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-2.18.25-PM-787x1024.png" alt="" width="787" height="1024" class="alignnone size-large wp-image-7582" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-2.18.25-PM-787x1024.png 787w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-2.18.25-PM-600x781.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-2.18.25-PM-230x300.png 230w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-2.18.25-PM-768x1000.png 768w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-23-at-2.18.25-PM.png 1630w" sizes="auto, (max-width: 787px) 100vw, 787px" /></a></p>
<h1><span style="color: #ff0000;">A Stern Warning</span></h1>
<p>Although it seems like a terrible idea to put a section called &#8220;A Stern Warning&#8221; at the very end of a discussion, I did this because without understanding everything else the warning doesn&#8217;t make sense.</p>
<p>It is almost impossible to move servers between subnets once they are created.  That means <span style="color: #ff0000;">YOU HAD BETTER PLAN YOUR SUBNETS BEFORE YOU CREATE SERVERS</span> or you will find yourself roasting in <span style="color: #ff0000;"><strong>HELL</strong>.<span style="color: #000000;"> </span></span></p>
<p>I got lulled into a sense of security because Amazon did such a good job setting things up by default.  But, when I decided to have Public/Private subnets I already had servers turned on in subnets.  This made getting everything unwound a real pain in the ass.  On the internet there is quite a bit of conversation about how to move RDS MySQL servers and EC2 instances.  All of the options suck so it is better to design it right from the outset.  Imagine that.</p>
<h1>Documentation and References</h1>
<div class="table-responsive"><table  style="width:95%; "  class="easy-table easy-table-default " border="1">
<thead>
<tr><th >Resource</th>
<th >Notes</th>
</tr>
</thead>
<tbody>
<tr><td ><a href="https://docs.aws.amazon.com/vpc/latest/userguide/what-is-amazon-vpc.html" target="_blank" rel="noopener noreferrer">Amazon VPC Documentation</a></td>
<td ></td>
</tr>

<tr><td ><a href="https://docs.aws.amazon.com/vpc/latest/userguide/VPC_Security.html" target="_blank" rel="noopener noreferrer">Amazon VPC Security</a></td>
<td ></td>
</tr>

<tr><td ><a href="https://linuxacademy.com/amazon-web-services/training/course/name/aws-essentials-new" target="_blank" rel="noopener noreferrer">AWS Essentials Course Material</a></td>
<td ></td>
</tr>

<tr><td ><a href="https://www.youtube.com/playlist?list=PLv2a_5pNAko0Mijc6mnv04xeOut443Wnk" target="_blank" rel="noopener noreferrer">AWS Essentials YouTube</a></td>
<td ></td>
</tr>
</tbody></table></div>
<p><span><p><div class="table-responsive"><table  style="width:95%; "  class="easy-table easy-table-default " border="1">
<thead>
<tr><th >#</th>
<th >Title</th>
</tr>
</thead>
<tbody>
<tr><td >0</td>
<td ><a href="https://iotexpert.com/2019/07/08/the-creek-amazon-aws-iot-solution-architecture-2-0/" target="_blank" rel="noopener">The Creek 2.0: Amazon AWS IoT Solution Architecture 2.0</a></td>
</tr>

<tr><td >1</td>
<td ><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/" target="_blank" rel="noopener">The Creek 2.0: AWS IoT MQTT Message Broker</a></td>
</tr>

<tr><td >2</td>
<td ><a href="https://iotexpert.com/2019/07/22/the-creek-2-0-read-sensor-data-send-to-aws-iot-via-mqtt/" target="_blank" rel="noopener">The Creek 2.0: Read Sensor Data Send to AWS IoT via MQTT</a></td>
</tr>

<tr><td >3</td>
<td ><a href="https://iotexpert.com/2019/07/29/amazon-aws-virtual-private-cloud-vpc/" target="_blank" rel="noopener">Amazon AWS Virtual Private Cloud (VPC)</a></td>
</tr>

<tr><td >4</td>
<td ><a href="https://iotexpert.com/2019/08/12/the-creek-2-0-aws-relational-database-server-rds-mysql/" target="_blank" rel="noopener">The Creek 2.0: AWS Relational Database Server (RDS) - MySQL</a></td>
</tr>

<tr><td >5</td>
<td ><a href="https://iotexpert.com/2019/08/19/the-creek-2-0-aws-lambda-function/" target="_blank" rel="noopener">The Creek 2.0: AWS Lambda Function</a></td>
</tr>

<tr><td >6</td>
<td ><a href="https://iotexpert.com/the-creek-2-0-aws-iot-actions-rules/">The Creek 2.0: AWS IoT Actions &amp; Rules</a></td>
</tr>
</tbody></table></div></p></span></p>
]]></content:encoded>
					
					<wfw:commentRss>https://iotexpert.com/amazon-aws-virtual-private-cloud-vpc/feed/</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
			</item>
		<item>
		<title>The Creek 2.0: Read Sensor Data Send to AWS IoT via MQTT</title>
		<link>https://iotexpert.com/the-creek-2-0-read-sensor-data-send-to-aws-iot-via-mqtt/</link>
					<comments>https://iotexpert.com/the-creek-2-0-read-sensor-data-send-to-aws-iot-via-mqtt/#respond</comments>
		
		<dc:creator><![CDATA[Alan Hawse]]></dc:creator>
		<pubDate>Mon, 22 Jul 2019 12:00:13 +0000</pubDate>
				<category><![CDATA[AWS IoT]]></category>
		<category><![CDATA[Elkhorn Creek 2.0]]></category>
		<guid isPermaLink="false">https://iotexpert.com/?p=7381</guid>

					<description><![CDATA[Summary In this article I will show you how to use Python to read from the I2C bus and then send the data to the AWS IoT Cloud via MQTT.  This will include the steps to install the two required libraries.  I will follow these steps: Install the SMBUS Python Library Create pyGetData.py to test [&#8230;]]]></description>
										<content:encoded><![CDATA[<h1>Summary</h1>
<p>In this article I will show you how to use Python to read from the I2C bus and then send the data to the AWS IoT Cloud via MQTT.  This will include the steps to install the two required libraries.  I will follow these steps:</p>
<ul>
<li>Install the SMBUS Python Library</li>
<li>Create pyGetData.py to test the I2C</li>
<li>Install the AWS IoT Python Library</li>
<li>Create  pyGetData.py to send data to AWS IoT</li>
<li>Add the pyGetData.py to runI2C (which is run every 2 minutes)</li>
<li>Verify that everything is functioning</li>
</ul>
<h1>Install the SMBUS Python Library &amp; Test</h1>
<p>In order to have a Python program talk to the Raspberry Pi I2C you need to have the &#8220;python3-smbus&#8221; library installed.  To do this run &#8220;sudo apt-get install python3-smbus&#8221;</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c" ">pi@iotexpertpi:~ $ sudo apt-get install python3-smbus
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  python3-smbus
0 upgraded, 1 newly installed, 0 to remove and 426 not upgraded.
Need to get 0 B/9,508 B of archives.
After this operation, 58.4 kB of additional disk space will be used.
Selecting previously unselected package python3-smbus.
(Reading database ... 136023 files and directories currently installed.)
Preparing to unpack .../python3-smbus_3.1.1+svn-2_armhf.deb ...
Unpacking python3-smbus (3.1.1+svn-2) ...
Setting up python3-smbus (3.1.1+svn-2) ...
pi@iotexpertpi:~ $ 
</pre>
<p>I like to make sure that everything is working with the I2C bus.  There is a program called &#8220;i2cdetect&#8221; which can probe all of the I2C addresses on the bus.  It was already installed on my Raspberry Pi, but you can install it with &#8220;sudo apt-get install i2c-tools&#8221;.  There are two I2C busses in the system and the PSoC 4 is attached to bus &#8220;1&#8221;.  When I run &#8220;i2cdetect -y 1&#8221; I can see that address ox08 ACKs.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">pi@iotexpertpi:~/pyGetData $ i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- 08 -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --                         
pi@iotexpertpi:~/pyGetData $ 
</pre>
<p>You might recall from an earlier article that I setup the register map of the PSoC4 as follows:</p>
<pre class="start-line:5 EnlighterJSRAW" data-enlighter-language="c"">typedef  struct DataPacket {
    uint16 pressureCounts;
    int16 centiTemp; // temp in degree C / 100
    float depth;
    float temperature;
} __attribute__((packed)) DataPacket;
</pre>
<p>If I use the i2ctools to read some data from the PSoC4 like this:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">pi@iotexpertpi:~/pyGetData $ i2cget -y 1 8 0 w
0x0196
pi@iotexpertpi:~/pyGetData $</pre>
<p>I get 0x196 which is 406 in decimal.  In my ADC I have it setup as 12-bits into 0-2.048v which means that it is 0.5mv per count in other words the ADC is reading .204v which is about .204V/51.1 ohm = 4mA also knowns as 0 PSI.  OK that makes sense.</p>
<p>Now I create a program called testI2C.py.</p>
<pre class="lang:python decode:true ">import smbus

######################################################
#Read the data from the PSoC 4
######################################################
bus = smbus.SMBus(1)
address = 0x08

# The data structure in the PSOC 4 is:
# uint16_t pressureCount ; the adc-counts being read on the pressure sensor
# int16_t centiTemp ; the temperaure in 10ths of a degree C
# float depth ; four bytes float representing the depth in Feet
# float temperature ; four byte float representing the temperature in degrees C

numBytesInStruct = 12
block = bus.read_i2c_block_data(address, 0, numBytesInStruct)
print(block)
</pre>
<p>What I will do next is run the program to see what data it gets back from the Raspberry Pi.  Then I will use the i2ctools to get the same data and compare to make sure that things are working.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">pi@iotexpertpi:~/pyGetData $ python3 testI2C.py 
[150, 1, 236, 14, 0, 0, 0, 0, 204, 204, 24, 66]
pi@iotexpertpi:~/pyGetData $ i2cget -y 1 8 0 w
0x0196
pi@iotexpertpi:~/pyGetData $ 
</pre>
<p>Hang on 150,1 isn&#8217;t 0x0196.  Well yes it is because the data is in decimal and is little endian.  When you switch it to hex and display it the same way you get 0x0196 same answer.  Good.</p>
<p>The next problem is that a list of bytes isn&#8217;t really that useful and you need to convert it to an array of bytes using the function &#8220;bytearray&#8221;.  A bytearray also isn&#8217;t that helpful, but, Python has a library called &#8220;<a href="https://docs.python.org/3/library/struct.html" target="_blank" rel="noopener noreferrer">struct</a>&#8221; which can convert arrays of bytes into their equivalent values.  Think converting a packed  C-struct of bytes into the different fields.  You have to describe the struct using this ridiculous text format.</p>
<p><a href="https://iotexpert.com/?attachment_id=7477" rel="attachment wp-att-7477"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-12.46.27-PM-1003x1024.png" alt="" width="1003" height="1024" class="alignnone size-large wp-image-7477" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-12.46.27-PM-1003x1024.png 1003w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-12.46.27-PM-600x613.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-12.46.27-PM-294x300.png 294w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-12.46.27-PM-768x784.png 768w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-12.46.27-PM.png 1534w" sizes="auto, (max-width: 1003px) 100vw, 1003px" /></a></p>
<p>The first part of the code is as before.  The only really new things are:</p>
<ul>
<li>On line 20 I convert and array of bytes into a bytearray</li>
<li>On line 26 I unpack the byte array using the format string.  You can see in the table above &#8220;h&#8221; is a signed 16-bit int.  &#8220;H&#8221; is a unsigned 16-bit int. &#8220;f&#8221; is a four byte float.</li>
</ul>
<p>The unpack method turns the bytes into a tuple.  Here is the whole code.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">import struct
import smbus

######################################################
#Read the data from the PSoC 4
######################################################
bus = smbus.SMBus(1)
address = 0x08

# The data structure in the PSOC 4 is:
# uint16_t pressureCount ; the adc-counts being read on the pressure sensor
# int16_t centiTemp ; the temperaure in 10ths of a degree C
# float depth ; four bytes float representing the depth in Feet
# float temperature ; four byte float representing the temperature in degrees C

numBytesInStruct = 12
block = bus.read_i2c_block_data(address, 0, numBytesInStruct)
print(block)
# convert list of bytes returned from sensor into array of bytes
mybytes = bytearray(block)
# convert the byte array into
# H=Unsigned 16-bit int
# h=Signed 16-bit int
# f=Float 
# this function will return a tuple with pressureCount,centiTemp,depth,temperature
vals = struct.unpack_from('Hhff',mybytes,0)
# prints the tuple
print(vals)</pre>
<p>When I run the program I get the raw data.  Then the unpacked data.  Notice the 406 which is the same value from the ADC as earlier.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">pi@iotexpertpi:~/pyGetData $ python3 testI2C.py
[150, 1, 76, 14, 0, 0, 0, 0, 214, 71, 18, 66]
(406, 3660, 0.0, 36.570152282714844)
pi@iotexpertpi:~/pyGetData $ 
</pre>
<h1>Install the AWS IoT Python Library</h1>
<p>Now I want to send the data to AWS IoT using MQTT.  All the time I have been using Python I have been questioning my sanity as Python is an ugly ugly language.  However, one beautiful thing about Python is the huge library of code to do interesting things.  Amazon is no exception, they have built a Python library based on the <a href="https://github.com/eclipse/paho.mqtt.python" target="_blank" rel="noopener noreferrer">Eclipse Paho library</a>.  You can read about the library in the <a href="https://docs.aws.amazon.com/iot/latest/developerguide/iot-sdks.html#iot-python-sdk" target="_blank" rel="noopener noreferrer">documentation</a>.</p>
<p><a href="https://iotexpert.com/?attachment_id=7468" rel="attachment wp-att-7468"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-11.25.55-AM-1024x192.png" alt="" width="1024" height="192" class="alignnone size-large wp-image-7468" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-11.25.55-AM-1024x192.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-11.25.55-AM-600x112.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-11.25.55-AM-300x56.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-11.25.55-AM-768x144.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a> <a href="https://iotexpert.com/?attachment_id=7469" rel="attachment wp-att-7469"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-11.26.34-AM-1024x481.png" alt="" width="1024" height="481" class="alignnone size-large wp-image-7469" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-11.26.34-AM-1024x481.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-11.26.34-AM-600x282.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-11.26.34-AM-300x141.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-11.26.34-AM-768x361.png 768w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-11.26.34-AM.png 1834w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>To get this going I install using &#8220;sudo pip3&#8230;&#8221;</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c" ">pi@iotexpertpi:~ $ sudo pip3 install AWSIoTPythonSDK
Downloading/unpacking AWSIoTPythonSDK
  Downloading AWSIoTPythonSDK-1.4.7.tar.gz (79kB): 79kB downloaded
  Running setup.py (path:/tmp/pip-build-ajpr2imp/AWSIoTPythonSDK/setup.py) egg_info for package AWSIoTPythonSDK
    
Installing collected packages: AWSIoTPythonSDK
  Running setup.py install for AWSIoTPythonSDK
    
Successfully installed AWSIoTPythonSDK
Cleaning up...
pi@iotexpertpi:~ $ 
</pre>
<p>To use the library to connect to AWS you need to know your &#8220;endpoint&#8221;.  The endpoint is just the DNS name of the virtual server that Amazon setup for you.  This can be found on the AWS IoT management console.  You should click on the &#8220;Settings&#8221; on the left.  Then you will see the name at the top of the screen in the &#8220;Custom endpoint&#8221;</p>
<p><a href="https://iotexpert.com/?attachment_id=7478" rel="attachment wp-att-7478"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-1.11.32-PM-1024x607.png" alt="" width="1024" height="607" class="alignnone size-large wp-image-7478" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-1.11.32-PM-1024x607.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-1.11.32-PM-600x355.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-1.11.32-PM-300x178.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-1.11.32-PM-768x455.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>The next thing that you need is</p>
<ul>
<li>Your Thing Certificate (I hope you downloaded them when you had the chance)</li>
<li>Your Thing Private Key</li>
<li>The Amazon Root CA which you can get on this <a href="https://docs.aws.amazon.com/iot/latest/developerguide/managing-device-certs.html" target="_blank" rel="noopener noreferrer">page</a> You should choose &#8220;Amazon Root CA 1&#8221;</li>
</ul>
<p><a href="https://iotexpert.com/?attachment_id=7479" rel="attachment wp-att-7479"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-1.16.07-PM-1024x264.png" alt="" width="1024" height="264" class="alignnone size-large wp-image-7479" data-wp-editing="1" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-1.16.07-PM-1024x264.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-1.16.07-PM-600x155.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-1.16.07-PM-300x77.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-1.16.07-PM-768x198.png 768w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-1.16.07-PM.png 1078w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>The program is really simple.  On lines 7-13 I just setup variables with all of the configuration information.  Then I create a JSON message by concatenating all of the stuff together that I read from the PSoC 4.  Lines 18-20 setup an MQTT endpoint with your credentials.  Line 22 opens the MQTT connection.  And finally line 21 Publishes the message.</p>
<pre class="lang:python decode:true">from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient

######################################################
# Send Data to AWS
######################################################

host = "a1c0l0bpd6pon3-ats.iot.us-east-2.amazonaws.com"
rootCAPath = "../aws-keys/AmazonRootCA1.pem"
certificatePath = "../aws-keys/a083ad1cff-certificate.pem.crt"
privateKeyPath = "../aws-keys/a083ad1cff-private.pem.key"
port = 8883
clientId = "applecreek"
topic = "$aws/things/Test1/shadow/update"

# Shadow JSON Message formware
messageJson = '{"state":{"reported":{"temperature":' + str(vals[3]) +',"depth": ' + str(vals[2]) + ',"thing":"applecreek"}}}'

myAWSIoTMQTTClient = AWSIoTMQTTClient(clientId)
myAWSIoTMQTTClient.configureEndpoint(host, port)
myAWSIoTMQTTClient.configureCredentials(rootCAPath, privateKeyPath, certificatePath)

myAWSIoTMQTTClient.connect()
myAWSIoTMQTTClient.publish(topic, messageJson, 1)
</pre>
<p>Now that I have the Python program, I want to plumb it into the rest of my stuff.  On my RPI I run &#8220;crontab -l&#8221; to figure out what my collect data program is.  That turns out to be &#8220;runI2C&#8221; which appears to run every two minutes.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58 * * * * /home/pi/getCreek/runi2c
</pre>
<p>I edit the runI2C shell script and add on my python program.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c" ">#!/bin/sh
cd ~pi/getCreek
sudo java -cp build/jar/getCreek.jar:classes:./lib/* CreekServer GetData
cd ~pi/pyGetData
python3 pyGetData.py</pre>
<p>Finally we are ready for the moment of truth.  Log into the console and start the test client.  Subscribe to &#8220;#&#8221; and after a bit of time I see that my publish happened and it was accepted into the Device Shadow of my Thing.</p>
<p><a href="https://iotexpert.com/?attachment_id=7481" rel="attachment wp-att-7481"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-1.38.45-PM-1024x656.png" alt="" width="1024" height="656" class="alignnone size-large wp-image-7481" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-1.38.45-PM-1024x656.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-1.38.45-PM-600x384.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-1.38.45-PM-300x192.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-1.38.45-PM-768x492.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Here is the device shadow</p>
<p><a href="https://iotexpert.com/?attachment_id=7482" rel="attachment wp-att-7482"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-1.39.11-PM-1024x603.png" alt="" width="1024" height="603" class="alignnone size-large wp-image-7482" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-1.39.11-PM-1024x603.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-1.39.11-PM-600x353.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-1.39.11-PM-300x177.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-1.39.11-PM-768x452.png 768w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-1.39.11-PM.png 2038w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p><span><p><div class="table-responsive"><table  style="width:95%; "  class="easy-table easy-table-default " border="1">
<thead>
<tr><th >#</th>
<th >Title</th>
</tr>
</thead>
<tbody>
<tr><td >0</td>
<td ><a href="https://iotexpert.com/2019/07/08/the-creek-amazon-aws-iot-solution-architecture-2-0/" target="_blank" rel="noopener">The Creek 2.0: Amazon AWS IoT Solution Architecture 2.0</a></td>
</tr>

<tr><td >1</td>
<td ><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/" target="_blank" rel="noopener">The Creek 2.0: AWS IoT MQTT Message Broker</a></td>
</tr>

<tr><td >2</td>
<td ><a href="https://iotexpert.com/2019/07/22/the-creek-2-0-read-sensor-data-send-to-aws-iot-via-mqtt/" target="_blank" rel="noopener">The Creek 2.0: Read Sensor Data Send to AWS IoT via MQTT</a></td>
</tr>

<tr><td >3</td>
<td ><a href="https://iotexpert.com/2019/07/29/amazon-aws-virtual-private-cloud-vpc/" target="_blank" rel="noopener">Amazon AWS Virtual Private Cloud (VPC)</a></td>
</tr>

<tr><td >4</td>
<td ><a href="https://iotexpert.com/2019/08/12/the-creek-2-0-aws-relational-database-server-rds-mysql/" target="_blank" rel="noopener">The Creek 2.0: AWS Relational Database Server (RDS) - MySQL</a></td>
</tr>

<tr><td >5</td>
<td ><a href="https://iotexpert.com/2019/08/19/the-creek-2-0-aws-lambda-function/" target="_blank" rel="noopener">The Creek 2.0: AWS Lambda Function</a></td>
</tr>

<tr><td >6</td>
<td ><a href="https://iotexpert.com/the-creek-2-0-aws-iot-actions-rules/">The Creek 2.0: AWS IoT Actions &amp; Rules</a></td>
</tr>
</tbody></table></div></p></span></p>
<p>Here is the whole program</p>
<pre class="lang:python decode:true">import struct
import sys
import smbus
from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient

######################################################
#Read the data from the PSoC 4
######################################################
bus = smbus.SMBus(1)
address = 0x08

# The data structure in the PSOC 4 is:
# uint16_t pressureCount ; the adc-counts being read on the pressure sensor
# int16_t centiTemp ; the temperaure in 10ths of a degree C
# float depth ; four bytes float representing the depth in Feet
# float temperature ; four byte float representing the temperature in degrees C

numBytesInStruct = 12
block = bus.read_i2c_block_data(address, 0, numBytesInStruct)

# convert list of bytes returned from sensor into array of bytes
mybytes = bytearray(block)
# convert the byte array into
# H=Unsigned 16-bit int
# h=Signed 16-bit int
# f=Float 
# this function will return a tuple with pressureCount,centiTemp,depth,temperature
vals = struct.unpack_from('Hhff',mybytes,0)
# prints the tuple
print(vals)

######################################################
# Send Data to AWS
######################################################

host = "a1c0l0bpd6pon3-ats.iot.us-east-2.amazonaws.com"
rootCAPath = "../aws-keys/AmazonRootCA1.pem"
certificatePath = "../aws-keys/a083ad1cff-certificate.pem.crt"
privateKeyPath = "../aws-keys/a083ad1cff-private.pem.key"
port = 8883
clientId = "applecreek"
topic = "$aws/things/applecreek/shadow/update"

# Shadow JSON Message formware
messageJson = '{"state":{"reported":{"temperature":' + str(vals[3]) +',"depth": ' + str(vals[2]) + ',"thing":"applecreek"}}}'

myAWSIoTMQTTClient = AWSIoTMQTTClient(clientId)
myAWSIoTMQTTClient.configureEndpoint(host, port)
myAWSIoTMQTTClient.configureCredentials(rootCAPath, privateKeyPath, certificatePath)

myAWSIoTMQTTClient.connect()
myAWSIoTMQTTClient.publish(topic, messageJson, 1)
</pre>
<p>&nbsp;</p>
]]></content:encoded>
					
					<wfw:commentRss>https://iotexpert.com/the-creek-2-0-read-sensor-data-send-to-aws-iot-via-mqtt/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>The Creek 2.0: AWS IoT MQTT Message Broker</title>
		<link>https://iotexpert.com/the-creek-2-0-aws-iot-mqtt-message-broker/</link>
					<comments>https://iotexpert.com/the-creek-2-0-aws-iot-mqtt-message-broker/#respond</comments>
		
		<dc:creator><![CDATA[Alan Hawse]]></dc:creator>
		<pubDate>Mon, 15 Jul 2019 12:00:40 +0000</pubDate>
				<category><![CDATA[AWS IoT]]></category>
		<category><![CDATA[Elkhorn Creek 2.0]]></category>
		<guid isPermaLink="false">https://iotexpert.com/?p=7386</guid>

					<description><![CDATA[Summary In this article I will explain the fundamentals of the Amazon Web Service IoT Device Cloud.  I will show you how to: Create a &#8220;Thing&#8221; in the AWS IoT Core Create and attach secret keys in the form of a X.509 Certificate Create and attach an access Policy to the Certificate Publish and Subscribe use [&#8230;]]]></description>
										<content:encoded><![CDATA[<h1>Summary</h1>
<p>In this article I will explain the fundamentals of the Amazon Web Service IoT Device Cloud.  I will show you how to:</p>
<ul>
<li>Create a &#8220;<span style="text-decoration: underline;"><strong>Thing</strong></span>&#8221; in the AWS IoT Core</li>
<li>Create and attach secret keys in the form of a X.509 <span style="text-decoration: underline;"><strong>Certificate</strong></span></li>
<li>Create and attach an access <span style="text-decoration: underline;"><strong>Policy</strong></span> to the Certificate</li>
<li>Publish and Subscribe use a Message Queuing Telemetry Transport (<span style="text-decoration: underline;"><strong>MQTT</strong></span>) Message Broker (that Amazon creates for you)</li>
<li>Use MQTT to update the cached &#8220;state&#8221; of your device, also called the <span style="text-decoration: underline;"><strong>Device Shadow</strong></span></li>
</ul>
<p>There are 5 fundamental concepts that you need in order to understand the AWS IoT system, specifically, Thing, Certificate, Policy, MQTT and Device Shadow.</p>
<p>A <span style="text-decoration: underline;"><strong>Thing</strong></span> is Amazon&#8217;s word for some device out in the world that attaches to the AWS IoT cloud.  In my case, Thing means the Elkhorn Creek in Georgetown, Kentucky.  But, it could be a garage door, dishwasher or whatever other ridiculous thing you want to connect to the internet.  The AWS IoT Cloud allows you to create a Thing, setup and manage security, receive data from it, send data to it, and keep track of its state.  In my case the state is the water level of the Creek and the temperature in my barn.</p>
<p>A <strong></strong><span style="text-decoration: underline;"><strong>Certificate</strong></span> is an <a href="https://en.wikipedia.org/wiki/X.509" target="_blank" rel="noopener noreferrer">X.509</a> document that has a signed public key of the Thing.  When you use the Amazon IoT Console to create a Thing,  you can also create a Certificate for the Thing, the private key that goes with the public key in the Certificate, as well as a copy of the public key that is embedded in the Certificate.  In order to create a TLS connection to AWS IoT you will need to use the Certificate as Amazon AWS does &#8220;double sided&#8221; TLS connections.  In other words you must verify Amazon and Amazon must verify you.  You will also need your private key in order to decrypt data that Amazon sends to you encrypted with your public key.  Amazon uses the Certificate to uniquely identify a specific Thing.</p>
<p>A <span style="text-decoration: underline;"><strong>Policy</strong></span> is a JSON document that is attached to a Certificate that specifies what &#8220;IoT Actions&#8221; your Thing is allowed to take and to which resources that it is allowed to take the action upon.  Actions include Connect, Subscribe, Publish etc.  All resources in the world of Amazon have an ARN (Amazon Resource Name), so in the Policy you specify what actions can happen to what ARNs.</p>
<p><span style="text-decoration: underline;"><strong>MQTT</strong></span> stands for <a href="http://mqtt.org" target="_blank" rel="noopener noreferrer">Message Queuing Telemetry Transport</a> and is an IoT protocol for a Thing to Publish messages to a Message Broker Topic.  A Message Broker is TCP/IP server that is running in the AWS IoT Cloud that Amazon creates for you and automatically turns on.  A Topic is just a name which you create that serves as a way to identify message channels.  In addition to Publishing messages to a Topic, a client can also Subscribe to a Topic.  In other words a Thing can Publish to any topic and any Thing can Subscribe to any Topic.  This you can create a many too many relationship for Publishing/Subscribing to message.  There are some topics which have special meaning in the world of AWS IoT and are used for updating and monitoring Thing state stored which is stored in the Device Shadow.</p>
<p>A <span style="text-decoration: underline;"><strong>Device Shadow</strong></span> is just a JSON document that is cached in the AWS IoT Cloud and is used to represent the Desired and Reported state of a Thing.  This allows other devices in the AWS IoT Cloud to communicate with a Thing even if it is not currently connected.  The JSON Device Shadow is just a JSON key value map which is defined by YOUR application.  Amazon doesn&#8217;t care what keys or values you use.  In my case the keys are &#8220;temperature&#8221; and &#8220;depth&#8221;.  When my Thing finds new values for the state of those two variables it will send updates to the Device Shadow via MQTT.</p>
<p>Amazon has pretty good documentation of how all of this fits together <a href="https://docs.aws.amazon.com/iot/latest/developerguide/what-is-aws-iot.html" target="_blank" rel="noopener noreferrer">here</a>.  One thing to note is that Amazon changes the screens on this system all of the damn time.  In my experience the changes are not major, but my screen shots may or may not reflect the current state of AWS.  Actually, there will almost certainly be some differences, but I can&#8217;t help that.  Please email bezos@amazon.com if don&#8217;t like it.</p>
<p>Here are the steps I will follow in this Article to show you this whole thing:</p>
<ul>
<li>Create an AWS IoT Account</li>
<li>AWS IoT Core Console Tour</li>
<li>Create a Thing &amp; Certificate</li>
<li>Create a Policy and Attach it to the Certificate</li>
<li>Explain MQTT &amp; Show the Test Client</li>
<li>Explain the Device Shadow</li>
<li>Update the Shadow Using the Test Client</li>
</ul>
<h1>Create an AWS IoT Account</h1>
<p>In order to use all of this, you will need to create an AWS IoT Account.  You can do that at <a href="https://console.aws.amazon.com" target="_blank" rel="noopener noreferrer">https://console.aws.amazon.com</a>.  Obviously Amazon makes all of their profit from AWS, however, for small amounts of usage, it is essentially free to use.  You will need to provide a credit card when you set this up, but for every thing that I have done, I have used &lt;$10.  So no big deal.</p>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-9-31-09-am/" rel="attachment wp-att-7398"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-9.31.09-AM-1024x698.png" alt="" width="1024" height="698" class="alignnone size-large wp-image-7398" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-9.31.09-AM-1024x698.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-9.31.09-AM-600x409.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-9.31.09-AM-300x204.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-9.31.09-AM-768x523.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>When you click on Create a new account it will bring you to this screen.  This will be a different account (even if it has the same password as your Amazon commercial account).</p>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-9-31-24-am/" rel="attachment wp-att-7399"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-9.31.24-AM-1024x458.png" alt="" width="1024" height="458" class="alignnone size-large wp-image-7399" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-9.31.24-AM-1024x458.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-9.31.24-AM-600x268.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-9.31.24-AM-300x134.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-9.31.24-AM-768x344.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Once you have an account you will end up on a Screen that looks like this.  You can see that I have recently been using all of the services that I am talking about.  Imagine that.  For this lesson we will focus on IoT Core, but in the future lessons Ill talk about other services.  You can get to IoT Core by typing IoT Core into the search box and the clicking it.</p>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-9-29-44-am/" rel="attachment wp-att-7396"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-9.29.44-AM-1024x718.png" alt="" width="1024" height="718" class="alignnone size-large wp-image-7396" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-9.29.44-AM-1024x718.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-9.29.44-AM-600x421.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-9.29.44-AM-300x210.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-9.29.44-AM-768x538.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>There is actually a bunch of good documentation (which you can see near the bottom of the screen) including tutorials (obviously none of them are as good as this one)</p>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-9-29-52-am/" rel="attachment wp-att-7397"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-9.29.52-AM-1024x530.png" alt="" width="1024" height="530" class="alignnone size-large wp-image-7397" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-9.29.52-AM-1024x530.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-9.29.52-AM-600x311.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-9.29.52-AM-300x155.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-9.29.52-AM-768x398.png 768w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-9.29.52-AM.png 1920w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>AWS IoT Core Console Tour</h1>
<p>Once you click on IoT Core, you will end up on a screen like this one.  It shows how much activity is going on in my account (basically not very much).  On the left side of the screen are all of the functions that we will use in this tutorial.</p>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-9-47-48-am/" rel="attachment wp-att-7401"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-9.47.48-AM-1024x703.png" alt="" width="1024" height="703" class="alignnone size-large wp-image-7401" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-9.47.48-AM-1024x703.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-9.47.48-AM-600x412.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-9.47.48-AM-300x206.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-9.47.48-AM-768x527.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p><span style="text-decoration: underline;"><strong>Monitor</strong></span> shows the screen shown above and gives you top level statistics about what is going on in your Cloud.</p>
<p><span style="text-decoration: underline;"><strong>Onboard</strong></span> is a set of new tools to help you attach devices to your AWS IoT Cloud (I have not used any of them)</p>
<p><span style="text-decoration: underline;"><strong>Manage</strong></span> allows you to create, delete, modify all of your Things (we will do quite a bit of this)</p>
<p><span style="text-decoration: underline;"><strong>Greegrass</strong></span> is a tool that allows you to have a local &#8220;server&#8221; that all of your things attach to.  I have not used it as of yet, but will in the future.</p>
<p>The <span style="text-decoration: underline;"><strong>Secure</strong></span> menu give you access to all of your Certificates and Policies.</p>
<p><span style="text-decoration: underline;"><strong>Defend</strong></span> gives you access to tools to monitor and defend your IoT network as the Russians, Chinese and CIA are all trying to get into your network.</p>
<p>The <span style="text-decoration: underline;"><strong>Act</strong></span> screen allows you to create Rules to do stuff based on things happening in the world of your MQTT Message Broker.  In a future article I will show you how to Act on an MQTT message to run an Amazon Lambda Function.</p>
<p><span style="text-decoration: underline;"><strong>Test</strong></span> starts up a REALLY cool web based MQTT test tool that will allow you to Publish and Subscribe to messages that are flying around on your MQTT broker.</p>
<h1>Create a Thing &amp; Certificates</h1>
<p>Amazon has some pretty decent documentation which shows you how to create and manage things which you can find <a href="https://docs.aws.amazon.com/iot/latest/developerguide/register-device.html" target="_blank" rel="noopener noreferrer">here</a>.</p>
<p>Finally, we are ready to actually do something.  Specifically we will create a &#8220;Thing&#8221; to represent the water level in the Elkhorn Creek.  Click on Manage -&gt; Things.  You can see in the picture below that I already have two devices in my Thing cloud, applecreek and Test1.  Press &#8220;Create&#8221; to start the process of creating  new Thing.</p>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-11-18-29-am/" rel="attachment wp-att-7404"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.18.29-AM-1024x467.png" alt="" width="1024" height="467" class="alignnone size-large wp-image-7404" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.18.29-AM-1024x467.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.18.29-AM-600x274.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.18.29-AM-300x137.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.18.29-AM-768x350.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Obviously, Amazon designed this whole system to be able to handle boatloads of Things, so they provide the ability to create many things, both in the GUI as well as with the command line.  But to learn the process we will create a single thing using the web gui.  Press &#8220;Create a single thing&#8221;</p>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-11-18-41-am/" rel="attachment wp-att-7405"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.18.41-AM-1024x520.png" alt="" width="1024" height="520" class="alignnone size-large wp-image-7405" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.18.41-AM-1024x520.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.18.41-AM-600x305.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.18.41-AM-300x152.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.18.41-AM-768x390.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Give you Thing a name (yes there are tons of bad jokes which could be done here).  I will call my example Thing &#8220;Test2&#8221;.  Then press &#8220;Next&#8221;</p>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-11-19-09-am/" rel="attachment wp-att-7406"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.19.09-AM-982x1024.png" alt="" width="982" height="1024" class="alignnone size-large wp-image-7406" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.19.09-AM-982x1024.png 982w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.19.09-AM-600x626.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.19.09-AM-288x300.png 288w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.19.09-AM-768x801.png 768w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.19.09-AM.png 2012w" sizes="auto, (max-width: 982px) 100vw, 982px" /></a></p>
<p>In order for you Thing to connect to the network it needs to have a Certificate attached to it.  The certificate documentation is <a href="https://docs.aws.amazon.com/iot/latest/developerguide/create-device-certificate.html" target="_blank" rel="noopener noreferrer">here</a>.  It is possible to use your own certificates or have Amazon sign your certificates.  However, we will do the simple thing and let Amazon create the Certificate for us.  Press &#8220;Create certificate&#8221;</p>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-11-19-25-am/" rel="attachment wp-att-7407"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.19.25-AM-1024x674.png" alt="" width="1024" height="674" class="alignnone size-large wp-image-7407" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.19.25-AM-1024x674.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.19.25-AM-600x395.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.19.25-AM-300x197.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.19.25-AM-768x505.png 768w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.19.25-AM.png 2046w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Once the Certificate is created you will come to this screen.  In order to use the Certificate on your Thing you will need to download it as well as the private/public key pair.  You should take the opportunity to down these NOW.  Once that is done press &#8220;Activate&#8221; to turn on the Certificate.</p>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-11-19-37-am/" rel="attachment wp-att-7408"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.19.37-AM-1024x700.png" alt="" width="1024" height="700" class="alignnone size-large wp-image-7408" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.19.37-AM-1024x700.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.19.37-AM-600x410.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.19.37-AM-300x205.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.19.37-AM-768x525.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Once you have activated the certificate you get your LAST!!! chance to download the certificates.  If you do not download them, then you will need to delete them and create a new set.  You should be careful where you store the keys on  your local device as they will give bad actors the ability to access your Things.   If you look around on GitHub it will be common to find them, so be careful.  Press &#8220;Done&#8221; to move to the next screen.</p>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-11-19-53-am/" rel="attachment wp-att-7409"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.19.53-AM-1024x687.png" alt="" width="1024" height="687" class="alignnone size-large wp-image-7409" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.19.53-AM-1024x687.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.19.53-AM-600x402.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.19.53-AM-300x201.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.19.53-AM-768x515.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>After you have created a device your screen will look something like this.  You can see that I already created several Things which I called &#8220;applecreek&#8221; (the Thing that is in production on my real system.  Now that you have &#8220;Test2&#8221; we can look at it to see some of the properties.  Click &#8220;Test2&#8221;</p>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-3-16-14-pm/" rel="attachment wp-att-7451"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-3.16.14-PM-1024x708.png" alt="" width="1024" height="708" class="alignnone size-large wp-image-7451" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-3.16.14-PM-1024x708.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-3.16.14-PM-600x415.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-3.16.14-PM-300x208.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-3.16.14-PM-768x531.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>You will see a list of properties classes of the device.  Starting with the official Amazon Resource Name (ARN) of your device.  If you click on &#8220;Security&#8221;</p>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-11-20-42-am/" rel="attachment wp-att-7410"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.20.42-AM-1024x671.png" alt="" width="1024" height="671" class="alignnone size-large wp-image-7410" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.20.42-AM-1024x671.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.20.42-AM-600x393.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.20.42-AM-300x197.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.20.42-AM-768x503.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>You will see that indeed you have a Certificate that is &#8220;attached&#8221; to your device.  Hopefully you downloaded the keys that go with the device.  If you didn&#8217;t you are screwed and will need to create a new Certificate (which you can do on this screen)</p>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-2-28-45-pm/" rel="attachment wp-att-7438"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.28.45-PM-1024x664.png" alt="" width="1024" height="664" class="alignnone size-large wp-image-7438" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.28.45-PM-1024x664.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.28.45-PM-600x389.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.28.45-PM-300x195.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.28.45-PM-768x498.png 768w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.28.45-PM.png 2026w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>Create a Policy and Attach it to your Certificate</h1>
<p>Amazon has documentation for Policies <a href="https://docs.aws.amazon.com/iot/latest/developerguide/create-iot-policy.html" target="_blank" rel="noopener noreferrer">here</a>.  As I discussed earlier a Policy is a JSON document that is attached to a Certificate that enables a Thing who is identified by that Certificate to take Action(s) on a specific Resource as identified by an ARN.  Policies can have wildcards for Actions and Resources, so they may be  attached to multiple Certificates.  Imagine Action:* and Resource:* (which is probably a bad policy)</p>
<p>Let&#8217;s create one and that should illuminate things better.  Go back to the main screen and click on &#8220;Secure-&gt;Policies&#8221;.  Then click &#8220;Create&#8221;</p>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-2-29-52-pm/" rel="attachment wp-att-7439"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.29.52-PM-1024x747.png" alt="" width="1024" height="747" class="alignnone size-large wp-image-7439" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.29.52-PM-1024x747.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.29.52-PM-600x437.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.29.52-PM-300x219.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.29.52-PM-768x560.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Give the Policy a name.  In this case &#8220;Test2Policy&#8221;.  My Policy has two Actions.</p>
<ol>
<li>IoT:Connect which is allowed by the Thing &#8220;&#8230;./Test2&#8221;</li>
<li>IoT:Publish which is allowed you to MQTT Publish to the topic listed (notice I made an error and I really meant Test2)</li>
</ol>
<p>When you click on the Actions box Amazon give you a list of suggestions.  One of the suggestions is &#8220;IoT:*&#8221; which means ANY of the IoT actions (like Connect, Publish, Subscribe,&#8230;)  You can also specify a wildcard for the resources with a &#8220;*&#8221;</p>
<p>After you have the policy done, click &#8220;Create&#8221;</p>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-2-31-58-pm/" rel="attachment wp-att-7440"></a> <a href="https://iotexpert.com/?attachment_id=7441" rel="attachment wp-att-7441"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.31.43-PM-928x1024.png" alt="" width="928" height="1024" class="alignnone size-large wp-image-7441" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.31.43-PM-928x1024.png 928w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.31.43-PM-600x662.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.31.43-PM-272x300.png 272w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.31.43-PM-768x848.png 768w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.31.43-PM.png 2028w" sizes="auto, (max-width: 928px) 100vw, 928px" /></a></p>
<p>And your screen will look something like this.  Notice that I setup a policy called &#8220;policyall&#8221; which is a wildcard policy that lets me do anything.  You can click on the policies and see what is going on with them.</p>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-2-31-58-pm/" rel="attachment wp-att-7440"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.31.58-PM-1024x586.png" alt="" width="1024" height="586" class="alignnone size-large wp-image-7440" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.31.58-PM-1024x586.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.31.58-PM-600x343.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.31.58-PM-300x172.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.31.58-PM-768x439.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>In order to have the Policy take effect you need to attach it to the Certificate.  Click on Secure-&gt;Certificates.  Then click your specific Certificate.  In my case it was &#8220;ca8&#8230;&#8221;</p>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-2-35-33-pm/" rel="attachment wp-att-7442"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.35.33-PM-1024x542.png" alt="" width="1024" height="542" class="alignnone size-large wp-image-7442" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.35.33-PM-1024x542.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.35.33-PM-600x318.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.35.33-PM-300x159.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.35.33-PM-768x407.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>When you get to the Certificate page you can then click on &#8220;policies&#8221;</p>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-2-35-41-pm/" rel="attachment wp-att-7443"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.35.41-PM-1024x674.png" alt="" width="1024" height="674" class="alignnone size-large wp-image-7443" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.35.41-PM-1024x674.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.35.41-PM-600x395.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.35.41-PM-300x198.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.35.41-PM-768x506.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Where you will see that you don&#8217;t have a Policy associated with your Certificate.</p>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-2-35-49-pm/" rel="attachment wp-att-7444"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.35.49-PM-1024x408.png" alt="" width="1024" height="408" class="alignnone size-large wp-image-7444" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.35.49-PM-1024x408.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.35.49-PM-600x239.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.35.49-PM-300x119.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.35.49-PM-768x306.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Fix that by click on &#8220;Actions&#8221; which is on the right hand side of the screen.  Pick &#8220;Attach Policy&#8221;</p>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-2-35-58-pm/" rel="attachment wp-att-7445"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.35.58-PM-1024x523.png" alt="" width="1024" height="523" class="alignnone size-large wp-image-7445" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.35.58-PM-1024x523.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.35.58-PM-600x306.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.35.58-PM-300x153.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.35.58-PM-768x392.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>On this screen pick the policy you want to attach.  In this case I picked &#8220;Test2Policy&#8221;.  Then click attach.</p>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-2-36-07-pm/" rel="attachment wp-att-7446"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.36.07-PM-1024x665.png" alt="" width="1024" height="665" class="alignnone size-large wp-image-7446" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.36.07-PM-1024x665.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.36.07-PM-600x390.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.36.07-PM-300x195.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.36.07-PM-768x499.png 768w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-2.36.07-PM.png 1488w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>MQTT &amp; the Test Client</h1>
<p>One of the coolest things that Amazon provides is a web browser based MQTT client.  To get to it press &#8220;Test&#8221; (the last item on the left)</p>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-06-at-7-54-04-am/" rel="attachment wp-att-7465"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-7.54.04-AM-1024x840.png" alt="" width="1024" height="840" class="alignnone size-large wp-image-7465" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-7.54.04-AM-1024x840.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-7.54.04-AM-600x492.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-7.54.04-AM-300x246.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-7.54.04-AM-768x630.png 768w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-06-at-7.54.04-AM.png 1146w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Which will bring you to this screen.  Here you can Subscribe to Topics by typing the name of the topic you are interested in and clicking &#8220;Subscribe to Topic&#8221;.  You can also Publish messages to a Topic by typing the Topic name in the Publish box, and typing the message in the black box.  The message is typically in JSON format, but this is not actually a requirement.</p>
<p>There are very few rules about topic names and as such are left up to you as application semantics.  There are, however, a few reserved names which cause specific things to happen in the AWS IoT Cloud.  These topics all start with $aws and are documented here.</p>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-11-59-23-am/" rel="attachment wp-att-7419"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.59.23-AM-1024x672.png" alt="" width="1024" height="672" class="alignnone size-large wp-image-7419" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.59.23-AM-1024x672.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.59.23-AM-600x394.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.59.23-AM-300x197.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.59.23-AM-768x504.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Let&#8217;s do a little demonstration of the system by subscribing to &#8220;myrandomtopic&#8221;, obviously just a name I made up.  Type in the box and press &#8220;subscribe to topic&#8221;</p>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-12-01-21-pm/" rel="attachment wp-att-7420"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.01.21-PM-1024x680.png" alt="" width="1024" height="680" class="alignnone size-large wp-image-7420" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.01.21-PM-1024x680.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.01.21-PM-600x398.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.01.21-PM-300x199.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.01.21-PM-768x510.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Once that is done you will see on the left side of the screen the topic name in bold with an &#8220;x&#8221;.   To actually publish something you can type a message to be sent into the black box&#8230; and when you press &#8220;Publish to topic&#8221;  Go ahead and type something.</p>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-12-01-48-pm/" rel="attachment wp-att-7421"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.01.48-PM-1024x678.png" alt="" width="1024" height="678" class="alignnone size-large wp-image-7421" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.01.48-PM-1024x678.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.01.48-PM-600x397.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.01.48-PM-300x199.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.01.48-PM-768x508.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>When you press publish, your screen will show each the message that is Published to the Topic because you are Subscribed.  This will include messages you Publish in the Test console, as well as Messages that are Published by other devices, like your Thing.  This is a really convenient way to debug what is going on in your system.</p>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-12-03-23-pm/" rel="attachment wp-att-7422"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.03.23-PM-1024x670.png" alt="" width="1024" height="670" class="alignnone size-large wp-image-7422" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.03.23-PM-1024x670.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.03.23-PM-600x393.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.03.23-PM-300x196.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.03.23-PM-768x503.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>If you go back to the publish to a topic screen and type a different message&#8230; then press &#8220;publish to topic&#8221;&#8230; you will notice a green dot next to the topic indicating a new message.</p>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-12-03-53-pm/" rel="attachment wp-att-7423"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.03.53-PM-1024x648.png" alt="" width="1024" height="648" class="alignnone size-large wp-image-7423" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.03.53-PM-1024x648.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.03.53-PM-600x380.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.03.53-PM-300x190.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.03.53-PM-768x486.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>And when you click the topic you will see the history of message Published since you Subscribed.</p>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-12-04-03-pm/" rel="attachment wp-att-7424"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.04.03-PM-1024x661.png" alt="" width="1024" height="661" class="alignnone size-large wp-image-7424" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.04.03-PM-1024x661.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.04.03-PM-600x387.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.04.03-PM-300x194.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.04.03-PM-768x496.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>You are allowed to subscribe to multiple topics at a time and it will show all of them.</p>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-12-07-22-pm/" rel="attachment wp-att-7425"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.07.22-PM-1024x325.png" alt="" width="1024" height="325" class="alignnone size-large wp-image-7425" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.07.22-PM-1024x325.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.07.22-PM-600x190.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.07.22-PM-300x95.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.07.22-PM-768x244.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>There is also the ability to subscribe to &#8220;wildcard&#8221; topics.</p>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-12-08-44-pm/" rel="attachment wp-att-7427"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.08.44-PM-1024x370.png" alt="" width="1024" height="370" class="alignnone size-large wp-image-7427" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.08.44-PM-1024x370.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.08.44-PM-600x217.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.08.44-PM-300x108.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.08.44-PM-768x277.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Which means you can subscribe to &#8220;#&#8221; which will give you all messages sent to the MQTT message broker</p>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-12-10-30-pm/" rel="attachment wp-att-7428"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.10.30-PM-1024x426.png" alt="" width="1024" height="426" class="alignnone size-large wp-image-7428" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.10.30-PM-1024x426.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.10.30-PM-600x250.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.10.30-PM-300x125.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.10.30-PM-768x320.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a> <a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-12-11-52-pm/" rel="attachment wp-att-7429"></a></p>
<p>Notice that if I Publish to &#8220;myrandomtopic&#8221; that it will match by &#8220;myrandomtopic&#8221; as well as &#8220;#&#8221; (look at the green dots on the left of the screen)</p>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-12-11-52-pm/" rel="attachment wp-att-7429"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.11.52-PM-1024x600.png" alt="" width="1024" height="600" class="alignnone size-large wp-image-7429" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.11.52-PM-1024x600.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.11.52-PM-600x352.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.11.52-PM-300x176.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.11.52-PM-768x450.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>The Device Shadow</h1>
<p>The purpose of the Device Shadow is to serve as a Cache of the Reported and Desired State of a Thing.  This allows a Thing to not be connected all of the time.  Imagine that a light build sends its &#8220;reported&#8221; state every time that it changes.  And a light switch will send the light bulbs &#8220;desired&#8221; state when it wants to change the light bulb.  This allows a device to figure out what state it is supposed to be in when a power outage occurs.  And it allows devices to find out what is going on with a Thing without having to talk directly to them.</p>
<p>The official <a href="https://docs.aws.amazon.com/iot/latest/developerguide/device-shadow-document-syntax.html" target="_blank" rel="noopener noreferrer">format</a> of the Device Shadow is as follows.  Notice just another JSON document.</p>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-3-35-27-pm/" rel="attachment wp-att-7452"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-3.35.27-PM-1024x501.png" alt="" width="1024" height="501" class="alignnone size-large wp-image-7452" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-3.35.27-PM-1024x501.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-3.35.27-PM-600x293.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-3.35.27-PM-300x147.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-3.35.27-PM-768x375.png 768w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-3.35.27-PM.png 1436w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Here is an example document</p>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-11-30-53-am/" rel="attachment wp-att-7417"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.30.53-AM-1024x707.png" alt="" width="1024" height="707" class="alignnone size-large wp-image-7417" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.30.53-AM-1024x707.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.30.53-AM-600x414.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.30.53-AM-300x207.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.30.53-AM-768x530.png 768w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.30.53-AM.png 1800w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>You can look at the Device Shadow by Clicking on a Thing in the Management Console.  Then clicking Shadow.  This device has a boring document which nothing in it.</p>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-11-21-00-am/" rel="attachment wp-att-7412"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.21.00-AM-1024x797.png" alt="" width="1024" height="797" class="alignnone size-large wp-image-7412" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.21.00-AM-1024x797.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.21.00-AM-600x467.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.21.00-AM-300x234.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.21.00-AM-768x598.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>Update the Shadow Using the Test Client</h1>
<p>The last piece of this puzzle is how a Thing interacts with its Device Shadow.  That is simple.  A Thing needs to send JSON message in the right format to the right MQTT Topic.  If you click on &#8220;Interact&#8221; it will show you the list of Topics.</p>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-11-26-42-am/" rel="attachment wp-att-7415"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.26.42-AM-822x1024.png" alt="" width="822" height="1024" class="alignnone size-large wp-image-7415" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.26.42-AM-822x1024.png 822w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.26.42-AM-600x747.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.26.42-AM-241x300.png 241w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.26.42-AM-768x956.png 768w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.26.42-AM.png 2024w" sizes="auto, (max-width: 822px) 100vw, 822px" /></a></p>
<p>In the documentation there are examples of JSON messages that you need to Publish.</p>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-11-29-10-am/" rel="attachment wp-att-7418"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.29.10-AM-751x1024.png" alt="" width="751" height="1024" class="alignnone size-large wp-image-7418" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.29.10-AM-751x1024.png 751w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.29.10-AM-600x818.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.29.10-AM-220x300.png 220w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.29.10-AM-768x1047.png 768w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-11.29.10-AM.png 1812w" sizes="auto, (max-width: 751px) 100vw, 751px" /></a></p>
<p>Given all of that, let&#8217;s update the shadow for Test2 by publishing a message with the temperature and depth in this JSON document</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c" ">{
    "state": {
        "reported" : {
           "temperature":30.12,
           "depth":10.2
        }
    }
}</pre>
<p>First subscribe to the &#8220;#&#8221; topic so you can see all of the messages.  Then publish the JSON document.</p>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-12-21-02-pm/" rel="attachment wp-att-7431"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.21.02-PM-1024x296.png" alt="" width="1024" height="296" class="alignnone size-large wp-image-7431" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.21.02-PM-1024x296.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.21.02-PM-600x173.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.21.02-PM-300x87.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.21.02-PM-768x222.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a> <a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-12-21-43-pm/" rel="attachment wp-att-7432"></a></p>
<p>In the MQTT test client you will see</p>
<ul>
<li>$aws/things/Test2/shadow/update/accepted</li>
<li>$aws/things/Test2/shadow/update</li>
<li>$aws/things/Test1/shadow/update/documents</li>
</ul>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-12-21-43-pm/" rel="attachment wp-att-7432"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.21.43-PM-1024x643.png" alt="" width="1024" height="643" class="alignnone size-large wp-image-7432" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.21.43-PM-1024x643.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.21.43-PM-600x377.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.21.43-PM-300x188.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.21.43-PM-768x482.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a> <a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-12-21-59-pm/" rel="attachment wp-att-7433"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.21.59-PM-1024x211.png" alt="" width="1024" height="211" class="alignnone size-large wp-image-7433" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.21.59-PM-1024x211.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.21.59-PM-600x124.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.21.59-PM-300x62.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.21.59-PM-768x158.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Then you will be able to go to the management console &#8211;&gt; Manage -&gt; Things.  This will show you all of your &#8220;things&#8221; including the &#8220;Test2&#8221; that we just updated.  Click on &#8220;Test2&#8221;</p>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-12-26-38-pm/" rel="attachment wp-att-7434"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.26.38-PM-1024x495.png" alt="" width="1024" height="495" class="alignnone size-large wp-image-7434" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.26.38-PM-1024x495.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.26.38-PM-600x290.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.26.38-PM-300x145.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.26.38-PM-768x371.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Then click Shadow.  Now you will be able to see that the document has been updated and it is caching the state of the device.</p>
<p><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/screen-shot-2019-07-05-at-12-26-48-pm/" rel="attachment wp-att-7435"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.26.48-PM-1024x1012.png" alt="" width="1024" height="1012" class="alignnone size-large wp-image-7435" srcset="https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.26.48-PM-1024x1012.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.26.48-PM-100x100.png 100w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.26.48-PM-600x593.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.26.48-PM-300x296.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-05-at-12.26.48-PM-768x759.png 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Now that we know how to interact with the device shadow via MQTT.  How do I get the Raspberry Pi to send MQTT messages?  That is the topic of the next article.</p>
<p><span><p><div class="table-responsive"><table  style="width:95%; "  class="easy-table easy-table-default " border="1">
<thead>
<tr><th >#</th>
<th >Title</th>
</tr>
</thead>
<tbody>
<tr><td >0</td>
<td ><a href="https://iotexpert.com/2019/07/08/the-creek-amazon-aws-iot-solution-architecture-2-0/" target="_blank" rel="noopener">The Creek 2.0: Amazon AWS IoT Solution Architecture 2.0</a></td>
</tr>

<tr><td >1</td>
<td ><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/" target="_blank" rel="noopener">The Creek 2.0: AWS IoT MQTT Message Broker</a></td>
</tr>

<tr><td >2</td>
<td ><a href="https://iotexpert.com/2019/07/22/the-creek-2-0-read-sensor-data-send-to-aws-iot-via-mqtt/" target="_blank" rel="noopener">The Creek 2.0: Read Sensor Data Send to AWS IoT via MQTT</a></td>
</tr>

<tr><td >3</td>
<td ><a href="https://iotexpert.com/2019/07/29/amazon-aws-virtual-private-cloud-vpc/" target="_blank" rel="noopener">Amazon AWS Virtual Private Cloud (VPC)</a></td>
</tr>

<tr><td >4</td>
<td ><a href="https://iotexpert.com/2019/08/12/the-creek-2-0-aws-relational-database-server-rds-mysql/" target="_blank" rel="noopener">The Creek 2.0: AWS Relational Database Server (RDS) - MySQL</a></td>
</tr>

<tr><td >5</td>
<td ><a href="https://iotexpert.com/2019/08/19/the-creek-2-0-aws-lambda-function/" target="_blank" rel="noopener">The Creek 2.0: AWS Lambda Function</a></td>
</tr>

<tr><td >6</td>
<td ><a href="https://iotexpert.com/the-creek-2-0-aws-iot-actions-rules/">The Creek 2.0: AWS IoT Actions &amp; Rules</a></td>
</tr>
</tbody></table></div></p></span></p>
]]></content:encoded>
					
					<wfw:commentRss>https://iotexpert.com/the-creek-2-0-aws-iot-mqtt-message-broker/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>The Creek 2.0: Amazon AWS IoT Solution Architecture 2.0</title>
		<link>https://iotexpert.com/the-creek-amazon-aws-iot-solution-architecture-2-0/</link>
					<comments>https://iotexpert.com/the-creek-amazon-aws-iot-solution-architecture-2-0/#respond</comments>
		
		<dc:creator><![CDATA[Alan Hawse]]></dc:creator>
		<pubDate>Mon, 08 Jul 2019 12:32:00 +0000</pubDate>
				<category><![CDATA[AWS IoT]]></category>
		<category><![CDATA[Elkhorn Creek 2.0]]></category>
		<guid isPermaLink="false">https://iotexpert.com/?p=7361</guid>

					<description><![CDATA[Summary Last week I talked about fixing my Creek Water Level sensor.  This got me to reflecting on a change that I have been wanting to make for a long long time: moving all of the backend server stuff to the Amazon AWS IoT Cloud.  In this article, I will explain the architecture of the [&#8230;]]]></description>
										<content:encoded><![CDATA[<h1>Summary</h1>
<p><a href="https://iotexpert.com/2019/07/01/repair-the-elkhorn-creek-water-level-sensor/" target="_blank" rel="noopener noreferrer">Last week</a> I talked about fixing my Creek Water Level sensor.  This got me to reflecting on a change that I have been wanting to make for a long long time: moving all of the backend server stuff to the Amazon AWS IoT Cloud.  In this article, I will explain the architecture of the intermediate end result.  What in the world does &#8220;intermediate end result&#8221; mean? Alan, is that a really goofy way to say that you aren&#8217;t going to finish the job?  Well, I suppose yes, not at first.  But I am going to hook up all of the middle stuff, from the current Raspberry Pi to an Amazon Relational Database Server (RDS) running MySQL.</p>
<p>There is a bunch of technology going on to make my new solution work, including:</p>
<ul>
<li>PSoC 4 &amp; Embedded C</li>
<li>Copious use of Python</li>
<li>MySQL</li>
<li>JSON</li>
<li>Raspberry Pi</li>
<li>MQTT</li>
<li>AWS IoT Core, Shadow</li>
<li>AWS Python SDK</li>
</ul>
<h1>Architecture</h1>
<p>This is a picture of the updates to the system architecture.  The boxes in green are unchanged from the<a href="https://iotexpert.com/2016/01/30/creek-architecture-1-0/"> original system architecture</a>.  The purple Raspberry Pi box will get some new stuff that bridges data to the Amazon IoT cloud and the blue boxes (which are Amazon AWS) are totally new.</p>
<p><a href="https://iotexpert.com/2019/07/08/the-creek-amazon-aws-iot-solution-architecture-2-0/creek20-2/" rel="attachment wp-att-7367"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/07/creek20-2-1024x339.png" alt="" width="1024" height="339" class="alignnone size-large wp-image-7367" srcset="https://iotexpert.com/wp-content/uploads/2019/07/creek20-2-1024x339.png 1024w, https://iotexpert.com/wp-content/uploads/2019/07/creek20-2-600x199.png 600w, https://iotexpert.com/wp-content/uploads/2019/07/creek20-2-300x99.png 300w, https://iotexpert.com/wp-content/uploads/2019/07/creek20-2-768x255.png 768w, https://iotexpert.com/wp-content/uploads/2019/07/creek20-2.png 1225w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>(1) Pressure Sensor</h1>
<p>The Measurement Specialties US381 Pressure sensor remains unchanged.  It senses the water pressure from the Creek and returns 4-20mA based on a pressure of 0 to 15PSI.  0PSI=4mA, 7.5PSI=12mA and 15PSI=20mA.</p>
<h1>(2) Creek Board</h1>
<p>The <a href="https://iotexpert.com/2016/02/07/the-creek-board-1-1/">Creek Board</a> remains unchanged.  It supplies power to the pressure sensor and has a 51.1Ohm sensing resistor which serves to turn the current of 4-20mA into voltage of 0.202V to 1.022V, which is perfect for the PSoC Analog to Digital Convertor.</p>
<p><a href="https://iotexpert.com/2016/02/07/the-creek-board-1-1/img_2652/" rel="attachment wp-att-401"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2016/01/IMG_2652-1024x768.jpg" alt="" width="1024" height="768" class="alignnone size-large wp-image-401" srcset="https://iotexpert.com/wp-content/uploads/2016/01/IMG_2652-1024x768.jpg 1024w, https://iotexpert.com/wp-content/uploads/2016/01/IMG_2652-600x450.jpg 600w, https://iotexpert.com/wp-content/uploads/2016/01/IMG_2652-300x225.jpg 300w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>(3) CyPi Board</h1>
<p>The <a href="https://iotexpert.com/2016/02/21/cypi/">CyPi Board</a> remains unchanged.  It has an Arduino pin out on the top to connect to the Creek Board and on the bottom it has the Raspberry PI I2C and GPIO interface.  On the board is a PSoC 4 which reads the voltage of the pressure sensor.  This board also provides power to the sensor and the Raspberry Pi (remember from the previous post that I blew up the power regulator)</p>
<p><a href="https://iotexpert.com/2016/02/21/cypi/img_1176-1/" rel="attachment wp-att-439"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2016/01/IMG_1176-1.jpg" alt="" width="520" height="311" class="alignnone size-full wp-image-439" srcset="https://iotexpert.com/wp-content/uploads/2016/01/IMG_1176-1.jpg 520w, https://iotexpert.com/wp-content/uploads/2016/01/IMG_1176-1-300x179.jpg 300w" sizes="auto, (max-width: 520px) 100vw, 520px" /></a></p>
<h1>(4) Raspberry Pi</h1>
<p>In the <a href="https://iotexpert.com/2016/04/24/the-creek-server-software-architecture/" target="_blank" rel="noopener noreferrer">original design</a> the Raspberry Pi runs a bunch of different Java programs as well as MySQL.</p>
<p><a href="https://iotexpert.com/2016/04/24/the-creek-server-software-architecture/backendserver-3/" rel="attachment wp-att-516"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2016/03/backendserver-3-1024x707.png" alt="" width="1024" height="707" class="alignnone size-large wp-image-516" srcset="https://iotexpert.com/wp-content/uploads/2016/03/backendserver-3-1024x707.png 1024w, https://iotexpert.com/wp-content/uploads/2016/03/backendserver-3-600x414.png 600w, https://iotexpert.com/wp-content/uploads/2016/03/backendserver-3-300x207.png 300w, https://iotexpert.com/wp-content/uploads/2016/03/backendserver-3.png 1449w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></p>
<p>I am going to leave all of the original stuff unchanged.  In the picture above, you can see the runI2C shell script, which is run by the Raspberry Pi crontab.  I will modify this script to run a Python program that will read the sensor state using the SMBus library, then format a JSON message, then connect to the AWS MQTT server using the AWS IoT Python library and send an update of the Shadow state.</p>
<h1>(5) AWS IoT MQTT Message Broker</h1>
<p>The AWS IoT Cloud provides a bunch of tools to help people deploy IoT functionality.  There are two principal methods for interacting with the AWS IoT Cloud: Message Queuing Telemetry Transport (MQTT) and Hyper Text Transfer Protocol (HTTP).  I will be using MQTT to interface with the AWS Cloud.  Specifically, I will create JSON messages that represent the state of my IoT Device (the Creek Depth and Temperature) and then I will send it to the Amazon AWS MQTT Message Broker.  The message will be stored in a facility provided by Amazon called the Device Shadow, which is a cache of your &#8220;thing&#8221; state.</p>
<h1>(6) AWS IoT Rule Actions and (7) AWS Lambda</h1>
<p>In the AWS IoT Core management console you can configure &#8220;Act&#8221;ions based on the MQTT messages that are flying around on the MQTT broker.  My action will be to look for updates to the Device Shadow topics and then to trigger an AWS Lamba function.  That Python function will take the JSON message (sent via AWS) and will insert the data into the MySQL database.</p>
<h1>(8) AWS RDS MySQL</h1>
<p>I will create almost the exact database that is running on the Raspberry Pi and install that into an Amazon Relational Database Server (RDS) running MySQL.  I decided to make the database extensible to add data from other &#8220;things&#8221;.  To do this I add a table of device names and id which map to the data table.</p>
<h1>Future</h1>
<p>When I get a few minutes there are a bunch of things that I would like to add to this system</p>
<ul>
<li>Remove the Raspberry PI and create a PSoC 6 / 43012 Amazon Free RTOS board to read the data and send it to the AWS Cloud</li>
<li>AWS Greengrass</li>
<li>Use Grafana to view the data</li>
<li>Create and AWS Django Python based web server to display the data</li>
</ul>
<p><span><p><div class="table-responsive"><table  style="width:95%; "  class="easy-table easy-table-default " border="1">
<thead>
<tr><th >#</th>
<th >Title</th>
</tr>
</thead>
<tbody>
<tr><td >0</td>
<td ><a href="https://iotexpert.com/2019/07/08/the-creek-amazon-aws-iot-solution-architecture-2-0/" target="_blank" rel="noopener">The Creek 2.0: Amazon AWS IoT Solution Architecture 2.0</a></td>
</tr>

<tr><td >1</td>
<td ><a href="https://iotexpert.com/2019/07/15/the-creek-2-0-aws-iot-mqtt-message-broker/" target="_blank" rel="noopener">The Creek 2.0: AWS IoT MQTT Message Broker</a></td>
</tr>

<tr><td >2</td>
<td ><a href="https://iotexpert.com/2019/07/22/the-creek-2-0-read-sensor-data-send-to-aws-iot-via-mqtt/" target="_blank" rel="noopener">The Creek 2.0: Read Sensor Data Send to AWS IoT via MQTT</a></td>
</tr>

<tr><td >3</td>
<td ><a href="https://iotexpert.com/2019/07/29/amazon-aws-virtual-private-cloud-vpc/" target="_blank" rel="noopener">Amazon AWS Virtual Private Cloud (VPC)</a></td>
</tr>

<tr><td >4</td>
<td ><a href="https://iotexpert.com/2019/08/12/the-creek-2-0-aws-relational-database-server-rds-mysql/" target="_blank" rel="noopener">The Creek 2.0: AWS Relational Database Server (RDS) - MySQL</a></td>
</tr>

<tr><td >5</td>
<td ><a href="https://iotexpert.com/2019/08/19/the-creek-2-0-aws-lambda-function/" target="_blank" rel="noopener">The Creek 2.0: AWS Lambda Function</a></td>
</tr>

<tr><td >6</td>
<td ><a href="https://iotexpert.com/the-creek-2-0-aws-iot-actions-rules/">The Creek 2.0: AWS IoT Actions &amp; Rules</a></td>
</tr>
</tbody></table></div></p></span></p>
]]></content:encoded>
					
					<wfw:commentRss>https://iotexpert.com/the-creek-amazon-aws-iot-solution-architecture-2-0/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
