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 with Elastic Beanstalk, but for this lab, you’ll be using .NET Core. 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. Avoid using spaces in your project name as this causes issues with Elastic Beanstalk.

2. Under the Web project node, select the “ASP.NET Core Web Application” template, choose a name for your project, then click the OK button.

3. In the next dialog, select the “API” blueprint, leave “Enable Docker Support” unchecked, and click the OK 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:

   NugetPackage

5. Install the Nuget package, “AWSSDK.Extensions.NETCore.Setup” for the project.

6. 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’ is fine:

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

7. 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:

   services.AddDefaultAWSOptions(Configuration.GetAWSOptions());

8. 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)

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

    loggerFactory.AddAWSProvider(Configuration.GetAWSLoggingConfigSection());

10. Finally, set up the ValuesController to log requests to the API methods by adding/updating the following code:

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

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

   using Microsoft.Extensions.Logging;

12. 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
   [HttpPost]
   public void Post([FromBody] string value)
   {
      _logger.LogInformation(
      string.Format("ValuesController Post() Request from {0}",Request.HttpContext.Connection.RemoteIpAddress));
   }

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. 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

   [default]
   aws_access_key_id=<access_key_id>
   aws_secret_access_key=<secret_access_key>
   region=<region>