将 `aws_secretsmanager_secret_policy` 用于 AWS Secrets Manager 的 creating/managing 策略有什么好处?

What is the benefit of using `aws_secretsmanager_secret_policy` for creating/managing policy for AWS Secrets manager?

在 Terraform aws 提供程序中,我们可以使用下面的方法将基于资源的策略附加到机密管理器。

我记得提供程序的旧版本,例如 2.7 没有 aws_secretsmanager_secret_policy,我们不得不使用数据资源将策略附加到机密管理器。现在最新版本都支持了。

使用 aws_secretsmanager_secret_policyaws_iam_policy_document 有什么好处?在什么条件下我们可以选择一个而不是另一个?

aws_secretsmanager_secret_policy是创建一个resource-based policy, whereas aws_iam_policy_document is for identity-based policy. There is a number of differences between them as explained in Identity-based policies and resource-based policies.

使用 resource-based policy 的最常见情况是 cross-account access to your secret

我认为您的实际问题是关于在 aws_secretsmanager_secret 资源上设置 policy 属性,而不是将策略创建为单独的 aws_secretsmanager_secret_policy 资源。

您使用 aws_secretsmanager_secret_policy 而不是直接在秘密资源上设置它的主要原因是,如果秘密是在不同的 Terraform 代码中创建的,或者可能完全在 Terraform 之外。例如,如果您想创建一个 Terraform 模板来查找所有 AWS 机密,并为所有这些机密设置策略。

您可以将 aws_iam_policy_document 与其中任何一个一起使用。 aws_iam_policy_document 只是一种在 Terraform 代码中定义 IAM 策略的方法,而不是嵌入原始 JSON 字符串。