Setting up AWS MQTT in 10 minutes

AWS
Ashiq
Updated 21 Jun 2021
HIGHLIGHTS
  • Creating thing in AWS IOT Core
  • Creating certificate in AWS IOT
  • Creating policy in AWS IOT
  • Creating user in AWS
  • Publishing a sample message over MQTT

Don't get confused with the term IOT (Internet of Thing). Whether you are into IOT world or not, AWS MQTT will fit into n numbers of projects, may be in web development, app devlopment, application development and ofcourse IOT.

I guess you are here because you know about MQTT, if not read about MQTT here.

Use cases

Some of the daily apps that we use are Messenger, Zomato, Uber, Slack, Discord, Twitter, Instagram, IOT applications which transmits data in realtime which can be achieved with mqtt.

May be you heard about websocket, MQTT is similar which facilitates realtime data transmission amoung multiple devices. MQTT is light weight in terms of data transmission which means even in slow network data transmission will be fast enough.

NOTE : To take advantage of AWS you will have to create an AWS account and setup billing.

Warning! This is going to be a long post so plan accordingly.

Get set go...

Step 1 - Search for IOT core in search bar of AWS

Step 2 - Open IOT Core

Find AWS IOT Core
Find AWS IOT Core

First we will have to create a thing
A thing the representation of device in the cloud

Step 3 - Find Manage > Things on left sidebar

Step 4 - Register a thing

Register a thing in AWS IOT Core
Register a thing

Step 5 - Give your thing a name

Step 6 - Keep all as default, hit Next

Naming a thing in AWS IOT Core
Naming a thing

Step 7 - Add a certificate for your thing.

It is recommended to select "one click certificate generation", In case your use case demands other ceertificate or you want to use you own CSR to generate certificate you can do it as well.

Add certificate for your thing in AWS IOT Core
Add certificate for your thing.

Step 8 - Download all 3 certificate files, required to connect from your client

Step 9 - Click activate to activate the certificate

Step 10 - Hit attach a policy

in AWS IOT Core

Congratulation! Your thing is created

Thing created in AWS IOT Core
Thing created

What next?

We are done with creation of Thing, Certificate. Now will have to create a policy and attach the policy to the certificate we are using.

Step 11 - Find Secure > Policies on left sidebar

Step 12 - Hit create a policy

Create a policy in AWS IOT Core
Create a policy

Now we are required to define types of actions that can be performed by a resource.

Step 13 - Add iot:Connect, Give a client ID in resource ARN field and select "Allow" in effect option.

Step 14 - Add iot:Publish, Give a topic name in resource ARN field

Step 15 - Add iot:Receive

Step 16 - Add iot:Subscribe

Step 16.2 - Hit "Create"

Define actions in AWS IOT Core
Define actions

+1, We have created a policy, Lets attach the policy with the certificate we created. in step 7

Successfully created policy in AWS IOT Core
Successfully created policy

Step 17 - Find Secure > Certificates on left sidebar

Step 18 - Click on the certificate name

Create certificate in AWS IOT Core
Create certificate

Step 19 - Click the action dropdown, Click "Attach policy"

Attach policy with certificate in AWS IOT Core
Attach policy with certificate

Step 20 - Select the policy we created in step 16.2

Step 21 - Hit "Attach"

Attach policy in AWS IOT Core
Attach policy

Wooh! Level up

So far we had created a thing, Generated certificate for our thing, Created policy (Which defines access level), We have attached policy with certificate. So next we will create an user and thats it.

Step 22 - Search and find "IAM" on main search bar

Find user in AWS IOT Core
Find user

Step 23 - Navigate to Access management > Users and hit Create user

Create user in AWS IOT Core
Create user

Step 24 - Give a user name

Step 25 - Check "Programmatic access"

Add user in AWS IOT Core
Add user

Step 26 - Select "Attach existing policies directly"

Step 27 - Find and check AWSIoTDataAccess

in AWS IOT Core

Step 28 - Hit "Next Review" in step 3

Set permission in AWS IOT Core
Set permission

Step 29 - Create user

Create user in AWS IOT Core
Create user

Boom! User had been created, Time to copy the credentials generated.

Step 30 - Copy both Access key DI and Secrect access key and keep in a safe place

in AWS IOT Core
Copy credentials

Time for testing

Step 31 - Find "Test" in left sidebar

Step 32 - Type in a topic name (Uniqueness is prefered)

Step 33 - Hit "Publish" to broadcast the message to all the clients subscribed to the particulat topic

Testing publish/subcribe from AWS testing panel
Testing pub/sub from AWS testing panel

You client can now subscribe to the topic and receive data in realtime. On next post I had covered how to create a client and Connect to AWS MQTT using Signature Version 4

Received published message in browser
Received published message in browser

AWS dashboard

You can explore your AWS IoT core dashboard to find and track statistics of usage as there is charge of using this service.

AWS IoT Core monitor
AWS IoT Core monitor


Recommended

  1. Getting started with MQTT
  2. Connect to AWS MQTT using signature version 4
  3. Connect to AWS MQTT using certificates

TAGS