[英]Syntax for conditions in aws_iam_policy resources in terraform
我目前正在尝试在 terraform 中实施一个 aws_iam_policy,它看起来像:
resource "aws_iam_policy" "policyName" {
name = "policyName"
path = "/"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
NotAction = [
"iam:*"
]
Resource = "*"
Condition = {
test = "StringEquals"
variable = "s3:prefix"
values = [
"home/"
]
}
}
]
})
}
但是,当我尝试在我在团队中使用的环境中部署它时,我收到一条错误消息,指出这存在语法错误(日志仅指出起始行,没有其他地方)。 当我取出条件块时,错误消失了,所以我知道这与条件有关。 我已经检查了 terraform 文档,他们在条件之后没有=
,但是当我删除=
时,我收到一条错误消息,说=
是预期的。 有谁知道正确的语法/正确的位置来寻找与此相关的文档(如前所述, https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document上的文档没有为我工作)?
作为 Joel Van Hollebeke 答案的替代方案,对原始文档中的 Condition 块进行简单更改也可以。
代替:
Condition = {
test = "StringEquals"
variable = "s3:prefix"
values = [
"home/"
]
}
用这个:
Condition = {
"StringEquals" = {
"s3:prefix" = "home/"
}
}
话虽如此,“aws_iam_policy_document”仍然是terraform中最推荐的做法。
我会尝试使用aws_iam_policy_document
数据块,如下例所示:
data "aws_iam_policy_document" "example" {
statement {
not_actions = [
"iam:*",
]
effect = "Allow"
resources = [
"*",
]
condition {
test = "StringEquals"
variable = "s3:prefix"
values = [
"home/"
]
}
}
}
然后添加一个引用此数据源的aws_iam_policy
资源:
resource "aws_iam_policy" "policyName" {
name = "policyName"
path = "/"
policy = data.aws_iam_policy_document.example.json
}
根据我的经验,这种模式在配置 IAM 策略时产生了最好的验证。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.