Setting up AWS MQTT in 10 minutes
- 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
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
Step 5 - Give your thing a name
Step 6 - Keep all as default, hit Next
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.
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
Congratulation! Your thing is 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
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"
+1, We have created a policy, Lets attach the policy with the certificate we created. in step 7
Step 17 - Find Secure > Certificates on left sidebar
Step 18 - Click on the certificate name
Step 19 - Click the action dropdown, Click "Attach policy"
Step 20 - Select the policy we created in step 16.2
Step 21 - Hit "Attach"
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
Step 23 - Navigate to Access management > Users and hit Create user
Step 24 - Give a user name
Step 25 - Check "Programmatic access"
Step 26 - Select "Attach existing policies directly"
Step 27 - Find and check AWSIoTDataAccess
Step 28 - Hit "Next Review" in step 3
Step 29 - 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
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
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
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.