AWS_Policy Json related

Hi Team ,

While trying the following code for policy attachment to new user
resource “aws_iam_user” “admin_user” {
name = “Arjun”
tags = {
Description = “Technical Team Leader”
}

}
resource “aws_iam_policy” “adminuser” {
name = “AdminUser”
policy = <<EOF

{
“Version”: “2012-10-17”,
“Statement”: [
{
“Effect”: “Allow”,
“Action”: “",
“Resource”: "

}
]
}

EOF

}

resource “aws_iam_policy_attachment” “arjun-admin-access” {
user = aws_iam_user.admin_user.name
policy_arn = aws_iam_policy.adminuser.arn

}

It says that Policy contains an invalid json policy : leading space characters are not allowed

Can you please help it seems to be with policy = <<EOF tried similar to Chapter video

Thanks in advance

There’s an empty line between policy = <<EOF and the { bracket; if you remove it, Terraform will complain about some errors that should be easy to fix.

A suggestion: when you copy a piece of code, use the button </>; this will keep the formatting and make it easier to read.

Example:

resource "aws_iam_policy" "adminuser" {
  name   = "AdminUser"
  policy = <<EOF
{
  "Version": "2012-10-17",
  "Statement": [
    {
    "Effect": "Allow",
    "Action": "",
    "Resource": ""
    }
  ]
}
EOF
}
1 Like

Thanks a ton …sure will try and pick your suggestion as well @Matteo-Marchelli

You’re welcome!
Let me know if you have trouble with the other errors.

One other thing: I moved from the heredoc syntax (<<EOF) where possible, using jsonencode.

The resource above would be:

resource "aws_iam_policy" "adminuser" {
  name = "AdminUser"
  policy = jsonencode({
    "Version" : "2012-10-17",
    "Statement" : [
      {
        "Effect" : "Allow",
        "Action" : "",
        "Resource" : ""
      }
    ]
  })
}

This is because with terraform fmt, I can format the code automatically and/or see if there’s an error like a missing comma.

I hope it’s useful