Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MalformedPolicyDocument error while creating an IAM Policy

I'm trying to create a managed policy by AWS CLI:

POLICY='
{
  "Version":"2012-10-17",
  "Statement":
  [{
    "Effect":"Allow",
    "Action":
    [
      "cloudformation:*"
    ],
    "Resource":"*"
  },
  {
    "Effect":"Deny",
    "Action":
    [
      "cloudformation:UpdateStack",
      "cloudformation:DeleteStack"
    ],
    "Resource": "'${arn}'"
  }]
}'

# Create policy if not already created
[ $(aws iam list-policies | grep -ce CloudFormation-policy-${StackName}) -eq 0 ] && (aws iam create-policy --policy-name CloudFormation-policy-${StackName} --policy-document "'${POLICY}'")

When I run the script I get this error:

An error occurred (MalformedPolicyDocument) when calling the CreatePolicy operation: Syntax errors in policy.

I can't figure out where the error is. Any idea?

like image 689
Souad Avatar asked Aug 16 '26 19:08

Souad


1 Answers

Each operating systems has its own way of treating single quote vs double quote escaping and as per AWS CLI documentation:

When passing in large blocks of data, you might find it easier to save the JSON to a file and reference it from the command line. JSON data in a file is easier to read, edit, and share with others.

Quoting Strings approach might not be best choice while passing Json data, instead use Loading parameters from file approach.

like image 105
kosa Avatar answered Aug 18 '26 23:08

kosa