【问题标题】:terraform cyclic dependeny for aws api gateway with resource policy具有资源策略的 aws api 网关的 terraform 循环依赖项
【发布时间】:2020-06-03 17:53:26
【问题描述】:

问题陈述 我正在尝试使用 terraform 使 aws api 网关自动化是我代码的一部分

用于 api 网关

resource "aws_api_gateway_rest_api" "rest_api" {
  #some code
policy = "${data.template_file.init.rendered}"
}

output "id" {
  value = "${aws_api_gateway_rest_api.rest_api.id}"
}

output "execution_arn" {
  value = "${aws_api_gateway_rest_api.rest_api.execution_arn}"
}

output "arn" {
  value = "${aws_api_gateway_rest_api.rest_api.arn}"
}

资源政策 请注意,我想在 json 策略文档中自动插入 api id

data "aws_region" "current" {}
data "aws_caller_identity" "current" {}
data "template_file" "init" {
  template = "${file("${path.root}/${var.policy_file_location}")}"
  vars = {
    current_region      = "${data.aws_region.current.name}"
    current_aws_account = "${data.aws_caller_identity.current.account_id}"
    current_api_id              = "${aws_api_gateway_rest_api.rest_api.id}"
  }
}

json 政策

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": "*",
            "Action": "execute-api:Invoke",
            "Resource": "arn:aws:execute-api:${current_region}:${current_aws_account}:${current_api_id}/*"
        }
    ]
}

当我尝试提供类似于以下的资源策略时,我得到了

错误:循环:module.simple-api-gw.data.template_file.init, module.simple-api-gw.aws_api_gateway_rest_api.rest_api

我该如何解决这个错误?我想在 json 文件中动态提供 api id。

【问题讨论】:

  • 我也有同样的问题。我最终硬化了 variables.tf 中的 api id,并使用该变量将其插入到策略中。它仍然是硬编码(事实上也是如此),但至少我不必在策略中硬编码 id。

标签: amazon-web-services terraform aws-api-gateway


【解决方案1】:

您生成的策略将是appropriate to assign to an IAM role or IAM user,以允许他们调用 API。将该特定策略直接分配给 API Gateway 没有意义。本质上,您是在说“任何有权调用 API 的人都可以有权调用 API”,这是一个循环语句。

策略appropriate for assigning to an API Gateway 会执行诸如限制对特定主体或特定 IP 地址的请求。

请参阅我上面链接的文档。它概述了通过 IAM 权限或通过资源策略控制对 API 网关的访问的两种不同方法。您正在尝试将 IAM 权限分配为资源策略,但这是行不通的。

【讨论】:

  • 您存在循环依赖,因为您尝试将错误类型的 IAM 策略应用于 API 网关。我以为我在回答中清楚地说明了这一点。如果不重新考虑如何应用此 IAM 规则,就无法“解决”此问题。
【解决方案2】:

问题是您在创建资源时尝试引用资源的输出。您无需为策略中的资源填充执行 arn,而是使用“execute-api:/*”。

【讨论】:

    猜你喜欢
    • 2017-08-03
    • 2021-04-16
    • 2020-08-21
    • 2021-09-27
    • 1970-01-01
    • 2018-04-05
    • 2017-12-12
    • 1970-01-01
    • 2020-09-04
    相关资源
    最近更新 更多