7
votes

I want to send a message to a MQTT topic via AWS IoT in golang using AWS-SDK via HTTP,
when tried with below code it was unsuccessful. The response was :

ResourceNotFoundException: Not Found
status code: 404, request id: 3d2c0f11-09f6-4e86-94bf-ea877a30ebcd

The following is the code I use:

package main


import (
        "github.com/aws/aws-sdk-go/aws"
        "github.com/aws/aws-sdk-go/aws/session"
        "github.com/aws/aws-sdk-go/service/iotdataplane"
        "fmt"
)


func main(){
svc := iotdataplane.New(session.New(), &aws.Config {Region: aws.String("us-west-2"), Endpoint: aws.String("https://YOUR_PREFIX.iot.us-west-2.amazonaws.com")})

params := &iotdataplane.PublishInput{
        Topic:   aws.String("mytopic"), // Required
        Payload: []byte("PAYLOAD"),
        Qos:     aws.Int64(0),
}
resp, err := svc.Publish(params)

if err != nil {
        // Print the error, cast err to awserr.Error to get the Code and
        // Message from an error.
        fmt.Println(err.Error())
        return
}

// Pretty-print the response data.
fmt.Println(resp)

And I also verified there have valid credential setting and policies verified for my AWS account. The following is my aws credentials in ~/.aws/credentials

[default]
aws_access_key_id = YOUR_ACCESS_KEY_ID
aws_secret_access_key = YOUR_SECRET_ACCESS_KEY

and Policy attached into your identities:

{
"Version": "2012-10-17",
"Statement": [
    {
        "Effect": "Allow",
        "Action": [
            "iot:*"
        ],
        "Resource": "*"
   }
]

}

I also tried with Node.JS it works!! The following is the WORKING source in Node for reference:

var    AWS = require('aws-sdk');

AWS.config.update({region: 'us-west-2'});
var iotdata = new AWS.IotData({endpoint: 'YOUR_PREFIX.iot.us-west-2.amazonaws.com'});


var params = {
  topic: 'mytopic', /* required */
  payload: new Buffer('hello') || 'STRING_VALUE',
  qos: 0
};

iotdata.publish(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
});

For the Node JS works, it need a proper AWS.config file in the root folder. Like this:

accessKeyId = YOUR_ACCESS_KEY_ID
secretAccessKey = YOUR_SECRET_ACCESS_KEY
1
Hey Josiah, can you please have a look at my question? I am lost on how to find the accessKeyId stackoverflow.com/questions/35823058/…Mona Jalal
Hi Josiah, are you still having this issue? Have you verified that the topic is owned by the account your request is being made with, and that the region being use is correct. Does it help if you use the IoT control plane API "IoT.DescribeEndpoint" to get the endpoint address instead of hardcoding it?Jason D.
Did you ever get this working in Golang? As I have the same issue.jsgoecke
This worked for me as written, I was able to push MQTT message to a topic, however, I realized I need to use my thing public key as credentials and NOT my AWS credentials. I see nothing in Config.Credentials struct to specify keys yet. Tip: Add to aws.Config{} LogLevel: aws.LogLevel(aws.LogDebug), to see what's happening and use MQTT client in AWS console to see incoming messagelazieburd
Also note, you'll eventually run into this issue github.com/aws/aws-sdk-go/issues/706 where you cannot auth using device certs.lazieburd

1 Answers

0
votes

If still relevant to anyone... use Credentials *credentials.Credentials in aws.Config

see docs at: https://godoc.org/github.com/aws/aws-sdk-go/aws#Config