Introduction
Welcome to the concluding installment of the Amazon IXD – VGT2 Las Vegas blog series. In the initial post, I explored how to kick off the application, Bob’s Used Books, and elaborated on the various debug and deployment modes available for testing. The second post delved into the architecture of Bob’s Used Books, detailing the decisions made during the development of this sample application. In this final post, we will examine the AWS Cloud Development Kit (AWS CDK) project responsible for provisioning AWS resources and deploying the sample application.
This article refers to version 1.0.0 of Bob’s Used Books, which can be found in the GitHub repository here.
Overview
The AWS CDK serves as an Infrastructure-as-Code (IaC) framework, allowing developers to describe AWS resources and services as application code. Executing this code against an AWS account automatically provisions the defined resources and services within that account.
IaC is commonly utilized alongside DevOps to automate the setup of application environments as part of a CI/CD pipeline. The advantages of IaC include:
- Version control of application environments by committing IaC scripts to source control
- Automation of environment creation and deletion, which reduces the likelihood of human errors
- On-demand creation and deletion of environments, facilitating agile software development workflows and enhanced cost management
When synthesized, an AWS CDK project generates an AWS CloudFormation template, which is then utilized by the CloudFormation service for processing. Although developers can write CloudFormation scripts directly, the AWS CDK offers higher-level constructs that often allow for achieving the same results with reduced effort.
Bob’s Used Books utilizes the AWS CDK to provision various AWS resources and services, including:
- An Amazon Virtual Private Cloud (Amazon VPC) for hosting the application and its components
- An Amazon Simple Storage Service (Amazon S3) bucket for storing 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 to power the application backend
- An Amazon Elastic Cloud Compute (Amazon EC2) instance serving as the web server for the application on AWS
- Multiple Amazon Identity and Access Management (IAM) roles, policies, and permissions facilitating communication among the application components
As evidenced, Bob’s Used Books leverages a multitude of AWS services, a common practice for cloud-based applications. Moreover, most applications today have multiple non-production environments such as Test and UAT, in addition to the production environment. Employing an IaC framework like the AWS CDK ensures each of these environments is provisioned swiftly and consistently.
Now, let’s explore how Bob’s Used Books implements the AWS CDK.
CDK Stacks
As previously mentioned, applications operating in the cloud typically rely on a variety of services and resources. The AWS CDK simplifies the grouping of these services and resources into stacks. A stack is a collection of services and resources managed as a single entity. When a stack is deployed, all resources defined within it are also deployed; conversely, when a stack is deleted, all its resources are removed.
For smaller applications, it may be practical to maintain a single stack encompassing all resources. However, as an application expands, managing a monolithic stack may become cumbersome. In such cases, it is wise to divide the stack into multiple smaller stacks.
Bob’s Used Books is organized into four distinct stacks:
- CoreStack: Defines an Amazon S3 bucket, an Amazon Cognito user pool, and a CloudFront distribution
- NetworkStack: Establishes an Amazon VPC and its associated resources
- DatabaseStack: Configures an Amazon RDS for SQL Server database
- EC2ComputeStack: Sets up an Amazon EC2 instance that acts as the web server hosting the application
This organization into stacks enhances control over resource deployment. For instance, in Integrated Debugging mode (as detailed in the first post), you only need to deploy the Amazon S3 bucket, CloudFront distribution, and Cognito user pool. The application continues to run locally, eliminating the need for networking resources, the database, or the web server. To deploy only the necessary resources for Integrated Debugging, you can use the following command:
cdk deploy BobsBookstoreCore
To simulate a production environment, all stacks must be deployed using:
cdk deploy BobsBookstoreEC2
One might wonder, doesn’t this command merely provision the web server and deploy the application? What about the network resources and the database? The AWS CDK project of Bob’s Used Books employs a feature known as cross-stack references. The EC2ComputeStack holds references to resources defined in the other stacks. When you deploy EC2ComputeStack, the AWS CDK acknowledges these dependencies and ensures they are provisioned as well. Let’s take a closer look at cross-stack references in Bob’s Used Books.
Cross-Stack References
The four stacks in Bob’s Used Books are instantiated in the Main method of Bookstore.Cdk/Program.cs. Upon initializing EC2ComputeStack, it receives 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 filled with resources created in NetworkStack, DatabaseStack, and CoreStack. For instance, EC2ComputeStack deploys an Amazon EC2 instance into the VPC established by NetworkStack. It uses the Amazon Cognito user pool from CoreStack to create a user pool client app for the web server, along with the Amazon S3 bucket and RDS for SQL Server database to establish the necessary access permissions for the web server. When you execute cdk deploy BobsBookstoreEC2
, the AWS CDK recognizes that EC2ComputeStack is dependent on the VPC from NetworkStack, the database from DatabaseStack, and both the bucket and user pool from CoreStack, ensuring they are provisioned beforehand.
Application Deployment
Beyond defining compute resources for the solution, EC2ComputeStack also deploys the application to the web server.
NOTE: Application deployments are typically handled via CI/CD pipelines rather than an IaC framework like AWS CDK; however, we aimed to provide the .NET development community with a straightforward, self-contained development experience, and deploying the application via the CDK accomplished that goal.
Within the Bookstore.Cdk project, there exists a folder named EC2Artifacts containing the following files:
- bobsbookstore.conf – An Apache configuration file outlining the virtual host for Bob’s Used Books.
- bobsbookstore.service
In conclusion, organizing resources into distinct stacks and using cross-stack references provide a robust framework for deploying applications in AWS. For further insights on this topic, check out this excellent resource. For more information on cloud development, you can refer to another blog post that offers valuable perspectives.
Location:
Amazon IXD – VGT2
6401 E Howdy Wells Ave,
Las Vegas, NV 89115