Terraform-provider-aws: Connect Route53 to API Gateway V2 Websocket using Hosted Zone id and Domain Name

Created on 20 Apr 2020  ·  13Comments  ·  Source: hashicorp/terraform-provider-aws

I'm trying to build an API Gateway Websocket API and then connect it to a Route53 domain name.

With the v1 api gateway I could do this by something similar to this

# The domain name to use with api-gateway
resource "aws_api_gateway_domain_name" "domain_name" {
  domain_name = local.route53_api_sub_domain_name

  certificate_arn = aws_acm_certificate.cert.arn
}

resource "aws_route53_record" "sub_domain" {
  name    = local.route53_api_sub_domain_name
  type    = "A"
  zone_id = data.aws_route53_zone.root_domain.id

  alias {
    name                   = aws_api_gateway_domain_name.domain_name.cloudfront_domain_name
    zone_id                = aws_api_gateway_domain_name.domain_name.cloudfront_zone_id
    evaluate_target_health = false
  }
}

The alias parameters would connect them together. But I'm trying to do this with a Websocket API and I can't find the information being exported. I think that it's not currently being exported cause I can't find the information in the documentation.

According to @ewbankkit, it's possible, but needs to be implemented:

Route 53 hosted zone and API Gateway domain name are available from the underlying AWS GetDomainName API.

If anybody is bored in self-isolation and fancies having a go at quickly adding these fields, then it would make my day! Danke!

enhancement servicapigateway servicroute53

Most helpful comment

The use of a list is an artifact of the way that nested configuration blocks are created inside Terraform.
I have opened https://github.com/terraform-providers/terraform-provider-aws/pull/12920 to add a documentation example of the RR association:

resource "aws_apigatewayv2_domain_name" "example" {
  domain_name = "http-api.example.com"

  domain_name_configuration {
    certificate_arn = "${aws_acm_certificate.example.arn}"
    endpoint_type   = "REGIONAL"
    security_policy = "TLS_1_2"
  }
}

resource "aws_route53_record" "example" {
  name    = "${aws_apigatewayv2_domain_name.example.domain_name}"
  type    = "A"
  zone_id = "${aws_route53_zone.example.zone_id}"

  alias {
    name                   = "${aws_apigatewayv2_domain_name.example.domain_name_configuration.0.target_domain_name}"
    zone_id                = "${aws_apigatewayv2_domain_name.example.domain_name_configuration.0.hosted_zone_id}"
    evaluate_target_health = false
  }
}

All 13 comments

@christhomas Looking at this more deeply, you should be able to access the attributes you want via aws_apigatewayv2_domain_name.domain_name.target_domain_name and aws_apigatewayv2_domain_name.domain_name.hosted_zone_id.

Hey, unfortunately not. Those are parameters given to the resource, not parameters that are available on the resource, check this out when I tried what you suggested

Error: Unsupported attribute
  on terraform/route53.tf line 14, in resource "aws_route53_record" "sub_domain":
  14:     name                   = aws_apigatewayv2_domain_name.domain_name.target_domain_name

This object has no argument, nested block, or exported attribute named
"target_domain_name".

Error: Unsupported attribute
  on terraform/route53.tf line 15, in resource "aws_route53_record" "sub_domain":
  15:     zone_id                = aws_apigatewayv2_domain_name.domain_name.hosted_zone_id

This object has no argument, nested block, or exported attribute named
"hosted_zone_id".

Maybe the parameters need exposing after they are computed, but then the documentation would need updates also to reference the attributes being available, right now they are not in the list of possible attributes

@christhomas Sorry, my typo.
Should be

  • aws_apigatewayv2_domain_name.domain_name.domain_name_configuration.target_domain_name
  • aws_apigatewayv2_domain_name.domain_name.domain_name_configuration.hosted_zone_id

