Part 1 - Create an ASP.NET Web API Project

Follow the steps below to create and customize an ASP.NET Core Web API Project in Visual Studio. AWS fully supports deploying .NET Framework applications on Microsoft Windows and additionally .NET Core on Lambda or Microsoft Windows. You’ll also set up AWS CloudWatch Logs as the default logging target for our application, so we can view the logs in case we need to troubleshoot.

1. In Visual Studio, use File -> New -> Project to open the New Project dialog.

(For Visual Studio 2019, click the Create a new project button)

2. Under the Web project node, select the AWS Serverless Application (.NET Core - C#) template, choose a name for your project like WebAPIChallenge, then click the OK or Create button.

3. In the next dialog, select the ASP.NET Core Web API blueprint, and click the Finish button to generate the project.

4. After the project has been generated, install the Nuget package, AWS.Logger.AspNetCore for the project. Note that the version will have changed from the following figure:

5. Add the Microsoft.OpenAPI NuGet package.

6. Additionally add the Swashbuckle.AspNetCore package, check the Include prerelease checkbox so that you can install version v5.0.0-rc4.

7. Add the following AWS.Logging configuration node to the appsettings.json file to configure logging to CloudWatch Logs. If you are using something other than the default profile, change the Profile name in the settings to match your AWS Explorer profile name. Also, make sure your Region matches the region you will be deploying in. Adding them immediately before ‘Logging’ section is fine:

   "AWS.Logging": {
   "Profile": "default",
   "LogGroup": "DotNetWebApiLab",
   "Region": "us-east-1",
   "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"

8. Add the following using statement to the top of the file:

using Microsoft.OpenApi.Models;

9. Next, configure the Startup class (in Startup.cs) to use the AWS options in the appsettings.json by adding the following code to the ConfigureServices method. Typically, this is added after the boilerplate code:

    // Register the Swagger generator, defining 1 or more Swagger documents
    services.AddSwaggerGen(c =>
        c.SwaggerDoc("v1", new OpenApiInfo { Title = "Web API Challenge", Version = "v1" });

10. Inject an instance of ILoggerFactory into the Configure method by adding a new argument (after IHostingEnvironment env), ILoggerFactory loggerFactory.

   public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)

11. Set the AWS logging provider for CloudWatch Logs as the default logger by adding the following code to the end of the Configure method:

	// Enable middleware to serve generated Swagger as a JSON endpoint.

    // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
    // specifying the Swagger JSON endpoint.
    app.UseSwaggerUI(c =>
        c.SwaggerEndpoint("/Prod/swagger/v1/swagger.json", "WebAPIChallenge v1");

12. Finally, set up the ValuesController to log requests to the API methods by adding/updating the following code at the begining of the Get() method:

   private ILogger<ValuesController> _logger;
   public ValuesController(ILogger<ValuesController> logger)
      _logger = logger; //inject logger
   // GET api/values
   public IEnumerable<string> Get()
      string.Format("ValuesController Get() Request from {0}",Request.HttpContext.Connection.RemoteIpAddress));
      return new string[] { "value1", "value2" };

13. Update the method Post([FromBody]string value) to log a message to CloudWatch also, using the same pattern as you did for the Get() method above. Change the message to make it clear in the logs that the Post() method was called.

   // POST api/values
   public void Post([FromBody] string value)
      string.Format("ValuesController Post() Request from {0}",Request.HttpContext.Connection.RemoteIpAddress));

14. You will need to add the following using statement that is required for the above code:

   using Microsoft.Extensions.Logging;

Ensure your application builds. If you have your AWS credentials configured in the AWS Toolkit for Visual Studio, or else in a local credentials file, you can run the application locally and it will log to CloudWatch Logs. You can run the code locally, you may receive some SSL dialog boxes, just click Yes to them to continue on. Next, you’ll deploy your Web API application to a virtual machine (EC2 instance) running in the AWS cloud.

Note: if you get an “ exception, you will need to add a default region to your credentials file. The credentials file is located at %userprofile%/.aws/credentials