This article is the second installment in a two-part series focused on migrating and containerizing an updated enterprise application. In Part 1, we provided a detailed guide on re-architecting a legacy ASP.NET MVC application and transitioning it to the .NET Core Framework. In this post, we will deploy the previously modernized application to Amazon Elastic Container Service (Amazon ECS) and operate it as a task using AWS Fargate.
Overview of the Solution
After transitioning the legacy MVC ASP.NET application to ASP.NET Core in the first post, we will now containerize this application using Docker and host it within the ECS cluster.
The architecture is illustrated in the following diagram.
First, launch a SQL Server Express RDS (1) instance and establish a Cycle Store database on that instance, which includes tables for various categories and subcategories of bikes. The re-architected and modernized ASP.NET Core application serves as the foundation for this post, utilizing AWS Secrets Manager (2) to retrieve database credentials for accessing the Amazon RDS instance. The next step involves creating a Docker image of the application and pushing it to Amazon Elastic Container Registry (Amazon ECR) (3). Subsequently, you will create an ECS cluster (4) to run the Docker image as an AWS Fargate task.
Prerequisites
To follow along with this tutorial, ensure you have the following prerequisites in place:
- An AWS account.
- An AWS Identity and Access Management (IAM) user with AdministratorAccess. For detailed guidance, refer to this link to create an IAM role with Administrator access.
- AWS Tools for Windows. Follow these steps to configure your AWS profile.
- The .NET Core 3.1 SDK installed. For instructions, see Download .NET Core 3.1.
- Microsoft Visual Studio 2017 or later (Visual Studio Code can be an alternative).
- SQL Server Management Studio for connecting to your SQL Server instance.
- Experience in ASP.NET application development.
This post implements the solution in the us-east-1 region.
Source Code
Clone the source code from the GitHub repository. The source code folder contains the re-architected source code along with the AWS CloudFormation template for launching the infrastructure and the Amazon ECS task definition.
Setting Up the Database Server
To ensure your database is functional from the start, use a CloudFormation template to create an instance of Microsoft SQL Server Express and AWS Secrets Manager secrets for storing database credentials, security groups, and IAM roles necessary for accessing Amazon Relational Database Service (Amazon RDS) and Secrets Manager. The stack should take approximately 15 minutes to provision, primarily due to the service setup time.
- Navigate to the AWS CloudFormation console and select Create stack.
- For Prepare template, select Template is ready.
- For Template source, choose Upload a template file.
- Upload SqlServerRDSFixedUidPwd.yaml, available in the GitHub repo.
- Click Next.
- For Stack name, enter SQLRDSEXStack.
- Click Next.
- Keep the remaining options at their defaults.
- Acknowledge that AWS CloudFormation may create IAM resources with custom names.
- Click Create stack.
- When the status shows CREATE_COMPLETE, navigate to the Outputs tab.
- Record the value for the SQLDatabaseEndpoint key.
- 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:
- In SQL Server Management Studio, connect to the DB instance using the previously defined ID and password.
- Under File, select New.
- Choose Query with Current Connection or select New Query from the toolbar.
- Open CYCLE_STORE_Schema_data.sql from the GitHub repository and execute it.
This creates the CYCLE_STORE database, complete with all required tables and data.
Setting Up the ASP.NET MVC Core Application
To configure your ASP.NET application, complete these steps:
- Open the re-architected application code you cloned from the GitHub repository. The included Dockerfile enables Docker support.
- Open the appsettings.Development.json file and replace the RDS endpoint in the ConnectionStrings section with the output from the AWS CloudFormation stack (excluding the port number :1433 for SQL Server).
The ASP.NET application should now load with bike categories and subcategories.
Setting Up Amazon ECR
To establish your repository in Amazon ECR, follow these steps:
- Navigate to the Amazon ECR console and click on Repositories.
- Select Create repository.
- For Repository name, enter coretoecsrepo.
- Click Create repository.
- Copy the repository URI for later use.
- Select the newly created repository and choose View push commands.
- In the folder where you cloned the repo, navigate to the AdventureWorksMVCCore.Web folder.
- In the View push commands pop-up window, complete steps 1–4 to push your Docker image to Amazon ECR.
Setting Up Amazon ECS
To configure your ECS cluster, follow these steps:
- Navigate to the Amazon ECS console and select Clusters.
- Choose Create cluster.
- Select the Networking only cluster template.
- Name your cluster cycle-store-cluster.
- Leave the other settings at their defaults.
- Click Create cluster.
- Select your cluster.
- Choose Task Definitions and click Create new Task Definition.
- On the Select launch type compatibility page, choose FARGATE and click Next step.
- On the Configure task and container definitions page, scroll to the bottom and select Configure via JSON.
- In the text area, input the task definition JSON (task-definition.json) from the GitHub repository. Ensure to replace [YOUR-AWS-ACCOUNT-NO] in task-definition.json with your AWS account number at lines 44, 68, and 71. The task definition assumes your repository is named coretoecsrepo. Adjust accordingly if your name is different. It also assumes you are using us-east-1 as your default region, so modify the region in task-definition.json at lines 15 and 44 if you are in a different region.
- Click Save.
- On the Task Definitions page, select cycle-store-td.
- From the Actions drop-down menu, choose Run Task.
- Select Launch type as Fargate.
- Choose your default VPC as Cluster VPC.
- Select at least one Subnet.
- Click Edit Security Groups and select ECSSecurityGroup (created by the AWS CloudFormation stack).
- Finally, click Run Task.
Running Your Application
Select the link under the task to find the public IP. When you navigate to the URL http://your-public-ip, you should see the .NET Core Cycle Store web application interface running in Amazon ECS. This excellent resource offers further insights into containerization, so check it out.
For more related content, visit this blog post. Additionally, if you’re interested in a deeper understanding of this topic, consult these experts. Lastly, for visual learners, this video could be highly beneficial.
Leave a Reply