title: Why does my .NET Core Web API (deployed on AWS Lambda) return a 502 Bad Gateway error?
body: <p>I am new to .NET Core, Entity Framework, AWS, and pretty much everything here (please comment and let me know if anything needs to be added to the post). </p>
  
  <p>What I am trying to do is deploy a .NET Core Web API with AWS Lambda. My API seems to be working locally, as I tested it (I have an Angular frontend app, with which I am able to successfully execute all the APIs). However, when I deploy with AWS and run a few of the APIs on Postman, I get a "502 Bad Gateway" error.</p>
  
  <p>My <code>serverless.yml</code> file looks like this:</p>
  
  <pre><code>service: angularawardwebapi
  
  provider: 
    name: aws
    runtime: dotnetcore2.1
    region: us-east-1
  
  package:
    artifact: bin/release/netcoreapp2.1/deploy-package.zip
  
  functions:
    api:
      handler: angularawardwebapi::angularawardwebapi.LambdaEntryPoint::FunctionHandlerAsync
      events: 
        - http:
            path: /{proxy+}
            method: ANY
  </code></pre>
  
  <p>My Model Class looks something like this:</p>
  
  <pre><code>using System.ComponentModel.DataAnnotations;
  using System.ComponentModel.DataAnnotations.Schema;
  
  namespace angularawardwebapi.Models {
      public class Student {
          [Key]
          public int StudentId{get;set;}
  
          [Required]
          [Column(TypeName="nvarchar(50)")]
          public string StudentFirstName{get;set;}
  
          [Required]
          [Column(TypeName="varchar(50)")]
          public string StudentLastName{get;set;}
      }
  }
  </code></pre>
  
  <p>My DbContext file looks like this: </p>
  
  <pre><code>public class DatabaseContext:DbContext {
          public DatabaseContext(DbContextOptions&lt;DatabaseContext&gt; options):base(options) {
  
          }
  
          public DbSet&lt;Student&gt; Students {get;set;} 
     }
  }
  </code></pre>
  
  <p>I have two methods in my Controller: </p>
  
  <pre><code>        [HttpGet("temp")]
          public ActionResult&lt;List&lt;String&gt;&gt; GetTemp() {
              return Ok("here");
          } 
  
  
          [HttpGet]
          public ActionResult&lt;IEnumerable&lt;Student&gt;&gt; GetStudent()
          {
              return Ok(_context.Students);
          }
  </code></pre>
  
  <p>The first of which works without a hitch (as expected). However, when I try to return a list of the Students that are in the DB, it seems that I'm encountering issues.</p>
  
  <p>I tried using the second API locally as well, and received the appropriate response: </p>
  
  <pre><code>[
      {
          "StudentId": 1,
          "StudentFirstName": "John",
          "StudentLastName": "Doe"
      }
  ]
  </code></pre>
  
  <p>I also checked the AWS CloudWatch Logs, which showed something like this:</p>
  
  <pre><code>[Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting GET https://[myurl]/dev/api/Student/ 
  [Information] Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker: Route matched with {action = "GetStudent", controller = "Student"}. Executing action WebApi.Controllers.StudentController.GetStudent ([myprojectname]) 
  [Information] Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker: Executing action method WebApi.Controllers.StudentController.GetStudent ([myprojectname]) - Validation state: Valid 
  [Information] Microsoft.EntityFrameworkCore.Infrastructure: Entity Framework Core 2.1.8-servicing-32085 initialized 'DatabaseContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None 
  END RequestId: f57417d3-4075-4680-a78d-e90e2c710760
  REPORT RequestId: f57417d3-4075-4680-a78d-e90e2c710760  Duration: 6006.80 ms    Billed Duration: 6000 ms Memory Size: 1024 MB   Max Memory Used: 141 MB 
  2019-06-06T14:19:41.116Z f57417d3-4075-4680-a78d-e90e2c710760 Task timed out after 6.01 seconds
  
  </code></pre>
  
  <p>This time out issue is not seen when I run the API locally, as I immediately get the correct response (as specified above).</p>
  
  <hr>
  
  <h2><strong>Update</strong></h2>
  
  <p>So I now have an RDS Database, which I can successfully connect to and play around with in SSMS. Running APIs locally also still work. However, after I deploy it using <code>serverless deploy -v</code>, I'm still facing the previous issue. As suggested, I tried modifying the permissions for the DB, although I'm not sure I'm doing the right thing there either. What I've done is:</p>
  
  <ul>
  <li>Went into SSMS and modified the database permissions by right-clicking the server, going to Permissions, selecting my login (that I set up while configuring the RDS), and granted the "Alter any database" option. I then went to the database itself and modified some permissions (Delete, Insert and Update).</li>
  <li>I also enabled the Windows Management Instrumentation (WMI-In) Windows Firewall rule.</li>
  </ul>
  
  <p>Neither of these seems to have solved my problem. </p>