我有一个使用API网关、Lambda和自定义域的CloudFormation堆栈。当我完全拆除堆栈并重新部署它时,我经常发现API网关处于一种状态;502坏网关";用于通过自定义域发出的每个API请求。
以下是其他一些观察结果:
- 我仍然可以使用标准执行URL(例如
<api-id>.execute-api.us-east-1.amazonaws.com
(成功调用API - AWS控制台中的所有域设置(API网关和路由53(看起来都是正确的
- 在AWS控制台中,我可以手动创建一个具有相同设置的备用自定义域,它可以工作
- 我尝试手动删除自定义域,并使用相同的设置重新创建它,但以相同的方式失败
- 唯一的解决方案似乎是删除堆栈并等待一两天重新部署
这个";感觉就像";API网关缓存错误,它仍然试图将我的自定义域路由到旧的(已删除的(API实例(尽管AWS控制台显示了正确的值(。
知道是什么原因导致了这个问题,或者如何调试它吗?我查看的日志似乎没有帮助,因为错误发生在我的API实际到达之前。
我的CloudFormation的相关部分如下:
AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
...
ApiDomainName:
Type: AWS::ApiGatewayV2::DomainName
Properties:
DomainName: !Ref DomainName
DomainNameConfigurations:
- CertificateArn: !Ref CertificateArn
ApiMapping:
Type: AWS::ApiGatewayV2::ApiMapping
Properties:
ApiId: !Ref ServerlessHttpApi
DomainName: !Ref ApiDomainName
Stage: !Ref ServerlessHttpApiApiGatewayDefaultStage
DNSRecord:
Type: AWS::Route53::RecordSet
Properties:
Name: !Ref DomainName
Type: A
AliasTarget:
DNSName: !GetAtt ApiDomainName.RegionalDomainName
HostedZoneId: !GetAtt ApiDomainName.RegionalHostedZoneId
HostedZoneId: !Ref HostedZoneId
> curl https://api.example.com
<html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
</body>
</html>
> curl https://xxxxxxxxx.execute-api.us-east-1.amazonaws.com
{"message":"Hello World"}%
在这里交叉发布到aws开发论坛:https://forums.aws.amazon.com/thread.jspa?messageID=960826
您似乎正在构建一个无服务器应用程序。为此,我将使用无服务器应用程序模型(SAM(:https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-getting-started.html
一旦切换,自定义域名的声明将如下所示:
MyApi:
Type: AWS::Serverless::Api
Properties:
BinaryMediaTypes: [image~1jpg]
StageName: !Ref Stage
EndpointConfiguration: REGIONAL
Domain:
DomainName: api.example.com
CertificateArn: <ARN of the Certificate from ACM>
EndpointConfiguration: REGIONAL
Route53:
HostedZoneId: <Hosted zone id from route 53>
- 如果您使用的是RDS aurora无服务器,它会在连续几分钟不活动后暂停计算能力,您可能会遇到此问题
- 等待2分钟后重试,问题通常会消失
- 既然你说问题经常出现,那可能是个问题。看一次