我拥有我正在构建的两个Terraform模块的骨架:api-gateway
和lambda
。这是文件结构:
.
├── modules
│ ├── api-gateway
│ │ ├── main.tf
│ │ ├── outputs.tf
│ │ └── variables.tf
│ └── lambda
│ ├── main.tf
│ ├── outputs.tf
│ ├── policies
│ │ └── lambda-role.json
│ └── variables.tf
├── main.tf
├── provider.tf
├── sandbox-environment.tfvars
└── variables.tf
modules/api-gateway/main.tf
的(摘录)内容为:
resource "aws_api_gateway_integration" "lambda_root" {
...
uri = "${aws_lambda_function.fn_name.invoke_arn}"
}
resource "aws_api_gateway_integration" "lambda" {
...
uri = "${aws_lambda_function.fn_name.invoke_arn}"
}
module "lambda" {
source = "../lambda"
}
modules/lambda/main.tf
的(摘录)内容为:
resource "aws_lambda_function" "fn_name" {
filename = "${data.archive_file.fn_name.output_path}"
...
runtime = "java8"
}
问题是我无法读取${aws_lambda_function.fn_name.invoke_arn}
中的值modules/api-gateway/main.tf
:
$ terraform init
Initializing modules...
- module.pipeline
Error: resource 'aws_api_gateway_integration.lambda_root' config: unknown resource 'aws_lambda_function.fn_name' referenced in variable aws_lambda_function.fn_name.invoke_arn
Error: resource 'aws_api_gateway_integration.lambda' config: unknown resource 'aws_lambda_function.fn_name' referenced in variable aws_lambda_function.fn_name.invoke_arn
是否可以从modules/api-gateway/main.tf
内部“导出”该值?
答案 0 :(得分:1)
您需要在modules/lambda/outputs.tf
中添加一个输出变量。
output "lambda_invoke_arn" {
value = "${aws_lambda_function.fn_name.invoke_arn}"
}
然后在modules/api-gateway/main.tf
中,您可以引用lambda模块的输出。
resource "aws_api_gateway_integration" "lambda" {
...
uri = "${module.lambda.lambda_invoke_arn}"
}