I found experimentally that the following attributes are correct

    name                   = aws_apigatewayv2_domain_name.domain_name.domain_name_configuration[0].target_domain_name
    zone_id                = aws_apigatewayv2_domain_name.domain_name.domain_name_configuration[0].hosted_zone_id

But I have a question regarding the above. Why is this an array? is this an implementation mistake? Or there are multiple configurations?

In any case, the documentation doesn't make mention of this information. Please can you update it cause I think it would be REALLY REALLY helpful to anybody following me and trying to do the same thing

The use of a list is an artifact of the way that nested configuration blocks are created inside Terraform.
I have opened https://github.com/terraform-providers/terraform-provider-aws/pull/12920 to add a documentation example of the RR association:

resource "aws_apigatewayv2_domain_name" "example" {
  domain_name = "http-api.example.com"

  domain_name_configuration {
    certificate_arn = "${aws_acm_certificate.example.arn}"
    endpoint_type   = "REGIONAL"
    security_policy = "TLS_1_2"
  }
}

resource "aws_route53_record" "example" {
  name    = "${aws_apigatewayv2_domain_name.example.domain_name}"
  type    = "A"
  zone_id = "${aws_route53_zone.example.zone_id}"

  alias {
    name                   = "${aws_apigatewayv2_domain_name.example.domain_name_configuration.0.target_domain_name}"
    zone_id                = "${aws_apigatewayv2_domain_name.example.domain_name_configuration.0.hosted_zone_id}"
    evaluate_target_health = false
  }
}

Great, thanks!

Hmm, when I try to connect to the route53 domain name, I get a 400 error.

$ wscat -c wss://xxxxxxxxxxx.execute-api.eu-west-1.amazonaws.com/dev?authKey=123
Connected (press CTRL+C to quit)
$ wscat -c wss://ws.xxxxxx.com?authKey=123
error: Unexpected server response: 400

Have you tried a full setup with route53 domain and it's working?

I've realised that it's the api mapping resources which are missing which is what I need, so if you know the release date of those resources, that'd be great, otherwise I can use a null resource and patch it together for now.

aws_apigatewayv2_api_mapping has been merged to master so will be in the next release - most likely later this week.

It's working great, thanks!

I found experimentally that the following attributes are correct

    name                   = aws_apigatewayv2_domain_name.domain_name.domain_name_configuration[0].target_domain_name
    zone_id                = aws_apigatewayv2_domain_name.domain_name.domain_name_configuration[0].hosted_zone_id

But I have a question regarding the above. Why is this an array? is this an implementation mistake? Or there are multiple configurations?

In any case, the documentation doesn't make mention of this information. Please can you update it cause I think it would be REALLY REALLY helpful to anybody following me and trying to do the same thing

For me, it only works if I access the index of the array as suggested by @Christhomas, which is :
aws_apigatewayv2_domain_name.domain_name.domain_name_configuration[0].target_domain_name

However doing like below does not work for me:
aws_apigatewayv2_domain_name.domain_name.domain_name_configuration.target_domain_name
In my provider I have :
version = "~> 2.0"

I’m confused, the above method worked for you, but the new method doesn’t? What new method?

You can add more details please?
On 1. Jun 2020, 17:31 +0200, Benjamin notifications@github.com, wrote:

I found experimentally that the following attributes are correct
name = aws_apigatewayv2_domain_name.domain_name.domain_name_configuration[0].target_domain_name
zone_id = aws_apigatewayv2_domain_name.domain_name.domain_name_configuration[0].hosted_zone_id
But I have a question regarding the above. Why is this an array? is this an implementation mistake? Or there are multiple configurations?
In any case, the documentation doesn't make mention of this information. Please can you update it cause I think it would be REALLY REALLY helpful to anybody following me and trying to do the same thing
This worked for me, but the new solution proposed by @ewbankkit is not working.
In my provider I have :
version = "~> 2.0"

You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub, or unsubscribe.

I'm going to lock this issue because it has been closed for _30 days_ ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!

Was this page helpful?
0 / 5 - 0 ratings