Akhil Balakrishnan
Akhil Balakrishnan

Reputation: 616

AWS Node JS SDK - Amazon IOT - MalformedPolicyException: Policy document is Malformed

I was trying to generate an IOT policy using AWS SDK for Node JS using the following code.

var params = {
  policyDocument: 'file:///tmp/mypolicy.json', 
  policyName: 'my_custom_policy'
};
iot.createPolicy(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
});

But running the script returns the following exception.

[MalformedPolicyException: Policy document is Malformed]

message: 'Policy document is Malformed',

code: 'MalformedPolicyException', etc

I have tried /tmp/mypolicy.json, ./mypolicy.json (after copying json file in the script's folder) etc. But the exception keeps on happaning.

The contents of the json is given below (Copied from Amazon official documentation).

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

Any idea about the correct method to specify policyDocument and generate IOT policy?

Upvotes: 0

Views: 236

Answers (1)

Vavrin Chen
Vavrin Chen

Reputation: 36

I encountered the same problem in Golang SDK, because it does not support the "file://" path, this is a feature specific to the CLI. To load a policy from disk you should use the Go stdlib open and read the contents in as a string.

file, err := os.Open("filename") 
if err != nil { 
// handle error 
} 

buf := &bytes.Buffer{} 
if err := io.Copy(buf, file); err != nil { 
// handle error 
} 

resp, err := svc.CreatePolicy(&iot.CreatePolicyInput{ 
PolicyDocument: aws.String(buf.String()), 
// ... 
}) 
if err != nil { 
// handle error 
} 

Upvotes: 1

Related Questions