IAM Role Mapping on EKS

0

I am working on IaC EKS using terraform. https://www.ahead.com/resources/automate-iam-role-mapping-on-amazon-eks I receive below error. Error: creating IAM Role (eks_admin): MalformedPolicyDocument: Invalid principal in policy: "AWS":"arn:aws:iam::xxxxxxxxxxx:user/eks-test-usr" status code: 400

resource "aws_iam_role" "eks_admin" {
  name = "eks_admin"

  assume_role_policy = jsonencode({
    Version = "2012-10-17"
    Statement = [
      {
        Action = "sts:AssumeRole"
        Effect = "Allow"
        Sid    = ""
        Principal: { "AWS" : "${var.assume_role}" }
      },
    ]
  })

  inline_policy {
    name = "eks_admin_policy"

    policy = jsonencode({
      Version = "2012-10-17"
      Statement = [
        {
          Action   = ["eks:DescribeCluster"]
          Effect   = "Allow"
          Resource = "*"
        },
      ]
    })
  }
}

I pass the variable as 
assum_role=["eks-test-dev","eks-test-admin"]
已提问 1 年前313 查看次数
1 回答
2

Hello rePost-User-9219791,

You are facing this issue because your terraform variable assume_role is a list of role names.

The Principal property of the IAM policy only allows role ARNs as valid principals. Therefore, pass the list of role ARNs instead of role names as shown below.

assume_role=["arn:aws:iam::<account_id>:role/eks-test-dev","arn:aws:iam::<account_id>:role/eks-test-admin"]

Also make sure that the above roles exist in your AWS account before creating the IAM role resource using terraform.

I hope this helps!

profile pictureAWS
支持工程师
已回答 1 年前
  • Thanks. I did the same, but the list I passed didn't exist in my AWS account.

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则