Revamping and Containerizing a Legacy MVC .NET Application with Entity Framework to .NET Core Featuring Entity Framework Core: Part 2 | Amazon VGT2 Las Vegas

Revamping and Containerizing a Legacy MVC .NET Application with Entity Framework to .NET Core Featuring Entity Framework Core: Part 2 | Amazon VGT2 Las VegasMore Info

This is the second installment in a two-part series focusing on the migration and containerization of an enterprise application. In the first part, we guided you through a comprehensive process to re-architect a legacy ASP.NET MVC application, transitioning it to the .NET Core Framework. In this post, you’ll deploy the re-architected application to Amazon Elastic Container Service (Amazon ECS) and operate it as a task using AWS Fargate.

Solution Overview

In the initial post, we transitioned the legacy MVC ASP.NET application to ASP.NET Core. Now, we will modernize this same application into a Docker container and host it within the ECS cluster.

The architecture is illustrated in the diagram below.

First, you’ll launch a SQL Server Express RDS (1) instance and set up a Cycle Store database on that instance, which will contain tables for various categories and subcategories of bikes. The previously re-architected ASP.NET Core application will serve as the foundation for this post, utilizing AWS Secrets Manager (2) to retrieve database credentials for accessing the Amazon RDS instance. Next, you will build a Docker image of the application and push it to Amazon Elastic Container Registry (Amazon ECR) (3). Finally, create an ECS cluster (4) to run the Docker image as an AWS Fargate task.

Prerequisites

Before you start, ensure you have the following prerequisites in place:

  • An AWS account.
  • An AWS Identity and Access Management (IAM) user with AdministratorAccess. For detailed instructions, see this link to create an IAM role with Administrator access.
  • AWS Tools for Windows. Follow these steps to configure your AWS profile.
  • .NET Core 3.1 SDK installed. For instructions, refer to Download .NET Core 3.1.
  • Microsoft Visual Studio 2017 or a later version (Visual Studio Code is also a viable alternative).
  • SQL Server Management Studio to connect to the SQL Server instance.
  • Experience in ASP.NET application development.

This post demonstrates the solution in the us-east-1 region.

Source Code

Clone the source code from the GitHub repository. The source code folder includes the re-architected source code and the AWS CloudFormation template for launching the infrastructure, along with the Amazon ECS task definition.

Setting Up the Database Server

To ensure your database is ready to go, utilize a CloudFormation template to create an instance of Microsoft SQL Server Express and AWS Secrets Manager secrets to securely store database credentials, security groups, and IAM roles for accessing Amazon RDS and Secrets Manager. This stack will take approximately 15 minutes to provision, primarily due to the service setup times.

  1. Access the AWS CloudFormation console and select “Create stack.”
  2. For “Prepare template,” choose “Template is ready.”
  3. For “Template source,” select “Upload a template file.”
  4. Upload SqlServerRDSFixedUidPwd.yaml from the GitHub repository.
  5. Click “Next.”
  6. Enter “SQLRDSEXStack” as the Stack name and click “Next.”
  7. Keep the remaining options at their defaults.
  8. Acknowledge that AWS CloudFormation may create IAM resources with custom names.
  9. Select “Create stack.”
  10. Once the status reads “CREATE_COMPLETE,” navigate to the Outputs tab.
  11. Record the value for the SQLDatabaseEndpoint key.
  12. Connect to the database using SQL Server Management Studio with the following credentials: User id: DBUser and Password: DBU$er2020.

Setting Up the CYCLE_STORE Database

To configure your database, follow these steps:

  1. In SQL Server Management Studio, connect to the DB instance using the credentials defined above.
  2. Under “File,” select “New.”
  3. Choose “Query with Current Connection” or select “New Query” from the toolbar.
  4. Open CYCLE_STORE_Schema_data.sql from the GitHub repository and execute it.

This will establish the CYCLE_STORE database with all necessary tables and data.

Setting Up the ASP.NET MVC Core Application

To configure your ASP.NET application, complete the following steps:

  1. Open the re-architected application code cloned from the GitHub repository. The Dockerfile included in the solution enables Docker support.
  2. Open the appsettings.Development.json file and replace the RDS endpoint in the ConnectionStrings section with the output from the AWS CloudFormation stack, omitting the port number, which is :1433 for SQL Server.

The ASP.NET application should now display bike categories and subcategories.

Setting Up Amazon ECR

To establish your repository in Amazon ECR, perform the following steps:

  1. Access the Amazon ECR console and select “Repositories.”
  2. Click “Create repository.”
  3. Name the repository “coretoecsrepo.”
  4. Choose “Create repository.”
  5. Copy the repository URI for later use.
  6. Select the newly created repository and click “View push commands.”
  7. In the folder where you cloned the repository, navigate to the AdventureWorksMVCCore.Web folder.
  8. Follow the steps outlined in the View push commands popup to push your Docker image to Amazon ECR.

Setting Up Amazon ECS

To configure your ECS cluster, follow these steps:

  1. Open the Amazon ECS console and select “Clusters.”
  2. Click “Create cluster.”
  3. Choose the “Networking only” cluster template.
  4. Name your cluster “cycle-store-cluster.”
  5. Leave all other settings at their defaults and click “Create cluster.”
  6. Select your cluster and click “Task Definitions,” then “Create new Task Definition.”
  7. On the Select launch type compatibility page, choose “FARGATE” and proceed to the next step.
  8. On the Configure task and container definitions page, scroll down and select “Configure via JSON.”
  9. Paste the task definition JSON (task-definition.json) from the GitHub repo. Ensure to replace [YOUR-AWS-ACCOUNT-NO] in task-definition.json with your AWS account number. This task definition assumes you named your repository coretoecsrepo, so adjust accordingly if different. It also presumes you are using us-east-1 as your default region, so modify if necessary.
  10. Click “Save.”
  11. In the Task Definitions page, select “cycle-store-td.”
  12. From the Actions dropdown, choose “Run Task.”
  13. Select “Launch type” as Fargate.
  14. Choose your default VPC as Cluster VPC, select at least one Subnet, and edit Security Groups to select ECSSecurityGroup (created by the AWS CloudFormation stack).
  15. Finally, click “Run Task.”

Running Your Application

Select the link under the task to find the public IP. By navigating to the URL http://your-public-ip, you should see the .NET Core Cycle Store web application interface operational in Amazon ECS.

For additional insights, check out this excellent resource on YouTube: YouTube Video. Also, for more engaging content, don’t forget to visit this blog post. Furthermore, for expert insights on this topic, refer to this link.

SEO Metadata


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *