简体   繁体   中英

ASP.NET Web API works locally but returning SqlException error on AWS Lambda

I am trying to host a Web API on aws lambda. The API connects to a database on AWS RDS. The database does store and serve data correctly. The problem is that when I run the API locally, it does return data from the database correctly, but after deploying it to the lambda it returns this error :

errortype: SqlException

It does not explain further on the error, I am new to AWS so I am clueless on how to solve this error, I tried running it on AWS Api Gateway and I got:

Respoonse headers:

{ "Content-Type":[""],"ErrorType":["SqlException"],"X-Amzn-Trace-Id":["Root=1-62d2052c-c1250f296445a5f4aa4daded;Sampled=0"]}

Logs:

Execution log for request 9a72a4d9-d4a0-4a54-913a-4bfc8e0e8ef6
Sat Jul 16 00:24:12 UTC 2022 : Starting execution for request: 9a72a4d9-d4a0-4a54-913a-4bfc8e0e8ef6
Sat Jul 16 00:24:12 UTC 2022 : HTTP Method: GET, Resource Path: /api/Tutors
Sat Jul 16 00:24:12 UTC 2022 : Method request path: {proxy=api/Tutors}
Sat Jul 16 00:24:12 UTC 2022 : Method request query string: {}
Sat Jul 16 00:24:12 UTC 2022 : Method request headers: {}
Sat Jul 16 00:24:12 UTC 2022 : Method request body before transformations: 
Sat Jul 16 00:24:12 UTC 2022 : Endpoint request URI: https://lambda.us-east-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:us-east-1:204758858648:function:TutorMeAPI-AspNetCoreFunction-feH0JQ7qc1x8/invocations
Sat Jul 16 00:24:12 UTC 2022 : Endpoint request headers: {X-Amz-Date=20220716T002412Z, x-amzn-apigateway-api-id=ls9fcxi4k1, Accept=application/json, User-Agent=AmazonAPIGateway_ls9fcxi4k1, Host=lambda.us-east-1.amazonaws.com, X-Amz-Content-Sha256=cc12d58899f939a039dd7463ba42b4d9f8e4646a3e0e42e4cce39c3b1649c782, X-Amzn-Trace-Id=Root=1-62d2052c-c1250f296445a5f4aa4daded, x-amzn-lambda-integration-tag=9a72a4d9-d4a0-4a54-913a-4bfc8e0e8ef6, Authorization=*********************************************************************************************************************************************************************************************************************************************************************************************************************************************9bb3fc, X-Amz-Source-Arn=arn:aws:execute-api:us-east-1:204758858648:ls9fcxi4k1/test-invoke-stage/GET/{proxy+}, X-Amz-Security-Token=IQoJb3JpZ2luX2VjEHgaCXVzLWVhc3QtMSJGMEQCICsQ6ZsgSb71mpvgkJSp932NXHuR0pli4knXT6Sbs+prAiBtOsSbcS8pwTB6eF56E/ARbK5e7Jf8qMiqKYwIdg8D5SrcBAix/ [TRUNCATED]
Sat Jul 16 00:24:12 UTC 2022 : Endpoint request body after transformations: {"resource":"/{proxy+}","path":"/api/Tutors","httpMethod":"GET","headers":null,"multiValueHeaders":null,"queryStringParameters":null,"multiValueQueryStringParameters":null,"pathParameters":{"proxy":"api/Tutors"},"stageVariables":null,"requestContext":{"resourceId":"wqoa6e","resourcePath":"/{proxy+}","httpMethod":"GET","extendedRequestId":"VVW-_GJ3IAMFqDQ=","requestTime":"16/Jul/2022:00:24:12 +0000","path":"/{proxy+}","accountId":"204758858648","protocol":"HTTP/1.1","stage":"test-invoke-stage","domainPrefix":"testPrefix","requestTimeEpoch":1657931052514,"requestId":"9a72a4d9-d4a0-4a54-913a-4bfc8e0e8ef6","identity":{"cognitoIdentityPoolId":null,"cognitoIdentityId":null,"apiKey":"test-invoke-api-key","principalOrgId":null,"cognitoAuthenticationType":null,"userArn":"arn:aws:iam::204758858648:root","apiKeyId":"test-invoke-api-key-id","userAgent":"aws-internal/3 aws-sdk-java/1.12.239 Linux/5.4.196-119.356.amzn2int.x86_64 OpenJDK_64-Bit_Server_VM/25.332-b08 java/1.8.0_332 [TRUNCATED]
Sat Jul 16 00:24:12 UTC 2022 : Sending request to https://lambda.us-east-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:us-east-1:204758858648:function:TutorMeAPI-AspNetCoreFunction-feH0JQ7qc1x8/invocations
Sat Jul 16 00:24:33 UTC 2022 : Received response. Status: 200, Integration latency: 21292 ms
Sat Jul 16 00:24:33 UTC 2022 : Endpoint response headers: {Date=Sat, 16 Jul 2022 00:24:33 GMT, Content-Type=application/json, Content-Length=138, Connection=keep-alive, x-amzn-RequestId=60dad212-1480-4555-a950-b18629fac5cc, x-amzn-Remapped-Content-Length=0, X-Amz-Executed-Version=$LATEST, X-Amzn-Trace-Id=root=1-62d2052c-c1250f296445a5f4aa4daded;sampled=0}
Sat Jul 16 00:24:33 UTC 2022 : Endpoint response body before transformations: {"statusCode":500,"headers":{},"multiValueHeaders":{"Content-Type":[null],"ErrorType":["SqlException"]},"body":"","isBase64Encoded":false}
Sat Jul 16 00:24:33 UTC 2022 : Method response body after transformations: 
Sat Jul 16 00:24:33 UTC 2022 : Method response headers: {Content-Type=null, ErrorType=SqlException, X-Amzn-Trace-Id=Root=1-62d2052c-c1250f296445a5f4aa4daded;Sampled=0}
Sat Jul 16 00:24:33 UTC 2022 : Successfully completed execution
Sat Jul 16 00:24:33 UTC 2022 : Method completed with status: 500

I also noticed it does return data for the default controller that was auto generated but for the ones I made connecting to the database they return an error

This is an example of my controller:

namespace WebAPI.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class GroupsController : ControllerBase
    {
        private readonly WebAPIContext _context;

        public GroupsController(WebAPIContext context)
        {
            _context = context;
        }

        // GET: api/Groups
        [HttpGet]
        public async Task<ActionResult<IEnumerable<Group>>> GetGroup()
        {
            if (_context.Group == null)
            {
                return NotFound();
            }

            return await _context.Group.ToListAsync();
        }

        // GET: api/Groups/5
        [HttpGet("{id}")]
        public async Task<ActionResult<Group>> GetGroup(Guid id)
        {
            if (_context.Group == null)
            {
                return NotFound();
            }

            var @group = await _context.Group.FindAsync(id);

            if (@group == null)
            {
                return NotFound();
            }

            return @group;
        }
    }
}

This works locally with no issues. What could be the issue with lambda? Thank you for your time

Lambda is a Serverless compute environment that runs methods/functions. You are looking for a hosting solution for your ASP.NET Web API. Look at deploying your ASP.NET Web API to Elastic Beanstalk. Elastic Beanstalk for .NET is available to anyone who hosts a web app that uses IIS.

Details here:

Creating and deploying .NET applications on Elastic Beanstalk

How to deploy a .NET sample application using Elastic Beanstalk

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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