从AWS Lambda

时间:2017-11-12 11:49:30

标签: c# visual-studio amazon-web-services lambda

我在aws中尝试无服务器计算。我创建并部署了一个简单的应用程序,它返回一个字符串,如下所示

public string FunctionHandler()
{
   return "input will be in UPPER case :-> ";
}

这是从visual studio-aws插件部署的,我可以从visual studio测试屏幕正确调用它。此外,我配置了一个“开放”端点的API网关。当我从浏览器调用该URL时,它会给出内部服务器错误和状态502.详细信息从Aws测试窗口得到响应

Execution log for request test-request
Sun Nov 12 11:44:00 UTC 2017 : Starting execution for request: test-invoke-request
Sun Nov 12 11:44:00 UTC 2017 : HTTP Method: GET, Resource Path: /testAnasfn
Sun Nov 12 11:44:00 UTC 2017 : Method request path: {}
Sun Nov 12 11:44:00 UTC 2017 : Method request query string: {}
Sun Nov 12 11:44:00 UTC 2017 : Method request headers: {}
Sun Nov 12 11:44:00 UTC 2017 : Method request body before transformations: 
Sun Nov 12 11:44:00 UTC 2017 : Endpoint request URI: https://lambda.ap-south-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:ap-south-1:126893027727:function:testAnasfn/invocations
Sun Nov 12 11:44:00 UTC 2017 : Endpoint request headers: {x-amzn-lambda-integration-tag=test-request, Authorization=*************************************************************************************************************************************************************************************************************************************************************************************************************************7d591a, X-Amz-Date=20171112T114400Z, x-amzn-apigateway-api-id=vjvpcua830, X-Amz-Source-Arn=arn:aws:execute-api:ap-south-1:126893027727:vjvpcua830/null/GET/testAnasfn, Accept=application/json, User-Agent=AmazonAPIGateway_vjvpcua830, X-Amz-Security-Token=FQoDYXdzEBQaDMyv494SIfdIl+qxLSK5AygkNULmfxDTCe8srTRObtfH8Gycwy22pkJzTD/fp+peUByQbXRD1MTeFFQHn6t+hulEBbAdESX0goX9/fytIFksJnMlT+/E+OlfIx17YwqqRIw+0dieUgDCl80J12UYR2CNPQFdc3yU5F60QXf4EjpgXOVTClSSSbqeXKZQ8M9BgzD3YsRbXnpVsRlpELJaV7i9R1rSeuI6ix66NPgpd+cp4578Fhpuol3xlueqk0JnlJZ9NtwH0N8vLbH3R2ZP0yPY8a9Pp19qHkVz9C2DzNXenChwxEb9seVZR7D92FIg8rBcQQkzDlbDa2D2lVMeSImhdlsvF/ttlpgWnpfeB [TRUNCATED]
Sun Nov 12 11:44:00 UTC 2017 : Endpoint request body after transformations: {"resource":"/testAnasfn","path":"/testAnasfn","httpMethod":"GET","headers":null,"queryStringParameters":null,"pathParameters":null,"stageVariables":null,"requestContext":{"path":"/testAnasfn","accountId":"126893027727","resourceId":"v0n0zx","stage":"test-invoke-stage","requestId":"test-invoke-request","identity":{"cognitoIdentityPoolId":null,"accountId":"126893027727","cognitoIdentityId":null,"caller":"126893027727","apiKey":"test-invoke-api-key","sourceIp":"test-invoke-source-ip","accessKey":"ASIAJNBVXURNV3ICQ2YA","cognitoAuthenticationType":null,"cognitoAuthenticationProvider":null,"userArn":"arn:aws:iam::126893027727:root","userAgent":"Apache-HttpClient/4.5.x (Java/1.8.0_144)","user":"126893027727"},"resourcePath":"/testAnasfn","httpMethod":"GET","apiId":"vjvpcua830"},"body":null,"isBase64Encoded":false}
Sun Nov 12 11:44:00 UTC 2017 : Sending request to https://lambda.ap-south-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:ap-south-1:126893027727:function:testAnasfn/invocations
Sun Nov 12 11:44:00 UTC 2017 : Received response. Integration latency: 19 ms
Sun Nov 12 11:44:00 UTC 2017 : Endpoint response body before transformations: "input will be in UPPER case :-> "
Sun Nov 12 11:44:00 UTC 2017 : Endpoint response headers: {x-amzn-Remapped-Content-Length=0, Connection=keep-alive, x-amzn-RequestId=c65a341b-c79e-11e7-8ef4-17eaf422a581, Content-Length=34, Date=Sun, 12 Nov 2017 11:44:00 GMT, X-Amzn-Trace-Id=root=1-5a083400-b65593a405f54027ad55da5c;sampled=0, Content-Type=application/json}
Sun Nov 12 11:44:00 UTC 2017 : Execution failed due to configuration error: Malformed Lambda proxy response
Sun Nov 12 11:44:00 UTC 2017 : Method completed with status: 502 

我试图从jQuery Ajax调用它但收到状态失败。

我检查了使用Lambda Proxy集成,以便从Proxy使用它!

更新1

当我在API网关和测试应用程序中取消选中使用Lambda代理集成集成请求时,我会得到更正状态为200的响应。

Execution log for request test-request
Sun Nov 12 19:44:19 UTC 2017 : Starting execution for request: test-invoke-request
Sun Nov 12 19:44:19 UTC 2017 : HTTP Method: GET, Resource Path: /testAnas1
Sun Nov 12 19:44:19 UTC 2017 : Method request path: {}
Sun Nov 12 19:44:19 UTC 2017 : Method request query string: {}
Sun Nov 12 19:44:19 UTC 2017 : Method request headers: {}
Sun Nov 12 19:44:19 UTC 2017 : Method request body before transformations: 
Sun Nov 12 19:44:19 UTC 2017 : Endpoint request URI: https://lambda.ap-south-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:ap-south-1:126893027727:function:testAnas1/invocations
Sun Nov 12 19:44:19 UTC 2017 : Endpoint request headers: {x-amzn-lambda-integration-tag=test-request, Authorization=***************************************************************************************************************************************************************************************************************************************************************************************bd4940, X-Amz-Date=20171112T194419Z, x-amzn-apigateway-api-id=ntn0u9y8gk, X-Amz-Source-Arn=arn:aws:execute-api:ap-south-1:126893027727:ntn0u9y8gk/null/GET/testAnas1, Accept=application/json, User-Agent=AmazonAPIGateway_ntn0u9y8gk, X-Amz-Security-Token=FQoDYXdzEBwaDKFa4kx/RGE/fe8dqyK5A7wMpA/qRGHmUARstMAvEJvFe3rvi32sR0faLB+IUTb8wOMeehIgNJ4O/QD+5CuM26+q6xnCN5Ov5qJvENp63KiqmNuhpub5hoDutE1zAahQsLoKzx5Muqy+/tzJ9yQCXaA1eHklH0XNNG9BZjgz1JANoPNfMGSvY8KBEqARH6fCEdOuSz0/KJeALd9xYbHATO0ml5ucXJLqVEZFPuwbwrfbFHINGXkjgpJCLwc5x4YoY+9bWzkuWc+akkMIeyRDmKvii88Qa3MCnbJz9wUqzG/bjh8QpVdg4M/dFhG3lor0s1tdT6r4w47MeUaJgqxAQXO+7eGTA2PfPWdsoB1JGl2qriDXwamgSQJEkpHCgGaZzk88lyEgabUI [TRUNCATED]
Sun Nov 12 19:44:19 UTC 2017 : Endpoint request body after transformations: 
Sun Nov 12 19:44:19 UTC 2017 : Sending request to https://lambda.ap-south-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:ap-south-1:126893027727:function:testAnas1/invocations
Sun Nov 12 19:44:20 UTC 2017 : Received response. Integration latency: 80 ms
Sun Nov 12 19:44:20 UTC 2017 : Endpoint response body before transformations: "I am Ahmad Anas"
Sun Nov 12 19:44:20 UTC 2017 : Endpoint response headers: {x-amzn-Remapped-Content-Length=0, Connection=keep-alive, x-amzn-RequestId=e0201f5e-c7e1-11e7-8754-61ed0803d92c, Content-Length=17, Date=Sun, 12 Nov 2017 19:44:20 GMT, X-Amzn-Trace-Id=root=1-5a08a493-f27ad9f6b7e304c7ef148949;sampled=0, Content-Type=application/json}
Sun Nov 12 19:44:20 UTC 2017 : Method response body after transformations: "I am Ahmad Anas"
Sun Nov 12 19:44:20 UTC 2017 : Method response headers: {X-Amzn-Trace-Id=sampled=0;root=1-5a08a493-f27ad9f6b7e304c7ef148949, Content-Type=application/json}
Sun Nov 12 19:44:20 UTC 2017 : Successfully completed execution
Sun Nov 12 19:44:20 UTC 2017 : Method completed with status: 200

但我的问题仍然存在,当我调用调用URL 时,会返回状态代码502。请注意,我已经创建了ProductionTest1阶段。

答案

我现在肯定不允许回答我自己的问题,但我认为回答[我自己]作为更新是可以的。 简短回答:在C#中构建自己的响应,然后在代码中部署lambda,如下所示。

public APIGatewayProxyResponse FunctionHandler(APIGatewayProxyRequest request, ILambdaContext context)
{
        //context.Logger.LogLine("This is useful for debugging!"); 
        return new APIGatewayProxyResponse()
        {
            StatusCode = (int)HttpStatusCode.OK, Body = "Go Serverless v1.0! Your function executed successfully!",
        };
}

现在来看一点细节。要使用AWS Lambda,您可以使用HTTP代理集成与代理资源(以防万一)

由HTTP网关REST API中的HTTP_PROXY指定的HTTP代理集成用于将方法请求与后端HTTP端点集成。 使用此集成类型,API Gateway只会传递前端和后端之间的整个请求和响应,受某些限制和限制。 here

因此,作为一种可能的解决方案,您可以创建自己的简单对象或使用AWS提供的对象 - APIGatewayProxyResponse。 More将NuGet用于此Install-Package Amazon.Lambda.APIGatewayEvents -Version 1.1.0

我希望这会解决问题并简要介绍响应对象技巧!

0 个答案:

没有答案