ACTS Blog Selection
We use machine learning technology to do auto-translation. Click "English" on top navigation bar to check Chinese version.
Bob’s Used Books: A .NET Sample Application – Part 3: Infrastructure
Introduction
Welcome to the third and final post in the Bob’s Used Books blog post series.
In the
This post references v1.0.0 of Bob’s Used Books. The Bob’s Used Books v1.0.0 GitHub repository can be found
Overview
The Amazon Web Services CDK is an
IaC is often used in conjunction with DevOps to automate the provisioning of application environments as part of a CI/CD pipeline. IaC provides the following benefits:
- Application environments can be version controlled by committing IaC scripts to source control
- Environment creation and deletion can be automated, reducing the risk of human errors
- Environments can be created and deleted on demand, enabling agile software development workflows and greater cost control
When an Amazon Web Services CDK project is synthesized it generates an
Bob’s Used Books uses the Amazon Web Services CDK to provision a number of Amazon Web Services resources and services, including:
- An
Amazon Virtual Private Cloud (Amazon VPC) to host the application and its components - An
Amazon Simple Storage Service (Amazon S3) bucket to store book cover images - An
Amazon CloudFront distribution for low latency delivery of application assets - An
Amazon Cognito user pool and admin user for Customer Identity and Access Management (CIAM) - An
Amazon Relational Database Service (Amazon RDS) for SQL Server for the application backend - An
Amazon Elastic Cloud Compute (Amazon EC2) instance to act as the web server when the application is deployed to Amazon Web Services - A number of
Amazon Identity and Access Management (IAM) roles, policies, and permissions that enable the application’s components to communicate with each other.
As you can see, Bob’s Used Books takes advantage of numerous Amazon Web Services services. This is very common for applications that run in the cloud. Further, almost all applications these days have multiple non-production environments like Test and UAT in addition to a production environment. Using an IaC framework like the Amazon Web Services CDK guarantees that each of those environments is provisioned quickly and consistently.
With all that in mind, let’s take a look at how Bob’s Used Books uses the Amazon Web Services CDK.
CDK Stacks
As discussed in the overview, applications that run in the cloud often depend upon multiple services and resources. The Amazon Web Services CDK makes it easy to group these services and resources together into stacks. A stack is a group of services and resources that can be managed as a single unit. When a stack is deployed, all of the resources defined in that stack are deployed; when a stack is deleted, all the resources in that stack are deleted.
For small applications it might make sense to maintain a single stack that contains all the resources for that application. As the application grows to include more services and resources though, maintaining a monolithic stack could become difficult. At that point it makes sense to split that monolithic stack into multiple stacks.
Bob’s Used Books has been organized into four stacks:
- CoreStack – Defines an Amazon S3 bucket, an Amazon Cognito user pool, and a CloudFront distribution
- NetworkStack – Defines an Amazon VPC and associated resources
- DatabaseStack – Defines an Amazon RDS for SQL Server database
- EC2ComputeStack – Defines an Amazon EC2 instance that acts as the web server and deploys the application to that web server
Organizing the resources for Bob’s Used Books into stacks provides greater control over resource deployment. When you are in Integrated Debugging mode (see the
cdk deploy BobsBookstoreCore
When you want to simulate a production environment you need to deploy all of the stacks. You can do that with the following command:
cdk deploy BobsBookstoreEC2
But wait, doesn’t that just provision the web server and deploy the application? What about the network resources and the database? The Amazon Web Services CDK project in Bob’s Used Books takes advantage of a feature of the Amazon Web Services CDK framework called
Cross-Stack References
The four stacks that are defined by Bob’s Used Books are instantiated in the Main method of Bookstore.Cdk/Program.cs
. When EC2ComputeStack is initialized it is passed an instance of EC2ComputeStackProps:
var ec2Stack = new EC2ComputeStack(app, $"{Constants.AppName}EC2", new EC2ComputeStackProps
{
Env = env,
Vpc = networkStack.Vpc,
Database = databaseStack.Database,
ImageBucket = coreStack.ImageBucket,
WebAppUserPool = coreStack.WebAppUserPool
});
EC2ComputeStackProps implements the IStackProps interface and is populated with the resources created in NetworkStack, DatabaseStack, and CoreStack. For example, EC2ComputeStack deploys an Amazon EC2 instance into the VPC that was created by NetworkStack. It uses the Amazon Cognito user pool created in CoreStack to generate a user pool client app for the web server, and it uses the Amazon S3 bucket and RDS for SQL Server database to generate the appropriate access permissions for the web server. When you run cdk deploy BobsBookstoreEC2
the Amazon Web Services CDK recognizes that EC2ComputeStack is dependent upon the VPC created in NetworkStack, the database created in DatabaseStack, and the bucket and user pool created in CoreStack, and it ensures they are provisioned first.
Application Deployment
In addition to defining compute resources for the solution, EC2ComputeStack also deploys the application to the web server.
NOTE: Application deployments are typically facilitated by CI/CD pipelines rather than an IaC framework like Amazon Web Services CDK, however we want to provide the .NET development community with a simple, self-contained development experience and deploying the application via the CDK achieved that goal.
The Bookstore.Cdk project has a folder called EC2Artifacts that contains the following files:
bobsbookstore.conf
– An Apache configuration file that defines the virtual host for Bob’s Used Books.bobsbookstore.service
– A Linux service that starts the application via the dotnet CLI.ssl.conf
– A configuration file that is used as part of the self-signed certificate configuration.configure_ec2_web_app.sh
– A bash script that configures the EC2 instance that hosts Bob’s Used Books.
EC2ComputeStack uploads these files, along with the application outputs that are produced when the application is published, to an S3 bucket (this is a different bucket from the one which is defined in CoreStack). This is achieved by using the Asset class defined in the Amazon.CDK.S3.Assets namespace and is implemented in the
Once the EC2 instance is provisioned a
Clean Up
If you have deployed any of the CDK stacks that are included in Bob’s Used Books you can delete the Amazon Web Services services and resources that were created by opening a command-line prompt, navigating to the application solution folder, and running the following command:
cdk destroy BobsBookstore*
This is an important step to ensure you don’t incur unnecessary costs.
Conclusion
Bob’s Used Books takes advantage of the Amazon Web Services SDK for .NET to define and provision the required Amazon Web Services resources. The Amazon Web Services CDK for .NET is a powerful Infrastructure-as-Code framework that enables .NET developers to represent their Amazon Web Services infrastructure using a programming language with which they are familiar and productive.
This is the final post in the introductory series to Bob’s Used Books. The
This is not, however, the last you will hear about Bob’s Used Books! On the contrary, we plan on using Bob’s Used Books to demonstrate common .NET modernization scenarios, to demonstrate integrating with additional Amazon Web Services services, and for inclusion in upcoming training and enablement content.
You can