Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terrafrom aws_iam_policy_document condition correct syntax

How can this S3 bucket IAM policy, which has multiple conditions, be re-written as aws_iam_policy_document data block, please?

    "Condition": {
      "StringEquals": {
          "s3:x-amz-acl": "bucket-owner-full-control",
          "aws:SourceAccount": "xxxxxxxxxxxx"
      },
      "ArnLike": {
          "aws:SourceArn": "arn:aws:s3:::my-tf-test-bucket"
      }
    }

With the aws_iam_policy_document condition data block syntax 1:

    condition {
      test = "StringEquals"
      values = []
      variable = ""
    }
like image 984
Maciej Avatar asked Jul 10 '20 10:07

Maciej


People also ask

What is Aws_iam_policy_document?

Data Source: aws_iam_policy_document. Generates an IAM policy document in JSON format. This is a data source which can be used to construct a JSON representation of an IAM policy document, for use with resources which expect policy documents, such as the aws_iam_policy resource.

How do you attach IAM policy to IAM role in terraform?

Attaching the policy to the role using Terraform: This is where, we'll be attaching the policy which we wrote above, to the role we created in the first step. The terraform script: The aws_iam_policy_attachment in the above resource block, is used to attach a Managed IAM Policy to user(s), role(s), and/or group(s).

What is Aws_iam_role?

Table: aws_iam_role. An IAM role is an AWS Identity and Access Management (IAM) entity with permissions to make AWS service requests.


1 Answers

The aws_iam_policy_document supports nested condition directives. The following Terraform configuration should help:

data "aws_iam_policy_document" "iam_policy_document" {
  condition {
    test = "StringEquals"

    values = [
      "bucket-owner-full-control"
    ]

    variable = "s3:x-amz-acl"
  }

  condition {
    test = "StringEquals"

    values = [
      "xxxxxxxxxxxx"
    ]

    variable = "aws:SourceAccount"
  }

  condition {
    test = "ArnLike"

    values = [
      "arn:aws:s3:::my-tf-test-bucket"
    ]

    variable = "aws:SourceArn"
  }
}

like image 108
jasonwalsh Avatar answered Sep 22 '22 07:09

jasonwalsh