如何将一种资源的输出值分配为另一种资源的输入?

时间:2020-05-18 13:53:45

标签: terraform terraform-provider-aws

我的根模块中有两个.tf文件:

第一个称为api-gateway.tf,它在AWS中设置了API网关:

resource "aws_apigatewayv2_api" "apiGateway" {
  name          = "some_Name"
  protocol_type = "HTTP"
}

output "api_gateway_endpoint" {
value = "${aws_apigatewayv2_api.apiGateway.api_endpoint}"
}
output "api_gateway_endpoint_id" {
value = "${aws_apigatewayv2_api.apiGateway.id}"
}

我还有一个名为.tf的{​​{1}}文件,它创建了一个route53.tf记录:

Route53

我需要将apigateway的resource "aws_route53_record" "www" { zone_id = "xxxxx" name = "someurl.com" type = "A" alias { name = "${output.api_gateway_endpoint}" zone_id = "${output.api_gateway_endpoint_id}" evaluate_target_health = false } } api_endpoint传递给route53,但我不知道如何?

我尝试使用id返回这两个值,并在route53资源中引用该值,但是它不起作用。它给了我output

如何将一种资源的输出值分配为另一种资源的输入?

1 个答案:

答案 0 :(得分:5)

假设您同时处理两个TF文件,则需要使用输出变量,这只是一个参考。

zone_id = "${aws_apigatewayv2_api.apiGateway.regional_zone_id}"


例如,我的api网关块(在api_gateway.tf中)是

resource "aws_api_gateway_domain_name" "devapi" 
{
  domain_name              = "${var.appLower}.${local.domain}"
  regional_certificate_arn = "${data.aws_acm_certificate.mts.arn}"

  endpoint_configuration {
    types = ["REGIONAL"]
  }
}

而我的route53块(在我的route53.tf中)看起来像这样

resource "aws_route53_record" "devapi" 
 {
  name    = "${aws_api_gateway_domain_name.devapi.domain_name}"
  type    = "A"
  zone_id = "${data.aws_route53_zone.mts.id}"

  alias {
    evaluate_target_health = true
    name                   = "${aws_api_gateway_domain_name.devapi.regional_domain_name}"
    zone_id                = "${aws_api_gateway_domain_name.devapi.regional_zone_id}"
  }
}