在使用AWS的简易策略服务(SCP)时,可能会遇到错误“MalformedPolicyDocumentException”,通常是由于策略文档格式错误或某些保留字符而导致。要解决此问题,请进行以下步骤:
1.将SCP策略文件中的所有JSON格式更改为正确的格式
例如,将策略文件中的:
{ "Version": "2012-10-17", "Statement": { "Effect": "Allow", "Action": "s3:", "Resource": "", } }
更改为:
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "s3:", "Resource": "" } ] }
2.检查SCP策略文件以查找可能存在的保留字符或拼写错误,并将其删除或更正。
3.在Terraform中,使用“jsonencode()”函数确保策略文件格式正确,并且可以再次使用SCP。
例如:
resource "aws_organizations_policy_attachment" "example" { policy_id = aws_organizations_policy.example.id target_id = aws_organizations_account.example.id
policy = jsonencode({ Version: "2012-10-17", Statement: [ { Action: "s3:", Effect: "Allow", Resource: "", } ] }) }
这些步骤应该可以解决“MalformedPolicyDocumentException”错误,并允许您成功使用AWS SCP服务。