How to define AWS IAM attribute-based access control in terraform

Viewed 345

I am defining attribute-based access control (ABAC) for AWS IAM within my terraform file. Sample policy is

resource "aws_iam_role_policy" "testS3" {
name = "testS3"

policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
    {
        "Effect": "Allow",
        "Action": [
            "s3:ListBucket"
        ],
        "Resource": "arn:aws:s3:::dev-${aws:PrincipalTag/team}*"
    }
]
}
EOF
}

How do I call that ${block} within terraform? Terraform translates that into its own variables.

1 Answers

It worked with extra $ in the string.

resource "aws_iam_role_policy" "testS3" {
name = "testS3"

policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
   {
    "Effect": "Allow",
    "Action": [
        "s3:ListBucket"
    ],
    "Resource": "arn:aws:s3:::dev-$${aws:PrincipalTag/team}*"
   }
  ]
}
EOF
}

I also tried with variables.tf file and referenced the variable here in json.

variables.tf

variable "principaltag" {
default = "$${aws:PrincipalTag/tedteam}"
}

****

policy.tf

resource "aws_iam_role_policy" "testS3" {
name = "testS3"

policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
   {
    "Effect": "Allow",
    "Action": [
        "s3:ListBucket"
    ],
    "Resource": "arn:aws:s3:::dev-${var.principaltag}*"
   }
  ]
}
EOF
}
Related