[英]How to invoke Lambda asynchronously via Application Load Balancer? (AWS)
Using AWS, wondering if anyone has managed to invoke a lambda asynchronously via an ALB before?使用 AWS,想知道以前是否有人设法通过 ALB 异步调用 lambda?
I've seen API Gateway examples and CLI examples and thought it should be a similar process however nothing has worked so far.我看过 API 网关示例和 CLI 示例,并认为这应该是一个类似的过程,但到目前为止还没有任何效果。 The examples mention passing the invocation type to the header which I have tried ("X-Amzn-Invocation-Type" and "Invocation-Type" with "Event").这些示例提到将调用类型传递给我尝试过的 header(“X-Amzn-Invocation-Type”和“Invocation-Type”与“Event”)。
From Cloudwatch logs I can see the header being passed but it is not the expected behaviour.从 Cloudwatch 日志中,我可以看到 header 正在通过,但这不是预期的行为。
An example, Lambda returns some html with 'Hello World'.例如,Lambda 返回一些带有“Hello World”的 html。
curl -i -X POST -H "X-Amz-Invocation-Type:Event" http://{alb.name}/{path}
Expected response is either 200 or 202预期响应为 200 或 202
Actual response is实际反应是
Server: awselb/2.0
Date: Tue, 25 May 2021 13:06:32 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 246
Connection: keep-alive
<html>
<head>
<title>Hello World!</title>
<style>
html, body {
margin: 0; padding: 0;
font-family: arial; font-weight: 700; font-size: 3em;
text-align: center;
}
</style>
</head>
<body>
<p>Hello World from Lambda</p>
</body>
</html>
Cloudwatch logs headers are as follows Cloudwatch 日志头如下
{
"body": "",
"requestContext": {
"elb": {
"targetGroupArn": "{arn}"
}
},
"queryStringParameters": {},
"httpMethod": "POST",
"headers": {
"x-amz-invocation-type": "Event",
"x-forwarded-port": "80",
"x-forwarded-for": "10.0.233.231",
"accept": "*/*",
"user-agent": "curl/7.61.1",
"x-amzn-trace-id": "Root=1-60acf23c-6f1772565f7fbf070f0dd1a9",
"host": "{alb}",
"x-forwarded-proto": "http"
},
"path": "{path}",
"isBase64Encoded": "False"
}
Just wondering if anyone has managed to successfully call a lambda asynchronously via an ALB or is that not possible?只是想知道是否有人设法通过 ALB 异步成功地调用 lambda 或者这是不可能的?
Different services invoke AWS Lambda differently:不同的服务调用 AWS Lambda 的方式不同:
https://aws.amazon.com/blogs/architecture/understanding-the-different-ways-to-invoke-lambda-functions/ https://aws.amazon.com/blogs/architecture/understanding-the-different-ways-to-invoke-lambda-functions/
ELB invokes it synchronously. ELB 同步调用它。 That said, the documentation is rather inaccurate.也就是说,文档相当不准确。 API Gateway by default does a synchronous invocation, but with a custom integration and appropriate headers, API Gateway can invoke lambda asynchronously. API 网关默认执行同步调用,但通过自定义集成和适当的标头,API 网关可以异步调用 lambda。 The ability to invoke asynchronously is built into API Gateway. API 网关内置了异步调用的能力。 From the looks of it, this is not present in ELBs like ALB, atleast not in a documented fashion.从外观上看,这在像 ALB 这样的 ELB 中是不存在的,至少没有以记录的方式存在。
There are round about ways but if you are considering those, I guess an API Gateway based solution might make more sense.有很多方法,但如果您正在考虑这些方法,我想基于 API 网关的解决方案可能更有意义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.