Game Tech Tutorial
Published in

Game Tech Tutorial

Deploying ASP.NET Core to AWS Elastic Beanstalk with CloudWatch Log

AWS Elastic Beanstalk is a very easy-to-use service that allows you to deploy a web application without worrying about availability, scalability, and administration of servers.

In this article, we will give you a step-by-step guideline and a practical API Server template in ASP.NET Core to deploy a web application on AWS Beanstalk with CloudWatch Log.

Table of Contents

  • Prerequisite
  • Setup environment
  • Setup CloudWatch Logs
  • Deploying API Server to AWS Beanstalk

Getting Started

Prerequisite:

  • AWS.Logger.AspNetCore v3.2.0
  • Visual Studio Community v2019
  • ASP.NET Core 3.1

Setup environment

  • Setup ASP.NET Core
  1. Download API Server Template
git clone git@github.com:gcoolmaneric/aws-eb-api-server.git
cd aws-eb-api-server

2. Download and Install [.Net Core 3.0 SDK]

3. Install ASP.NET Core and AWS SDK

/aws-eb-api-server/dotnet add package AWS.Logger.AspNetCore --Version 3.2.0

4. Run the application locally

/aws-eb-api-server/dotnet fake build -t run

5. Test and Open URL at http://localhost:8000

If you can see a page on local, then let’s start to set up the logs.

Setup CloudWatch Log

  • Create a log group in CloudWatch.
  • Activate Log Stream by Beanstalk Console UI on AWS. (After you create the environment, you can configure it to enable Log Stream for CloudWatch)
  • Create a policy below and attach to arn:aws:Iam::[your AWS ID]:role/aws-elasticbeanstalk-ec2-role
{
“Version”: “2012–10–17”,
“Statement”:
{
“Sid”: “CloudWatchLogsAccess”,
“Effect”: “Allow”,
“Action”: “logs:*”,
“Resource”: “*”
}
]
}
  • Modify Region and LogGroup Name
/aws-eb-api-server/vim appsettings.json

Region: <Your Region>

LogGroup: <Specify your Log Group Name in CloudWatch>

Note:

  • Given that ASP.NET Core Logs, by the default settings, does not support to output logs on AWS Beanstalk, you need to set up Startup.cs and program.cs properly to make it function well.
  • This API Template has already been set it up for you, you can refer to the following for details.

Next, let’s add some logs. You can output log info or error logs as below.

/aws-eb-api-server/vim Startup.cs// Add the following 
// Log information
_logger.LogInformation("Hello World");
// Log Error
_logger.LogError("exception errorMsg:" + exception.Message);

Important Note:

  • By default setting, Beanstalk will send a health check to validate your server at endpoint http://<your domain>/
  • Line 53: Your server has to reply HTTP 200 OK to Beanstalk for passing the healthCheck. Otherwise, your server cannot be started properly.

Lastly, let’s build the web application.

  • Build to Create a new bundle
/aws-eb-api-server/dotnet fake build -t bundle

If successful, you will find dotnet-core-eb-api-server.zip in

/aws-eb-api-server/bundle/dotnet-core-eb-api-server.zip

Deploying API Server to AWS Beanstalk

1. Launch an AWS Elastic Beanstalk environment

  • Log in to AWS Console
  • Goto Elastic Beanstalk → Click create a new environment

Select Web server environment

Application name: <fill in your name>

Fill in Environment name and Domain

Platform: Select Managed platform

Platform: Select .NET Windows Server

Platform branch: IIS 10.0 running on 64bit Windows Server 2019

Platform version: 2.7.0

Finally, you can upload dotnet-core-eb-api-server.zip to AWS Beanstalk to test.

Summary

Congratulations! You have successfully built an ASP.NET Core application and deployed it to AWS Beanstalk with CloudWatch Logs.

You can reuse the API Server Template as a starting point.

Hope this tutorial will be helpful to you. Don’t forget to give us a clap to support us in writing more useful articles in the future.

Thanks for reading.

You might be interested in

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Eric Wei

Eric Wei

Senior Full Stack Engineer & Solution architecture | AWS, GCP, Azure | Cloud, Unity Game Development, SDK, DevOps, and more.