[英]aws lambda function using serverless template of asp.net core
I don't have enough knowledge of aws but my company asked me to do a job which I guess is what AWS Lambda does perfectly.我对 aws 没有足够的了解,但我的公司要求我做一份工作,我猜这就是 AWS Lambda 完美的工作。 The requirement is I have to create a service that has an endpoint that needs to be called twice a day.
要求是我必须创建一个具有需要每天调用两次的端点的服务。 The approach I followed is I created a serverless web API through visual studio and created API gateway endpoint for each endpoint.
我遵循的方法是通过 Visual Studio 创建了一个无服务器 web API 并为每个端点创建了 API 网关端点。 Then added a trigger through cloud watch events to run it twice a day but whenever the function is triggered I get this error.
然后通过云监视事件添加一个触发器以每天运行两次,但是每当触发 function 时,我都会收到此错误。
Object reference not set to an instance of an object.: NullReferenceException
at Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction.MarshallRequest(InvokeFeatures features, APIGatewayProxyRequest apiGatewayRequest, ILambdaContext lambdaContext)
at Amazon.Lambda.AspNetCoreServer.AbstractAspNetCoreFunction`2.FunctionHandlerAsync(TREQUEST request, ILambdaContext lambdaContext)
at lambda_method(Closure , Stream , Stream , LambdaContextInternal )
I have the same issue and could fix it recently.我有同样的问题,最近可以解决。
If you use Lambda with ASP.NET Core, you should have LambdaEntryPoint
class to handle all the requests.如果您将 Lambda 与 ASP.NET 内核一起使用,您应该有
LambdaEntryPoint
class 来处理所有请求。 Try to override MarshallRequest
method in this class, add logging and see what you have in apiGatewayRequest
parameter.尝试在此 class 中覆盖
MarshallRequest
方法,添加日志记录并查看apiGatewayRequest
参数中的内容。 The code can look something like this:代码看起来像这样:
protected override void MarshallRequest(InvokeFeatures features, APIGatewayProxyRequest apiGatewayRequest, ILambdaContext lambdaContext)
{
LambdaLogger.Log($"Request path: {apiGatewayRequest.Path}");
LambdaLogger.Log($"Request path parameters: {apiGatewayRequest.PathParameters}");
LambdaLogger.Log($"Request body: {apiGatewayRequest.Body}");
LambdaLogger.Log($"Request request context: {apiGatewayRequest.RequestContext}");
base.MarshallRequest(features, apiGatewayRequest, lambdaContext);
}
In my case, all these values were nulls.就我而言,所有这些值都是空值。 The reason for it was in using Amazon EventBridge for keeping Lambda online to avoid a cold start.
其原因是使用 Amazon EventBridge 保持 Lambda 在线以避免冷启动。 If you also use EventBridge, try to configure the request there properly.
如果您还使用 EventBridge,请尝试在那里正确配置请求。 If not, you can try to update
MarshalRequest
the following way:如果没有,您可以尝试通过以下方式更新
MarshalRequest
:
protected override void MarshallRequest(InvokeFeatures features, APIGatewayProxyRequest apiGatewayRequest, ILambdaContext lambdaContext)
{
if(apiGatewayRequest.RequestContext == null) //Or other property
{
return;
}
base.MarshallRequest(features, apiGatewayRequest, lambdaContext);
}
A few days ago I had the same problem, Grigory Zhadko helped me a lot by knowing which method I should overwrite, LambdaEntryPoint requires for any other process to instantiate an ApiGatewayProxiRequest object manually (for example, eventBridge), the configuration I implemented to fix the problem is as follows.几天前我遇到了同样的问题,Grigory Zhadko 通过知道我应该覆盖哪个方法帮助了我很多,LambdaEntryPoint 需要任何其他进程手动实例化 ApiGatewayProxiRequest object(例如,eventBridge),我实施的配置来修复问题如下。
protected override void MarshallRequest(InvokeFeatures features, APIGatewayProxyRequest apiGatewayRequest, ILambdaContext lambdaContext)
{
var endpoint = "my/endpoint";
if (apiGatewayRequest != null && apiGatewayRequest?.RequestContext == null)
{
apiGatewayRequest.Path = $"/{endpoint}";
apiGatewayRequest.Resource = $"/{endpoint}";
apiGatewayRequest.HttpMethod = "ANY METHOD";
apiGatewayRequest.RequestContext = new APIGatewayProxyRequest.ProxyRequestContext
{
Path = $"/path/{endpoint}", // your path request
Identity = new APIGatewayProxyRequest.RequestIdentity
{
ClientCert = new APIGatewayProxyRequest.ProxyRequestClientCert
{
Validity = new APIGatewayProxyRequest.ClientCertValidity()
}
},
ResourcePath = $"/{basePath}{eventEntpoint}",
HttpMethod = "ANY METHOD",
Authorizer = new APIGatewayCustomAuthorizerContext()
};
}
base.MarshallRequest(features, apiGatewayRequest, lambdaContext);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.