blakegreendev / cdk-application-pipeline-ts

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Overview

Reference implementation of application pipeline from DPRA that includes a CI/CD pipeline to build an application and deploy via AWS CloudFormation across different accounts for each environment.

Pipeline Diagram

The application is a Java Spring Boot API that runs as an Amazon ECS Service with an Amazon Aurora database.

Deployment Diagram

This reference implementation contains the following significant components:

  • infrastructure/ - Amazon CDK code necessary to provision the pipeline (see pipeline.ts) and the application (see deployment/index.js). This directory also contains tests for the infrastructure code, including SAST (static application security testing) with cdk-nag.
  • src/ - Java source code for a simple API that manages a list of fruits in a relational database. Unit tests are available for the application in src/test/java.

Warning This reference implementation has intentionally not followed the following AWS Well-Architected best practices to make it accessible by a wider range of customers. Be sure to address these before using this code for any workloads in your own environment:

  • TLS on HTTP endpoint - the listener for the sample application uses HTTP instead of HTTPS to avoid having to create new ACM certificates and Route53 hosted zones. This should be replaced in your account with an HTTPS listener.

Prerequisites

Before beginning this walk through, you should have the following prerequisites:

Initial setup

  1. Clone the repository from GitHub (https://github.com/aws-samples/aws-deployment-pipeline-reference-architecture):
git clone https://github.com/aws-samples/aws-deployment-pipeline-reference-architecture.git
cd aws-deployment-pipeline-reference-architecture/examples/cdk-application-pipeline

This reference implementation contains the following significant components:

  • infrastructure/ - Amazon CDK code necessary to provision the pipeline
  • src/ - Java source code for a simple API that manages a list of fruits in a relational database
  1. To build and run the unit tests for the Spring Boot Application, run the following command:
mvn package
  1. To install dependencies run:
npm install
  1. To bootstrap your AWS accounts for AWS CDK, run:
npx ts-node infrastructure/src/bootstrap.ts

BootStrap-1 Diagram

Use the keyboard up/down arrow-key to choose the AWS profile for toolchain, beta, gamma and production as prompted.

BootStrap-2 Diagram

The bootstraping script shall print out 4 commands as shown above.

  1. Run the following commands to bootstrap toolchain environment, account id 111111111111:
npx cdk bootstrap --profile toolchain aws://111111111111/us-east-1

BootStrap-3 Diagram

  1. Run the following commands to bootstrap beta environment, account id 222222222222:
npx cdk bootstrap --profile beta --trust 111111111111 --cloudformation-execution-policies 'arn:aws:iam::aws:policy/AdministratorAccess' aws://222222222222/us-west-2
  1. Run the following commands to bootstrap gamma environment, account id 333333333333:
npx cdk bootstrap --profile gamma --trust 111111111111 --cloudformation-execution-policies \
 arn:aws:iam::aws:policy/AdministratorAccess aws://333333333333/us-west-2 aws://333333333333/us-east-1
  1. Run the following commands to bootstrap production environment, account id 444444444444:
npx cdk bootstrap --profile production --trust 111111111111 --cloudformation-execution-policies \
 arn:aws:iam::aws:policy/AdministratorAccess aws://444444444444/us-west-2 aws://444444444444/us-east-1 aws://444444444444/eu-central-1 aws://444444444444/eu-west-1 aws://444444444444/ap-south-1 aws://444444444444/ap-southeast-2

To learn more about the CDK boostrapping process, see: https://docs.aws.amazon.com/cdk/v2/guide/bootstrapping.html

Pipeline Deploy

To deploy the pipeline to the toolchain AWS account run:

npx cdk deploy --profile toolchain --all --require-approval never

Pipeline-1 Diagram

Using AWS management console, login to toolchain account and click AWS CodePipeline to check the different stages of the pipeline.

Fruit API Diagram

Here is the deployment to Beta environment.

Beta-0 Diagram

Here is the deployment to Gamma environment.

Gamma-0 Diagram

Click the Review button to manually approve the PromoteToProd.

Prod-0 Diagram

Here is the deployment to Prod-1 environment.

Prod-1 Diagram

Here is the deployment to Prod-2 environment.

Prod-2 Diagram

Here is the application running in production in us-east-1 region.

App Diagram

(OPTIONAL) If you'd like to make changes and deploy with the pipeline, you'll need to setup Git for AWS CodeCommit and then clone the new CodeCommit repository:

git clone https://git-codecommit.us-west-2.amazonaws.com/v1/repos/fruit-api

To fix failed Trivy scans, see: https://www.mojohaus.org/versions/versions-maven-plugin/index.html or https://docs.npmjs.com/packages-and-modules

Pipeline Teardown

The cleanup task will take these actions:

  1. Destroy the CDK app using: npx cdk --profile dpra-toolchain destroy --all
  2. Empty and remove the CDKToolkit S3 staging buckets in all environments
  3. Empty and remove the CDKToolkit ECR repository in all environments
  4. Delete the CDKToolkit stacks in all environments
./infrastructure/src/cleanup.ts

NOTE: account removal is left to the account owner.

About

License:Apache License 2.0


Languages

Language:TypeScript 70.1%Language:Java 21.8%Language:HTML 6.8%Language:Dockerfile 1.3%