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.

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南