How do you assign the output value of one resource as input to another?

Viewed 154

I have two .tf files in my root module:

the first one is called api-gateway.tf which provision an API Gateway in AWS:

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

I have got another .tf file called route53.tf which creates a Route53 record :

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
  }
}

I need to pass the api_endpoint and id of the apigateway to route53, but I don't know how?

I have tried returning these two values using output and reference that inside the route53 resource, however it doesn't work. It gives me an undeclared resource error.

How do you assign the output value of one resource as input to another?

1 Answers

Assuming that you're processing both TF files together, there is need to use output vars, that is simply a reference.

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


As an example, my api gateway block (in my api_gateway.tf) is

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"]
  }
}

whereas my route53 block (in my route53.tf) looks like this

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}"
  }
}
Related