【问题标题】:Terraform - Cannot include the given value in a string template: string requiredTerraform - 不能在字符串模板中包含给定值:需要字符串
【发布时间】:2021-03-22 04:09:49
【问题描述】:

我正在尝试使用 terraform 在 SageMaker 上创建模型,关注 this page 由于权限限制,我无法为 sagemaker 角色分配完全访问策略,因此我创建了一个角色并附加了一个具有部分权限的策略

当我测试Terraform plan 时,它给了我这个:

Error: Invalid template interpolation value
...
..........................
 141:                 "ecr:GetRepositoryPolicy"
 142:             ],
 143:             "Resource": [
 144:                 "arn:aws:s3:::${aws_s3_bucket.xx_xxxxxxxxxx_xxx_bucket}",
 145:                 "arn:aws:s3:::${local.binaries_bucket_name}",
 146:                 "arn:aws:s3:::${aws_s3_bucket.xx_xxxxxxxxxx_xxx_bucket}/*",
 147:                 "arn:aws:s3:::${local.binaries_bucket_name}/*",
 148:                 "arn:aws:ecr:us-east-1:*:repository/*",
 149.....................
 157:         }
 158:     ]
 159: }
 160: POLICY
    |----------------
    | aws_s3_bucket.xx_xxxxxxxxxx_xxx_bucket is object with 25 attributes

Cannot include the given value in a string template: string required.

我是新手,只是想知道这是抱怨存储桶名称太长还是其他什么?我该怎么做才能解决这个问题,我有点困惑。非常感谢。

(PS:Terraform 版本v0.13.4 + 提供者registry.terraform.io/hashicorp/aws v3.20.0

【问题讨论】:

  • “大约 60 行不同的权限” - 你能提供强制的,准备好复制和粘贴评估代码吗?
  • @Marcin 您好我已经添加了所有权限,希望现在可以重现此错误。
  • 你检查过@Matt 的回答吗?没错。

标签: amazon-web-services amazon-s3 terraform terraform-provider-aws amazon-sagemaker


【解决方案1】:

看来您需要的是 S3 存储桶的 ARN,它由 exported resource attributes 提供。具体来说,您可能需要arn 资源属性。

更新您的政策,例如:

 144:             "${aws_s3_bucket.xx_xxxxxxxxxx_xxx_bucket.arn}",
 146:             "${aws_s3_bucket.xx_xxxxxxxxxx_xxx_bucket.arn}/*",

将通过访问arn 属性为您提供所需的字符串。当前编写的策略是访问aws_s3_bucket.xx_xxxxxxxxxx_xxx_bucket,它是该资源的每个参数和属性的映射(可能是对象),并且不会插入到您的策略字符串中。

【讨论】:

  • 您好,我试过了,错误消失了,但是当我查看创建的策略时,这两行变成了这样:arn:aws:s3:::arn:aws:s3:::xxxxxxxxxxxxx", "arn:aws:s3:::arn:aws:s3:::xxxxxxxx/*"
  • `````arn:aws:s3:::` 由于某种原因重复了......
  • @Cecilia 确保在您的政策中逐字使用我的答案,然后重复将消失。由于对我的回答进行了修改,因此存在重复。
  • 我的错,它现在可以工作了,非常感谢!
【解决方案2】:

我认为您需要正确创建自定义策略,然后将其链接到存储桶,您可以在此处查看一些示例: https://registry.terraform.io/modules/JousP/s3-bucket-policy/aws/latest/examples/custom-policy

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-18
    • 2021-07-04
    • 1970-01-01
    • 1970-01-01
    • 2011-03-18
    • 2018-05-15
    • 2021-06-23
    • 2021-01-26
    相关资源
    最近更新 更多