【问题标题】:Amazon AWS C# Lambda Code Pipeline亚马逊 AWS C# Lambda 代码管道
【发布时间】:2018-11-13 21:29:16
【问题描述】:

使用 Amazon Lambda 函数模板编写了一个 .NET core 2 Web api 应用程序。使用邮递员编写了一些测试电话。 .NET 核心代码在本地运行时一切正常。将 .NET 核心代码上传到 Lambda 函数,修改邮递员以调用该 URL。在 Lambda 函数中运行的 .NET 代码一切正常 - 使用 Postman 在本地调用。

现在的要求是从 aws codepipline 调用 Lambda 函数内的 .NET 核心代码。无法让它工作

当 Postman 调用 lambda 函数时 - 我在 cloudwatch 日志中看到了这一点:

Lambda Deserialize Amazon.Lambda.APIGatewayEvents.APIGatewayProxyRequest:
{
    "resource": "/{proxy+}",
    "path": "/api/Rds/InstanceStatus",
    "httpMethod": "GET",

这个电话有效! 我的 .NET 核心代码记录以下内容

LOG_LEVEL_TRACE,Nexus.Rds.Restore.dll,1.0.0.0,Nexus.Rds.Restore.LambdaEntryPoint,FunctionHandlerAsync,APIGatewayProxyRequest.Body = null

LOG_LEVEL_TRACE,Nexus.Rds.Restore.dll,1.0.0.0,Nexus.Rds.Restore.LambdaEntryPoint,FunctionHandlerAsync,APIGatewayProxyRequest.HttpMethod = GET

LOG_LEVEL_TRACE,Nexus.Rds.Restore.dll,1.0.0.0,Nexus.Rds.Restore.LambdaEntryPoint,FunctionHandlerAsync,APIGatewayProxyRequest.Path = /api/Rds/InstanceStatus

LOG_LEVEL_TRACE,Nexus.Rds.Restore.dll,1.0.0.0,Nexus.Rds.Restore.LambdaEntryPoint,FunctionHandlerAsync,APIGatewayProxyRequest.Resource = /{proxy+}

注意 - 您可以看到在调用我的 .NET 核心代码之前填充了 LambdaEntryPoint 对象

当我尝试从 codepipeline 中复制相同的调用时 - 我在 cloudwatch 日志中看到了这一点:

Lambda Deserialize Amazon.Lambda.APIGatewayEvents.APIGatewayProxyRequest:
{
    "CodePipeline.job": {
        "id": "379d05fe-ec03-4ec9-8bce-59f31901aeb8",
        "accountId": "109319094079",
        "data": {
            "actionConfiguration": {
                "configuration": {
                    "FunctionName": "NexuIntRestServerless2-AspNetCoreFunction-T8Q5XCCN0FIC",
                    "UserParameters": "{\"resource\":\"/{proxy+}\",\"path\":\"/api/Rds/InstanceStatus\",\"httpMethod\":\"GET\"}"
                }
            },

我试图重现代码管道用户参数中的参数。但是,我的 .NET 核心代码日志如下所示:

LOG_LEVEL_TRACE,Nexus.Rds.Restore.dll,1.0.0.0,Nexus.Rds.Restore.LambdaEntryPoint,FunctionHandlerAsync,APIGatewayProxyRequest.Body = null

LOG_LEVEL_TRACE,Nexus.Rds.Restore.dll,1.0.0.0,Nexus.Rds.Restore.LambdaEntryPoint,FunctionHandlerAsync,APIGatewayProxyRequest.HttpMethod = null

LOG_LEVEL_TRACE,Nexus.Rds.Restore.dll,1.0.0.0,Nexus.Rds.Restore.LambdaEntryPoint,FunctionHandlerAsync,APIGatewayProxyRequest.Path = null

LOG_LEVEL_TRACE,Nexus.Rds.Restore.dll,1.0.0.0,Nexus.Rds.Restore.LambdaEntryPoint,FunctionHandlerAsync,APIGatewayProxyRequest.Resource = null

LOG_LEVEL_TRACE,Nexus.Rds.Restore.dll,1.0.0.0,Nexus.Rds.Restore.LambdaEntryPoint,FunctionHandlerAsync,APIGatewayProxyRequest.RequestContext = null

注意:LambdaEntryPoint 现在为 null,并引发异常 - 我假设是因为 .NET 核心不知道如何路由调用:

One or more errors occurred. (Object reference not set to an instance of an object.): AggregateException
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
at lambda_method(Closure , Stream , Stream , LambdaContextInternal )

at Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction.MarshallRequest(InvokeFeatures features, APIGatewayProxyRequest apiGatewayRequest, ILambdaContext lambdaContext)
at Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction.<FunctionHandlerAsync>d__13.MoveNext()

关于如何从 Codepipeline 中调用 Lambda 函数有什么想法吗?

谢谢

【问题讨论】:

    标签: c# .net-core aws-lambda aws-codepipeline


    【解决方案1】:

    当您通过邮递员调用该函数时,您似乎正在使用 API 网关。

    从 API 网关调用将具有与从 CodePipeline 调用时不同的函数输入,因为来自 API 网关的输入表示 HTTP 请求与 CodePipeline 作业。

    您的 Lambda 函数需要设计为处理 CodePipeline 作业的输入。

    有关于此作业的格式以及如何使用它的文档,然后在此处将结果报告回 CodePipeline:https://docs.aws.amazon.com/codepipeline/latest/userguide/actions-invoke-lambda-function.html

    【讨论】:

    • Isn't the entry point into the .net core application ---> public override Task FunctionHandlerAsync(APIGatewayProxyRequest request, ILambdaContext lambdaContext) 似乎代码管道将不得不填充 APIGatewayProxyRequest 对象就像 API 网关一样
    • 我在 .net 核心应用程序中添加了这么多日志记录的原因是 - 我找不到 codepipeline 在用户参数中传递的位置。如果我可以在 FunctionHandlerAsync() 中获取用户参数 - 我可以在我的 .net 核心应用程序中进行自己的路由
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-16
    • 1970-01-01
    • 2017-07-10
    • 1970-01-01
    • 2013-09-02
    相关资源
    最近更新 更多