Help us understand the problem. What is going on with this article?

Terraform + AWS環境でIAMポリシーを書いていたら「MalformedPolicyDocument: Has prohibited field Resource」というエラーが出た

全く大した話ではなく、TerraformでEC2にIAMポリシーを割り当てようとコードを書いていたら掲題のエラーが出力されて少し調べたのでメモ。

Terraform: Error creating IAM Role. MalformedPolicyDocument: Has prohibited field Resource

使っているTerraformのバージョンは0.12.6

その時のTerraformのコードはこんな感じです。

resource "aws_iam_role" "this" {
  name = "iam_role"

  assume_role_policy = <<EOF
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "",
            "Effect": "Allow",
            "Action": [
                "eks:UpdateClusterVersion",
                "eks:DescribeCluster",
                "eks:ListClusters"
            ],
            "Resource": "*"
        }
    ]
}
EOF

resource "aws_iam_instance_profile" "this" {
  name = "profile"
  role = aws_iam_role.this.name
}

よくよく考えるとassume-roleブロックに直接ポリシーを書いているのはまあ、違和感があるかも。。。
とか考えてました。

結論

aws_iam_roleには信頼ポリシーと呼ばれる記法で委譲する対象を指定しなければならない。
元々期待していたポリシーに関する記述はaws_iam_role_policyと言うresourceに書きましょう。

認証、認可やassume-roleの概念について雰囲気でしか理解していなかったため、
うっかりassume-roleに直接ポリシー書いてしまっていたと言うチョンボなお話でした。

下記の様なコードに修正。

resource "aws_iam_role" "operation_iam_role" {
  name = "iam_role"

// ここに権限を委譲するポリシーを書く
  assume_role_policy = <<EOF
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "",
            "Effect": "Allow",
            "Principal": {
            "Service": "ec2.amazonaws.com"
            },
            "Action": "sts:AssumeRole"
        }
    ]
}
EOF
}

// このresourceを追加してポリシーを記載する
resource "aws_iam_role_policy" "this" {
  name   = "iam_role_policy"
  role   = aws_iam_role.this.id
  policy = <<EOF
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "",
            "Effect": "Allow",
            "Action": [
                "eks:UpdateClusterVersion",
                "eks:DescribeCluster",
                "eks:ListClusters"
            ],
            "Resource": "*"
        }
    ]
}
EOF
}

resource "aws_iam_instance_profile" "this" {
  name = "profile"
  role = aws_iam_role.this.name
}

無事にapplyし、EC2にIAM-Roleを割り当てる事ができました。

参考

AWS IAMポリシーを理解する
Terraformを使ってEC2インスタンスにIAMロールを当ててみる

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away