【发布时间】: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