AWS Developer Tools Blog
Developing a Microsoft .NET Core Web API application with Amazon Aurora Database using AWS CDK
In our previous blog, we showed you how to build a Microsoft.NET Web API application with Amazon Aurora Database using AWS CloudFormation. In this blog, we will leverage the development capabilities of defining cloud infrastructure as code using AWS Cloud Development Kit (AWS CDK) which provisions it through AWS CloudFormation. The AWS CDK is an open source software development framework to model and provision your cloud application resources using familiar programming languages, including TypeScript, JavaScript, Python, C# and Java. For the solution in this blog, we will use C# for the infrastructure code. Let’s get started!
At a high-level, we will go through the following:
- Create a simple TODO, Microsoft .NET Core Web API application and integrate AWS SDK Package like SSM into it.
- Use AWS CDK to define the cloud infrastructure.
- Use Elastic Container Service (ECS), Elastic Container Registry (ECR), Amazon Systems Manager (SSM) (maintains the Aurora DB Credentials).
- Amazon Aurora Database (Serverless) is used as the backend.
The Github source code includes a “cdk” folder with Microsoft .NET Core based AWS Cloud Development Kit (CDK) solution to build the infrastructure. This solution constructs the AWS infrastructure where the “webapi” (.NET Core Web api) is packaged, built as an artifact and pushed to AWS ECR. The provided .NET project leverages AWS SDK, Mysql data packages to connect to MySQL and interact with Amazon Aurora database. The exposed Web API endpoint performs HTTP calls (GET & POST) to add/retrieve TODOs. The end user can use any http get/put tool like curl or UI tools like Google Chrome ARC Rest Client or POSTMAN to validate the changes.
Overview of the AWS services used in this solution
Amazon Aurora, a MySQL and PostgreSQL-compatible relational database is used as the backend for the purpose of this project.
Amazon Elastic Container Service is used using Docker template containers and allows you to easily run and scale containerized applications on AWS. The Dockerfile is provided as part of the solution.
Amazon Elastic Container Registry, the AWS provided Docker container registry is used and integrated with ECS, simplifying the development to production workflow.
Prerequisites
We will use Docker Containers to deploy the Microsoft .NET Web API Application. The following are required to setup your development environment:
- Dotnet CLI:
- Web API application was built using Microsoft .NET core 2.2
- Please refer Microsoft Documentation for installation.
- Docker
- Install Docker depending on your hardware specification.
- Make sure the docker daemon is running
- AWS CDK
- Additionally, we use AWS SDK, MySql data packages for the Microsoft .NET project and have added them as nuget packages to the solution. In this example, we use Mysql data to connect to MySql/Aurora database and also AWS SDK Systems Manager to connect to Amazon Systems Manager.
Walk-through of the Solution
We will use the following steps to provision the infrastructure (and services) and deploy the application:
1. Clone the sample code from the GitHub location.
$ git clone https://github.com/aws-samples/aws-netcoreapi-aurora-cdk
The git source provided above has a “cdk” and “webapi” folder. “webapi” has the necessary .NET Web API solution. We will use the AWS CDK commands to build the infrastructure and deploy the webapi into ECS.
Once the code is downloaded, please take a moment to see how CDK provides a simpler implementation for spinning up an infrastructure using C# code. You may use Visual Studio Code or your favorite choice of IDE to open the folder (aws-netcoreapi-aurora-cdk).
Open the file “/aws-netcoreapi-aurora-cdk/cdk/src/todo/TodoInfraStack.cs”. Code below (provided a snippet from the github solution) spins up a VPC for the required Cidr and number of availability zones.
IVpc todoVpc = new Vpc(this, Constants.VPC_NAME, new VpcProps{
Cidr = Constants.CIDR_RANGE,
MaxAzs = 4
});
Similarly, developers have an option of implementing the CDK Constructs and can define & deploy an AWS Service. In the below snippet, Fargate execution role is created by implementing the construct and returns an IAM Role. See full source in /aws-netcoreapi-aurora-cdk/cdk/src/todo/Modules/FargateExecutionRole.cs
public sealed class FargateExecutionRole: Construct {
...
...
public FargateExecutionRole(Construct scope, string id): base(scope, id){
this.scope = scope;
this.id = id;
this.Role = GetRole(
...
...
);
}
2. Build the CDK source code and deploy the AWS CloudFormation stacks.
$ cd cdk
$ dotnet build src
$ cdk bootstrap
$ cdk deploy --require-approval never
NOTE: Optionally, you can also use the “run.sh” script/bash file provided as part of the code base within “cdk” folder, that will take care of the above steps.
At the end of this step, you will create the Amazon Aurora DB table and the ECS Service where the .NET Core Web API is deployed. The output of the stack returns the following:
- HealthCheckUrl – http://<your_ALB>.us-east-1.elb.amazonaws.com/api/values
- Web ApiUrl – http://<your_ALB>.us-east-1.elb.amazonaws.com/api/todo
Once the above CloudFormation stack is created successfully, take a moment to identify the major components. Here is the infrastructure you’d have created —
- Infrastructure containing VPC, Public & Private Subnet, Route Tables, Internet Gateway, NAT Gateway, Public Load Balancer, ECS Cluster.
- Other AWS Services – ECR, Amazon Aurora Database Serverless, Systems Manager, CloudWatch Logs.
Using CDK constructs, we have built the above infrastructure and integrated the solution with a Public Load Balancer. The output of this stack will give the API URLs for health check and API validation. As you notice by defining the solution using CDK, you were able to:
- Use object-oriented techniques to create a model of your system
- Organize your project into logical modules
- Code completion within your IDE
Other major advantages using this CDK approach include, as a developer/development team we should be able to
- Use logic (if statements, for-loops, etc) when defining your infrastructure
- Define high level abstractions, share them, and publish them to your team, company, or community
- Share and reuse your infrastructure as a library
- Testing your infrastructure code using industry-standard protocols
- Use your existing code review workflow
3. Stack Verification
Let’s test the TODO API using any REST API tools, like Postman, Chrome extension ARC or RestMan.
- GET – Open browser and you can hit the Web ApiUrl to see the data.
- http://<your_ALB>.us-east-1.elb.amazonaws.com/api/todo
- POST – Create a sample –
Set Headers as “Content-type” & “application/json”
Sample request:
{
"Task": "Coding for new project1",
"Status": "InProgress"
}
4. Code Cleanup
Make sure the following resources are deleted before performing the delete/destroy:
- Contents of the S3 files are deleted.
- In AWS Console, look for “CDKToolkit” stack
- Go to “Resources” tab, select the s3 bucket
- Select all the contents & delete the contents manually
Once above steps are completed. Execute the below command(s):
– AWS CDK Command to destroy the stack
$ cdk destroy
– Optionally the below CLI Commands or AWS Console can also be used to delete the stack(s)
$ aws cloudformation delete-stack --stack-name ToDoInfraStack
$ aws cloudformation delete-stack --stack-name CDKToolkit
Conclusion
As you can see, we were able to onboard an ASP.NET Core Web API application and integrate it with various AWS Services. The post walked through deploying Microsoft .NET Core application code as containers with infrastructure as code using CDK. To try out architecture as code with applications on AWS, check out AWS CDK ECS patterns.
We encourage you to try this example and see for yourself how this overall application design works within AWS. Then, it will just be a matter of replacing your current applications (Web API, MVC, or other Microsoft .NET core application), package them as Docker containers and let the AWS ECS Container handle the process efficiently.
If you have any questions/feedback about this blog please provide your comments below!
References
- AWS Cloud Development Kit (CDK)
- AWS CDK .NET API Reference
- Microsoft .Net Core
- Windows on AWS
- Docker Containers
- Amazon ECS Application Architecture
About the authors
Sivasubramanian Ramani (Siva Ramani) is a Sr Cloud Application Architect at AWS. His expertise is in application optimization, serverless solutions and using Microsoft application workloads with AWS.
Naveen Balaraman is Cloud Application Architect at Amazon Web Services. He is passionate about application optimization, architecting microservices and helping customers leverage the power of AWS cloud easily.