Raziza O
Raziza O

Reputation: 1646

How do I publish/Subscribe AWS IoT - server side using Java SDK

I'm really new with AWS and IoT, and my goal is to:

  1. Use the Java SDK v.2 from my serverless application to create/get/update/attach/... certificates and things.

  2. Create client side MQTT demo application to connect publish and subscribe to messages used by my new certificates and thing created in phase 1.

  3. Publish/subscribe messages in the server side in order to talk to my things/clients.

1 & 2 I've managed to do perfectly. But I don't understand how should I do the 3rd one.

In order to connect to IoT Core from the server I first configure my SSO connection using the AWSCLI and in the code I simply use my profile name and region to connect.

Upvotes: 2

Views: 1418

Answers (1)

Chris Miller
Chris Miller

Reputation: 350

Your serverless Java application needs to be configured as a "Thing" in the same account/region as your IoT devices. In the console, go to

AWS IoT -> Manage -> Things

and create a thing for your app. In this case you shouldn't need a "Device Shadow", and you can select "Auto Generate Certificates".

For the IoT Policy, you will need the following :

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "iot:Connect",
      "Resource": "arn:aws:iot:us-east-1:YOUR_AWS_ACCOUNT_ID:client/*"
    },
    {
      "Effect": "Allow",
      "Action": "iot:Subscribe",
      "Resource": "arn:aws:iot:us-east-1:YOUR_AWS_ACCOUNT_ID:topicfilter/*"
    },
    {
      "Effect": "Allow",
      "Action": "iot:Receive",
      "Resource": "arn:aws:iot:us-east-1:YOUR_AWS_ACCOUNT_ID:topic/*"
    },
    {
      "Effect": "Allow",
      "Action": "iot:Publish",
      "Resource": "arn:aws:iot:us-east-1:YOUR_AWS_ACCOUNT_ID:topic/*"
    }
  ]
}

Your application will communicate with IoTCore using the endpoint shown in the Settings screen in IoTCore for the region where you have created your thing. Your application will authenticate using the key/cert you downloaded when creating the thing (username/password auth is not allowed).

Once your application connects to the endpoint, you will want to "subscribe" to the same topic your devices use to send messages. You can also publish to one or more topics.

In order to debug communications, you can use the MQTT client in the AWS IoTCore console, just note the console needs to be refreshed periodically when communication times out. I recommend marking your topics as favorites so they are easy to re-subscribe to on a refresh.

As for coding in Java, you should be able to leverage examples from the AWS IoT Device SDK here :

https://github.com/aws/aws-iot-device-sdk-java-v2/tree/main/samples

Here's a link to the MQTT client class :

http://aws-iot-device-sdk-java-docs.s3-website-us-east-1.amazonaws.com/com/amazonaws/services/iot/client/AWSIotMqttClient.html

Please note that your app will not have access to messages when not in use. There are a few strategies to deal with message persistence, but that's outside the scope of your question, so I won't cover it here.

Hopefully this gets you pointed in the right direction.

Upvotes: 1

Related Questions