简体   繁体   English

在 Terraform 内设置 IAM 策略

[英]Setting IAM Policy within Terraform

I am having troubles setting an IAM Policy following documentation on Terraform. While trying to assign a this policy to my S3 Bucket using this documentation from databricks我在按照 Terraform 上的文档设置 IAM 策略时遇到问题。尝试使用databricks 中的文档将此策略分配给我的 S3 存储桶时

the following error is being returned Policy document should not specify a principal.正在返回以下错误Policy document should not specify a principal.

You may reproduce using the following code section您可以使用以下代码部分进行复制

resource "aws_iam_policy" "databricks_bucket_policy" {
  name        = "databrick_bucket_policy"
  path        = "/"
  description = "A policy for Databricks S3 Bucket"

  # Terraform's "jsonencode" function converts a
  # Terraform expression result to valid JSON syntax.
  policy = jsonencode({
    "Version" : "2012-10-17",
    "Statement" : [
      {
        "Effect" : "Allow",
        "Principal" : {
          "AWS" : [
            "arn:aws:iam::414351767826:role/unity-catalog-prod-UCMasterRole-14S5ZJVKOTYTL",
            "arn:aws:iam::<YOUR_AWS_ACCOUNT_ID>:role/<THIS_ROLE_NAME>"
          ]
        },
        "Action" : "sts:AssumeRole",
        "Condition" : {
          "StringEquals" : {
            "sts:ExternalId" : "<DATABRICKS_ACCOUNT_ID>"
          }
        }
      }
    ]
  })
}

I've tried following this terraform doc but it is not fully understood .我已经尝试遵循此 terraform 文档,但尚未完全理解 I would appreciate if someone could clarify on how this can be done.如果有人可以澄清如何做到这一点,我将不胜感激。

You are trying to create an aws_iam_policy resource and assign it to an S3 bucket, but you can only assign an aws_s3_bucket_policy to a bucket.您正在尝试创建aws_iam_policy资源并将其分配给 S3 存储桶,但您只能将aws_s3_bucket_policy分配给存储桶。 An IAM policy is assigned to users or roles, so you never specify a principal in an IAM policy because it is directly assigned to the principal it is applied to. IAM 策略分配给用户或角色,因此您永远不会在 IAM 策略中指定委托人,因为它直接分配给它所应用的委托人。

By contrast a resource policy, such as the S3 bucket policy, is assigned to a resource, and you specify the principals that you are granting or denying access to the resource.相比之下,将资源策略(例如 S3 存储桶策略)分配给资源,并且您指定要授予或拒绝访问该资源的委托人。


Now, looking at the Databricks documentation, it appears what they are giving you in step 3 of the documentation is a trust relationship.现在,查看 Databricks 文档,他们在文档的第 3 步中为您提供的似乎是一种信任关系。 Step 4 has the IAM policy.第 4 步具有 IAM 策略。 They are also instructing you to create an IAM Role, not an S3 bucket policy.他们还指导您创建 IAM 角色,而不是 S3 存储桶策略。

It appears that what you are being instructed to do is create an IAM role that Databricks can assume, that gives Databricks access to the S3 bucket in your account.看起来您被指示要做的是创建一个 Databricks 可以承担的 IAM 角色,这使 Databricks 可以访问您帐户中的 S3 存储桶。 You are not being instructed to create an S3 bucket policy at all.根本没有指示您创建 S3 存储桶策略。

Your Terraform should look like this:您的 Terraform 应如下所示:

resource "aws_iam_role" "databricks_role" {
  name = "databricks_role"

  assume_role_policy = jsonencode(
    # The JSON from Step 3 goes here
  )
}

resource "aws_iam_policy" "databricks_role_policy" {
  name        = "databrick_role_policy"
  path        = "/"
  description = "A policy for Databricks IAM Role"

  policy = jsonencode(
    # The JSON from Step 4 goes here
  )
}

resource "aws_iam_role_policy_attachment" "databricks_role" {
  role       = aws_iam_role.databricks_role.name
  policy_arn = aws_iam_policy.databricks_role_policy.arn
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Terraform 一个策略到多个 IAM 角色 - Terraform one policy to multiple IAM roles GCP:使用 Terraform 从服务帐户中删除 IAM 策略 - GCP: Remove IAM policy from Service Account using Terraform 创建内联策略以通过 terraform 附加 IAM 用户 - Create Inline policy to attach with IAM user via terraform terraform managed_policy_arns 问题与 aws_iam_role - terraform managed_policy_arns issue with aws_iam_role Terraform AWS IAM 角色“inline_policy.0.policy”包含使用 ${file xyz} 和 jsonencode 的无效 JSON 策略 - Terraform AWS IAM Role “inline_policy.0.policy” contains an invalid JSON policy using ${file xyz} and jsonencode 如何确定使用 AWS Terraform 资源所需的 AWS IAM 策略权限? - How to determine the AWS IAM policy permissions needed to use AWS Terraform Resources? 使用 terraform 将 IAM 策略分配给 Google 云中的现有 Cloud Build 服务帐户失败 - Assigning IAM Policy to an existing Cloud Build Service Account in Google cloud using terraform fails 参考Terraform中用for_each创建的几个aws_iam_policy_document数据源 - Reference several aws_iam_policy_document data sources created with for_each in Terraform aws / terraform Iam - 内联 - aws / terraform Iam - inline 沙盒环境的 IAM 政策 - IAM Policy for Sanbox Enviornment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM