AWS API Gateway的API调用问题

7
我有一个简单的HTTP API,使用AWS API Gateway创建,并使用lambda集成返回一些数据。我还使用route53(CNAME)配置了自定义域名。
最近在调用端点时遇到以下错误:
Error: Hostname/IP does not match certificate's altnames: Host: xxxxxx. is not in the 
cert's altnames:DNS:*.execute-api.eu-west-2.amazonaws.com

有人能解决这个问题吗?我使用AWS证书管理器为我的自定义域设置了证书,因此所有服务均为AWS服务,但出于某种原因它突然停止工作了。

谢谢 安德鲁


编辑:我奇怪地间歇性遇到此问题,当我在浏览器中调用API时,会出现以下错误:

This server could not prove that it is api.xxxx.co.uk; 
its security certificate is from *.execute-api.eu-west-2.amazonaws.com. 
This may be caused by a misconfiguration or an attacker 
intercepting your connection.

它突然消失了,然后又可以正常工作?哎呀?有什么想法吗?


你的证书是应用在哪里了?当你说你安装了一个证书,具体是安装/应用到了哪里? - Todd Price
你在调用端点时使用了那个子域名吗? - Todd Price
嗨,托德。是的,正确的,当我进行API调用(例如从PostMan)时,我使用https://api.xxx.com进行调用,并且出现错误。但是API本身没有问题,因为如果我使用原始的AWS生成的域名,即CNAME到我的域名,它可以正常工作,没有任何问题。 - Andrew Kew
嗨Pududu - 不,我使用的子域是api.xxx.com,这是我用来访问API端点的,这也是注册到我的SSL证书以及配置在我的AWS API Gateway APU中作为自定义域的相同子域。 - Andrew Kew
顺便说一下,我的证书已经在ACM注册为api.xxx.com而不是xxx.com。 - Andrew Kew
显示剩余2条评论
1个回答

9

好的,感谢以下帖子,我已经找到了问题所在。

如果您查看原始帖子底部的评论,作者已经解决了问题,但并未将其作为帖子的答案发布,因此您需要阅读所有内容才能找到答案。

问题是,您需要确保在route53中正确设置DNS。我最初是从我的自定义DN创建CNAME到API的调用URL。

相反,您需要从自定义DN创建一个ALIAS A记录,指向您区域性API的DN(前缀为d-*)。

注意:这与您的调用URL不同

通过进行这些更改,我所有的问题都消失了。

对于任何在Terraform中执行此操作的人,您需要的是:

//HTTP API using quick create (regional)
resource "aws_apigatewayv2_api" "qc_technical_test" {
  name          = "qc_technical_test"
  protocol_type = "HTTP"
  target        = aws_lambda_function.tt_lambda.arn
  route_key = "GET /persons/address"
}

//custom domain name for API (regional)
resource "aws_apigatewayv2_domain_name" "qc_tt_custom_domain" {
  domain_name = "api.${aws_route53_zone.quadcorps.name}"

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

//route53 alias a record to api
resource "aws_route53_record" "tt_api" {
  zone_id = aws_route53_zone.quadcorps.zone_id
  name = aws_apigatewayv2_domain_name.qc_tt_custom_domain.domain_name
  type = "A"

  alias {
    name = aws_apigatewayv2_domain_name.qc_tt_custom_domain.domain_name_configuration.0.target_domain_name
    zone_id = aws_apigatewayv2_domain_name.qc_tt_custom_domain.domain_name_configuration.0.hosted_zone_id
    evaluate_target_health = false
  }
}

希望这能在未来帮助有需要的人节省大量时间。

4
谢谢。我不能说它为我节省了很多时间,但它让我免于跳桥。 - Pikaro
对于其他研究此问题的人 - 在我的情况下,我所需要做的就是使用以“d-”为前缀的API网关URL。我为此使用了CNAME记录,不需要使用A记录。 - Rich

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接