简体   繁体   English

当服务(在 ecs fargate 上)返回 503 时,AWS 弹性负载均衡器能否将请求重定向到 lambda function

[英]Can AWS Elastic load balancer redirect the request to a lambda function when the service (on ecs fargate) returns 503

The goal is that when the service is unavailable I'd redirect it to the lambda function.目标是当服务不可用时,我会将其重定向到 lambda function。

Requests going through AWS Elastic Loadbalancer to service in ECS, let say service is paused (ie task count=0) then we get a message "503 services temporarily unavailable" on the web.请求通过 AWS Elastic Loadbalancer 在 ECS 中提供服务,假设服务已暂停(即任务计数 = 0),然后我们在 web 上收到消息“503 服务暂时不可用”。 now when this happens I would like to redirect this request to a lambda or anywhere else.现在发生这种情况时,我想将此请求重定向到 lambda 或其他任何地方。

addition1: I am using route53 as my DNS.另外1:我使用route53作为我的DNS。 I am doing filtering of requests on ALB level, In route53 I have a record *.example.com send to my ALB then on ALB I'm filtering using host-header and send to target group containing the target.我正在对 ALB 级别的请求进行过滤,在 route53 中,我有一条记录 *.example.com 发送到我的 ALB,然后在 ALB 上我使用主机头过滤并发送到包含目标的目标组。

addition2: (based on my research so far) I have two approaches in my mind, it would be helpful if received some comments另外2:(根据我目前的研究)我有两种方法,如果收到一些评论会有所帮助

Approach1: Two target groups 1 directs to target other directs to lambda function then based on trigger events I can change weights for these target groups方法 1:两个目标组 1 指向其他指向 lambda function 然后根据触发事件我可以更改这些目标组的权重

Approach2: Two targets in single target group base on the event I will toggle btw targets方法 2:单个目标组中的两个目标基于事件我将切换 btw 目标

You can't do it directly on ELB alone.您不能单独在 ELB 上直接执行此操作。

However, if you can use Route53, you could configure DNS failover with failover records .但是,如果您可以使用 Route53,则可以使用故障转移记录配置DNS 故障转移。

Using the failover records you would define primary and secondary records .使用故障转移记录,您可以定义主要记录和次要记录 The primary would point to the ELB, while secondary, for example, to a S3 static website.主要将指向 ELB,而次要则指向 S3 static 网站。 The health checks on the primary record would automatically failover your users to the secondary record if ELB (ie your ecs tasks go to zero).如果 ELB(即您的 ecs 任务 go 为零),主记录上的运行状况检查将自动将您的用户故障转移到辅助记录。

When ELB becomes health again, failover will start routing traffic from it again automatically .当 ELB 再次恢复健康时,故障转移将再次自动开始路由流量。

You have 2 choices for how you can handle this type of issue.对于如何处理此类问题,您有 2 种选择。

The first way to do this is through using Route 53, assuming you're not using Route 53 as your DNS solution you would need to first migrate to using Route 53 as your DNS provider.第一种方法是使用 Route 53,假设您没有使用 Route 53 作为 DNS 解决方案,您需要首先迁移到使用 Route 53 作为 DNS 提供程序。

Once you have migrated you can then update the record for the host to become a failover record which would automate the failing over to the secondary value in the event of an issue.迁移后,您可以将主机的记录更新为故障转移记录,这将在出现问题时自动故障转移到辅助值。

However, no everyone wants to migrate to Route 53 or possibly cannot migrate.但是,不是每个人都想迁移到 Route 53,或者可能无法迁移。 For this there is another solution, which is to use a CloudFront distribution in front of your endpoint.为此,还有另一种解决方案,即在您的终端节点前使用 CloudFront 分配。

By doing this you are presented with a couple of solutions:通过这样做,您会看到几个解决方案:

  • CloudFront Custom Error pages for displaying a nice friendly error if something occurs (this is what Amazon used to display there error pages during Prime Day of Dogs of Amazon ). CloudFront 自定义错误页面,用于在发生某些事情时显示友好的错误(这是亚马逊在亚马逊狗的黄金日期间用来显示错误页面的内容)。
  • Use a Lambda@Edge to modify the behaviour if an error code is detected.如果检测到错误代码,请使用Lambda@Edge修改行为。

The advantage to the CloudFront solutions is that both actually apply instantly when an error occurs, whereas Route 53 takes potentially a few health checks, looking at 30-60 seconds before failover. CloudFront 解决方案的优势在于,两者实际上都会在发生错误时立即应用,而 Route 53 可能会进行一些健康检查,在故障转移前 30-60 秒进行检查。

This means whenever your service becomes available again you will have instant service return back with CloudFront.这意味着只要您的服务再次可用,您将通过 CloudFront 获得即时服务返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM