Lab 6.1 IOT - AWS
Lab 6.1 IOT - AWS
The Cars will be simulated in a Cloud9 environment that you will need to create.
You will download the code of the Cars, upload their IoT Certificates and start their
engine. You will use the AWS IoT MQTT Client in the AWS Management Console
to subscribe to the telemetry IoT Topic to confirm that the Cars are sending data.
The diagram below shows the resources and data flow that you will create in this
exercise.
1. In the AWS Management Console, click Services, and then click IAM to open the
IAM dashboard.
5. In the editor text box, replace the sample policy with the following:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"greengrass:*",
"iot:*",
"iotanalytics:*",
"cloud9:*",
"lambda:*",
"s3:*",
"sns:*",
"iam:*",
"cognito-identity:*",
"cognito-sync:*",
"cognito-idp:*",
"logs:*",
"ec2:*",
"cloudwatch:*",
"kms:ListAliases",
"kms:DescribeKey",
"cloudformation:DescribeStackResources",
"tag:getResources"
],
"Resource": "*"
}
]
}
6. Click Review Policy.
1. In the AWS Management Console, click Services, and then click IAM to go to the
IAM dashboard.
6. For Console password, choose Custom password and enter a password of your
choosing. Note the password.
7. Remove the check mark next to User must create a new password at next
sign-in.
14. Review the information, and click Create user. You should see a success
message.
15. Note the sign-in URL in the success message at the top. This is a special URL for
IAM users, which includes your account ID.
16. Click on the sign-in URL in the success message at the top. This will log you out.
1. In the AWS Management Console, click Services, and then click Cloud9 to open
the Cloud9 dashboard.
2. Make sure you are in the Frankfurt, Ireland, N. Virginia, Ohio, Oregon or
Tokyo Region. Those are, at the time of writing the exercises, the only regions with
all the services that will be used: Amazon Cognito, Amazon EC2, Amazon S3,
Amazon SNS, AWS Cloud9, AWS IAM, AWS IoT Analytics, AWS IoT Core, AWS
IoT Greengrass, AWS Lambda. Since all resources must be in the same region for
the exercises to work, you must use a region where all those services are
available. You can find a list of the services available per region at this link.1
6. On the Configure Settings page, leave the default settings, and click Next step.
7. Review the details and click Create environment. This should launch your AWS
Cloud9 environment within a few minutes.
Note that this Cloud9 instance will automatically shutdown after 30 minutes if it's
not used. All of your work will be saved and brought back to what it was if you were
to re-open it.
1
https://aws.amazon.com/es/about-aws/global-infrastructure/regional-product-services/
1. Install the AWS IoT Device SDK Node package by running the following command
in your AWS Cloud9 terminal. You can find that terminal at the bottom of the page.
There is a bash tab with the prompt labIoTUser:~/environment: $. You can adjust
the size of that screen like you would in a normal IDE: put your cursor above the tab
and select&drag to increase/decrease the space.
You can ignore the warnings that there are no package.json files.
2. Create the repository structure for the Car application. As there will be 2 Cars, you
will create 2 folders. Run the following commands in your AWS Cloud9 terminal.
3. Download and copy the application code in each Car folder by running the
following commands in your AWS Cloud9 terminal:
cd ~/environment
wget https://aws-tc-largeobjects.s3.amazonaws.com/OTP-
AWS_D5-2019/v1.0/code/exercise-1.1.js
cp exercise-1.1.js car1/
cp exercise-1.1.js car2/
rm exercise-1.1.js
4. Download the AWS IoT Certificate Authority Public Certificate that will be used in
the code later and that will sign the IoT Certificates you will create in the next
section. To do so, execute the following commands in your Cloud9 terminal:
cd ~/environment
wget -O root-CA.crt
https://www.amazontrust.com/repository/AmazonRootCA1.pem
5. Create Car 1 IoT Thing, Certificate and Policy
In this section you will use the AWS Management Console to create all of the
resources required for your Car 1 to connect to AWS IoT. This includes the the Car
Thing, the Certificate and the Policy. We could use the wizard to create many of
these resources, but instead, you will do it manually to see how each of the
components is attached to each other. You will finish by uploading the Certificate
and Private Key that you generated as part of these steps to Cloud9.
1. You may have lost your AWS Management Console and only see the Cloud9
console at this moment. To get back to the AWS Management Console without
having to retype the URL, click on the cloud icon with the number 9 in it at the top
left corner of the page and select Go To Your Dashboard.
2. In the AWS Management Console, click Services, and then click IoT Core to open
the the IoT Console.
3. Make sure you are in the same Region as your Cloud9 instance.
6. Click Things.
8. Click Create a single thing (any of the two buttons with that name works).
10. Click Create thing without certificate so that you skip the creation of the
Certificate via the wizard.
2. Click Policies.
6. Replace the sample policy with the following policy which authorize to Connect to
your AWS IoT Core endpoint, to Publish and Subscribe to an IoT Topic, Receive
messages from AWS IoT once subscribed and use the Discover API from
Greengrass which will be used in a later exercise.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:Connect",
"iot:Publish",
"iot:Subscribe",
"iot:Receive",
"greengrass:Discover"
],
"Resource": [
"*"
]
}
]
}
7. Click Create.
1. Expand Certificates.
2. Click Create a certificate.
4. Make sure that you do the following steps before going to the next screen as you
would have to restart the creation of this Certificate.
4. Click the Download link next to A private key. Note that you
won't need the public key.
5. Click Done.
You now have a Certificate and Private Key that can be used to connect to your
AWS IoT Core endpoint. However, this is only for authentication, you don't have
any authorization yet associated to this Certificate. You may see that the Certificate
is inactive, if you refresh the page, it should show as active. This will be done next.
6. If you click on Policies or Things on the left menu, you can see that both the
labPolicy Policy and car1 Thing have been attached.
1. You may have closed your Cloud9 tab. If you did so, in the AWS Management
Console, click Services, and then click Cloud9 to open the Cloud9 dashboard.
Click Open IDE under the IoTOnAWS environment card.
5. Click Select files. Note that the button may appear in grey, it isn't disabled and you
can click on it.
8. Browse to the Private Key you have downloaded and renamed earlier:
private.pem.key. Select it and click Open. The Private Key should now be
uploaded to your car1 folder.
Both car1 Certificate and Private Key should now be in the car1 folder.
cd ~/environment/car2
aws iot create-thing --thing-name car2
This command will place the Certificate and Private Key in the certificate.pem.crt
file and private.pem.key respectively. It will also output the certificateArn which you
will re-use in the next command.
3. To attach the Policy to the Certificate, enter the following command. Replace
certificateArn_changeme with the value of the attribute certificateArn from the
output of the previous command. It should be similar to: arn:aws:iot:us-east-
1:1234567890:cert/0f11db22dafacda87be0940dd5b2e010635916f541461ccf2d1c5
6ced0f343ee
1. In the AWS Management Console, click Services, and then click IoT Core to open
the IoT Core console.
3. You should see car2 listed. If not, refresh the page as your browser may have
cached the previous version of this page.
4. Click car2.
5. Click Security.
8. Click on Policies.
You have now created the car2 Thing, its Certificate and Private Key to
authenticate and attached the labPolicy to authorize the commands to execute
later.
The code (exercise-1.1.js) requires one more resource to communicate with AWS
IoT and that is your specific AWS IoT Endpoint. It will be stored in a file that will be
used in all of the other exercises. To authenticate to your AWS IoT Endpoint, it will
use the certificate.pem.crt (Certificate), private.pem.key (Private Key) and root-
CA.crt (Certificate Authority public certificate). It then connects to AWS IoT and
starts publishing random telemetry data every 5 seconds to the lab/telemetry IoT
Topic.
Feel free to read the code to understand what is happening. Comments have been
added to the code so it can be self explanatory.
The command above will not output anything if it worked. Feel free to look at that
file as it will be visible on the left side of the editor.
2. In the Cloud9 environment, click the circled + icon that is next to your current
terminal and select New Terminal. You now have 2 different terminals.
3. In the left terminal, execute the following commands to start the code for car1.
Car1's Certificate and Private Key, the Root Certificate Authority and your specific
AWS IoT Endpoint will all be read from the files you created.
cd ~/environment/car1
node exercise-1.1.js
...
4. In the right terminal, execute the following commands to start the code for car2.
Car2's Certificate and Private Key, the Root Certificate Authority and your specific
AWS IoT Endpoint will all be read from the files you created.
cd ~/environment/car2
node exercise-1.1.js
...
Both cars are now successfully sending telemetry data to the lab/telemetry IoT
Topic.
7.2 Subscribe to the lab/telemetry Topic
In this section, you will use the AWS IoT MQTT Client in the AWS Management
Console to subscribe to the lab/telemetry IoT Topic. While connected to this MQTT
Client, it does consume connection minutes. The free tier is generous and provides
2,250,000 minutes of connection for free, but you should make sure to disconnect
when you are not using it by browsing away from that page.
1. In the AWS Management Console, click Services, and then click IoT Core to open
the IoT Core console.
2. Click Test in the left menu. It will open an AWS IoT MQTT Client where you can
interact with any Topic that you have access to. This Client will automatically
connect to your IoT Endpoint.
In the next 5 seconds, you should start seeing data being published by both cars in
the interface. You can see which car is sending the data by looking at
the device attribute.
8. Delete the resources created in this exercise
While there are no connections nor data being transmitted to the IoT service, there
will not be any charge for this exercise for that service.
The Cloud9 environment uses a t2.micro EC2 instance and an 8GiB Elastic Block
Storage (EBS) volume which is what you are being charged on for the usage of
Cloud9. The t2.micro has 750 hours of free utilization per month under the free tier
for the first 12 months after opening your account. You have 30GiB of space for
EBS covered under the free tier for the first 12 months of the opening of your
account. If you are no longer under the free tier, you will incur a charge while the
EC2 instance is running for the t2.micro instance and a charge for the EBS volume
as long as your environment exists.
All of the other exercises will use the resources created as part of this exercise.
The recommendation is to keep the IoT resources for the future exercises and to
let Cloud9 stop by itself after 30 minutes of inactivity so you can keep the
environment. If you decide not to keep some of those resources, you will have to
do this exercise again before the other exercises.
If you would prefer to remove all of your resources. Follow the steps hidden below:
Expand for instructions on how to delete all your resources (not recommended).
1. Press Ctrl-c in each of the Cloud9 terminal to stop them from interacting with
AWS IoT.
1. Navigate away from the AWS IoT MQTT Client page to disconnect from the
client.
1. In the AWS Management Console, click Services, and then click EC2 to open the
EC2 console.
3. Select the EC2 Instance that has a name that starts with aws-cloud9-IoTOnAWS.