LoginSignup
3
2

More than 1 year has passed since last update.

【AWS CLI】 aws iam create-policyで`The policy failed legacy parsing`となるときの対処法

Posted at

はじめに

AWS CLI でaws iam create-policyした際にThe policy failed legacy parsingと表示されハマったので、備忘録として残しておく。
「明らかに JSON は正しいのに何でや!」って人には、役に立つかもしれない。

$ aws iam create-policy --policy-name foo --policy-document '
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Resource": [
        "arn:aws:logs:ap-northeast-1::log-group:/aws/codebuild/*"
      ],
      "Action": [
        "logs:CreateLogGroup",
        "logs:CreateLogStream",
        "logs:PutLogEvents"
      ]
    },
    {
      "Effect": "Allow",
      "Resource": [
        "arn:aws:s3:::codepipeline-ap-northeast-1-*"
      ],
      "Action": [
        "s3:PutObject",
        "s3:GetObject",
        "s3:GetObjectVersion",
        "s3:GetBucketAcl",
        "s3:GetBucketLocation"
      ]
    }
  ]
}
'

An error occurred (MalformedPolicyDocument) when calling the CreatePolicy operation: The policy failed legacy parsing

環境

$ aws --version
aws-cli/2.1.28 Python/3.8.8 Linux/4.14.225-168.357.amzn2.x86_64 exec-env/CloudShell exe/x86_64.amzn.2 prompt/off

結論

こちらによると、--policy-documentの先頭の{の前に空白文字(改行、スペース等)を入れては駄目らしい。

確かに、空白文字をなくすことで問題なく作成できた。

$ aws iam create-policy --policy-name foo --path /service-role/ --policy-document '{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "s3:GetObject",
        "s3:GetObjectVersion"
      ],
      "Resource": [
        "arn:aws:s3:::hpm-prod-codebuild-src/*"
      ]
    },
    {
      "Effect": "Allow",
      "Resource": [
        "arn:aws:s3:::hpm-prod-codebuild-src"
      ],
      "Action": [
        "s3:ListBucket",
        "s3:GetBucketAcl",
        "s3:GetBucketLocation"
      ]
    }
  ]
}
'

ちなみに、JSON をファイル形式 (--policy-document file://foo.json)で指定した場合も、先頭に空白文字が含まれるとエラーになった。

おわりに

公式ドキュメントには以下のようにあるが、これだけ見ると改行(line feed)は許可されているように見える。
事実、一番最初以外の改行は問題なく読み込まれる。

--policy-document (string)

The JSON policy document that you want to use as the content for the new policy.

You must provide policies in JSON format in IAM. However, for AWS CloudFormation templates formatted in YAML, you can provide the policy in JSON or YAML format. AWS CloudFormation always converts a YAML policy to JSON format before submitting it to IAM.

To learn more about JSON policy grammar, see Grammar of the IAM JSON policy language in the IAM User Guide .

The regex pattern used to validate this parameter is a string of characters consisting of the following:

Any printable ASCII character ranging from the space character (\u0020 ) through the end of the ASCII character range
The printable characters in the Basic Latin and Latin-1 Supplement character set (through \u00FF )
The special characters tab (\u0009 ), line feed (\u000A ), and carriage return (\u000D )

不思議……。
※AWS に問い合わせたものの、具体的な処理ロジックについては教えていただけなかった。
aws/aws-cli at v2を見たものの、エラー文言で検索しても何も出てこず。ご存じの方いらっしゃいましたら、ご教示いただきたいです。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2