首页 > 解决方案 > 使用 AWS API Gateway API 的问题

问题描述

我有一个使用 AWS API Gateway 创建的简单 HTTP API,它使用 lambda 集成来返回一些数据。我还使用 route53 (CNAME) 使用自定义 DN 对其进行了配置

最近我在调用端点时遇到以下错误

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.

然后它消失了,它又可以工作了吗?嗯?有任何想法吗?

标签: amazon-web-servicesaws-lambdaaws-api-gateway

解决方案


好的,由于以下帖子,我发现了问题所在

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

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

相反,您需要做的是创建一个别名 A 记录,从您的自定义 DN 到您的区域 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
  }
}

希望这可以为将来节省很多时间。


推荐阅读