从Terraform模块导入值

时间:2018-08-16 15:44:49

标签: terraform terraform-template-file

我拥有我正在构建的两个Terraform模块的骨架:api-gatewaylambda。这是文件结构:

.
├── 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内部“导出”该值?

1 个答案:

答案 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}"
}