Question: How do I resolve "MalformedPolicyDocument" errors in AWS CloudFormation?

When I try to create or update an AWS CloudFormation stack, I get a "MalformedPolicyDocument" error message. I still get the error, even after I validate the template.

Short descriptionThe ValidateTemplate API in AWS CloudFormation can validate only the syntax of your template. The API can't validate the property values that you specify for a resource. Because a policy document is configured as part of the property value, the validity of the policy isn't verified.You get the "MalformedPolicyDocument" error when the policy document isn't syntactically or semantically correct, according to the grammar of the policy language.To resolve this error, you must confirm that the policy document is valid for the particular resource type that it's a part of.

ResolutionLook for error message details in your stack eventsOpen the AWS CloudFormation console.From the navigation pane, choose Stacks.Choose the stack that returned the error, and then choose the Events tab.Search the Status reason column for a message that explains the cause of the error.If you still can't find the cause of the error, then complete the steps in the Get more information on the AWS CloudTrail event that caused the error section.Get more information on the AWS CloudTrail event that caused the errorYou can get a more information by checking the errorMessage property of the CloudTrail event for the operation on your resource type that caused the error.For example, if "MalformedPolicyDocument" results from an error in an inline policy that's attached to an AWS Identity and Access Management (IAM) role, complete the following steps:Open the AWS CloudTrail console.From the navigation pane, choose Event history.In the Filter search box, select Event name as the lookup attribute, and then enter PutRolePolicy in the corresponding text box.For Time range, set the time of the CloudTrail event to the time that you see in the error message shown in AWS CloudFormation events.In the Event name column, choose your event.From the Event record, check the value of the errorMessage property for a detailed message.Validate the policy passed in the CloudTrail eventThe CloudTrail event for the API-level action on the resource that's causing the error usually contains the resolved form of the policy document. You can copy this resolved policy document, and then create a new policy in the AWS Management Console directly for that particular resource.For example, if "MalformedPolicyDocument" results from an error in an inline policy attached to an IAM role, complete the following steps:Open the CloudTrail console.From the navigation pane, choose Event history.In the Filter search box, select Event name as the lookup attribute, and then enter PutRolePolicy in the corresponding text box.For Time range, set the time of the CloudTrail event to the time that you see in the error message shown in AWS CloudFormation events.In the Event name column, choose your event.From the Event record, copy the value of the policyDocument property under the requestParameters.In a text editor, remove any "" escape characters from the policy document.Open the IAM console.From the navigation pane, choose Policies.Choose Create policy, and then choose the JSON tab.Enter the policy document that you copied from step 6, and then choose Review policy.Check the contents of the error message shown in the red dialogue box at the beginning of the page. The error message provides a more detailed explanation of the validation failure of the policy.

Related informationGrammar of the IAM JSON policy languageIAM JSON policy elements reference