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
- Setup environment
- Setup CloudWatch Logs
- Deploying API Server to AWS Beanstalk
- AWS.Logger.AspNetCore v3.2.0
- Visual Studio Community v2019
- ASP.NET Core 3.1
- Setup ASP.NET Core
- Download API Server Template
git clone firstname.lastname@example.org:gcoolmaneric/aws-eb-api-server.git
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
- Modify Region and LogGroup Name
Region: <Your Region>
LogGroup: <Specify your Log Group Name in CloudWatch>
- 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.
How to integrate ASP.NET Core Web to CloudWatch on AWS Beanstalk?
AWS Elastic Beanstalk is a managed service that allows you can ensure high availability, scalability, and less…
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);
- 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
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.
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
How to create a Serverless Cron Job in AWS Batch?
AWS Batch is a managed service to allow you to run batch jobs on AWS like big data analyzing or updating data in MYSQL.
How to reverse engineer Unity3D Games?
Before writing this article, I thought Unity’s games were much safer than some browser games built by Java Script…