gpt4 book ai didi

amazon-web-services - 在 AWS 策略上的 Terraform 中使用模板文件,该策略使用 IAM 策略变量

转载 作者:行者123 更新时间:2023-12-03 08:45:57 24 4
gpt4 key购买 nike

我正在尝试使用 Terraform 在 AWS 中构建云基础设施。我想为 S3 存储桶添加一个策略,该策略通过 templatefile 使用基于属性的授权 (ABAC)地形的功能。我的问题是 terraform 和 AWS 使用的变量语法是相同的 ( ${...} )。

这是策略模板:

{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowReadRole1",
"Action": [
"s3:GetObject"
],
"Resource": "arn:aws:s3:::${bucketName}/*",
"Effect": "Allow",
"Principal": "*",
"Condition": {
"s3:ExistingObjectTag/myid": "${aws:PrincipalTag/myid}"
}
}
]
}

terrafrom 文件的相关部分是:

resource "aws_s3_bucket_policy" "mybuckets-policy" {
bucket = aws_s3_bucket.mybuckets[count.index].bucket
policy = templatefile("${path.module}/bucket-policy.json", {
bucketName = aws_s3_bucket.mybuckets[count.index].bucket
})
count = 2
}

所以我想要的是${bucketName}模板的一部分被 terraform 替换,同时保留 AWS 表达式 ${aws:PrincipalTag/user-id}到位。但是在上面的配置上运行 terraform 会导致错误消息

Call to function "templatefile" failed: ./bucket-policy.json:14,49-50: Extra characters after interpolation expression; Expected a closing brace to end the interpolation expression, but found extra characters..

如果我放另一个项目${foobar}在我的模板中,没有指定它的值,错误消息是

Invalid value for "vars" parameter: vars map does not contain key "foobar", referenced at ./bucket-policy.json:11,30-36.

如何让 terraform 对模板文件进行部分评估,同时保持所有其他项目完好无损?

最佳答案

在上面的示例中,语法 ${} 将导致 Terraform 尝试将该字段计算为插值函数。由于您想按字面意思使用该值,而不是作为插值函数,因此需要使用两个 $ 符号进行双重转义。

$${aws:PrincipalTag/用户 ID}

关于amazon-web-services - 在 AWS 策略上的 Terraform 中使用模板文件,该策略使用 IAM 策略变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61478347/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com