This repo contains code for provisioning the Servian Tech Challenge App into AWS using the AWS Cloud Development Kit (CDK)
The CDK app provisions the following infrastructure in AWS, along with associated IAM roles, security groups and other dependant resources:
- A VPC with private and public subnets
- A Postgres RDS instance with multi-az enabled for the database
- An auto-generated secret for the RDS database password
- An ECS cluster with a load-balanced auto-scaled Fargate task to run the app container
Unfortunately I ran out of time to get the app fully working, but it does provision the infrastructure, create the database instance and serve the app from the ALB
Note: The deployment process was tested on Windows 10, but should also work on Mac and Linux
- An AWS account for the infrastructure to be provisioned into (a single region is used)
- An IAM user with an access key for programmatic access. The user must have permission to provision the resources into AWS (the deployment process was developed and tested with AdministratorAccess permissions)
- Node.js installed (tested with 14.17.6) https://nodejs.org/en/download/
- AWS CLI v2 installed (tested with 2.0.26) https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html
- AWS CDK v1 installed (tested with 1.124.0) https://docs.aws.amazon.com/cdk/latest/guide/getting_started.html
- Python v3.x installed (tested with 3.9.5) https://www.python.org/downloads/
- Git installed and configured (tested with 2.23.0). https://gitforwindows.org/
- Open a command prompt or terminal. It should stay open for all of the steps in this deployment guide
- Run
aws configure
- When prompted, enter values for
AWS Access Key ID
andAWS Secret Access Key
for the IAM user - When prompted for
Default region name
, enter the region that you want to deploy the app into (the app was developed and tested in ap-southeast-2) - When prompted for
Default output format
, press Enter
Run the commands below to clone this repo locally
git clone https://github.com/bazd/cdk-ecs-rds.git
cd cdk-ecs-rds
Run the following commands according to the OS of the deployment environment
Windows
python -m venv .venv
.venv\scripts\activate.bat
pip install -r requirements.txt
Mac / Linux:
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
- Run
cdk synth
- This executes the app, which causes the resources defined in it to be translated into an AWS CloudFormation template locally
- A list of the resources to be created is displayed
- Run
cdk deploy
then entery
to confirm when prompted- This provisions the resources into AWS using the automatically generated CloudFormation template
- The terminal window will display the status of the deployment of each resource
- You can optionally log in to the AWS Console and see that a CloudFormation stack has been created
- Wait for the deployment to complete - it should take about 10 mins
- When all resources have finished deploying, 2 outputs will be displayed. These are the Load Balancer DNS name and the Service URL, and can be used to connect to the app
- Copy the Service URL into a web browser to access the app front end. (Unfortunately the app is not fully functional, but you should see a _servian logo and a To Do heading)
If you want to completely remove all resources, do the following:
- Run
cdk destroy
theny
to confirm - From the AWS console, manually delete any CloudWatch log groups with a prefix of
TcaStack-TcaTask
Potential improvements that can be added as required
- Support for multiple environments, with different config in each (eg no multi-az database in dev)
- Split the CDK app into separate stacks for easier management (eg network, app and database stacks)
- IAM deployment user with permissions restricted to the minimum required
- CI/CD for deployment
- Nice DNS cname record pointing to the ALB name
- RDS storage autoscaling
- The healthcheck settings for the ALB target group should be configured to use the
/healthcheck/
endpoint and not the default/
- Monitoring
- The
updatedb
task should only be executed in a new environment and not with every deployment
cdk ls
list all stacks in the appcdk synth
emits the synthesized CloudFormation templatecdk deploy
deploy this stack to your default AWS account/regioncdk diff
compare deployed stack with current statecdk docs
open CDK documentation