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