stratospheric-dev / cdk-constructs

Some CDK constructs to get started with deploying to AWS.
Apache License 2.0
35 stars 22 forks source link
aws-cdk cdk-constructs

Stratospheric CDK Constructs

Build & Release CodeQL

CDK Constructs Maven Central

A collection of Java CDK constructs that play well together to deploy an application and a database to Amazon ECS.

The constructs have been built to deploy a real application into production and will be updated as this application evolves.

These constructs are explained in further detail in our book.

From version 0.1.0 onwards, this constructs library only supports the AWS CDK v2. For migrating your existing AWS CDK v1 setup, follow the official AWS migration guide.

Installation

Load the dependency from Maven Central by adding this to your pom.xml:

<dependencies>
  <dependency>
    <groupId>dev.stratospheric</groupId>
    <artifactId>cdk-constructs</artifactId>
    <version>${latestVersion}</version>
  </dependency>
</dependencies>

... our your build.gradle:

implementation('dev.stratospheric:cdk-constructs:${latestVersion}')

The latestVersion is:

Use this version without the v prefix inside your pom.xml or build.gradle: v0.1.0 -> 0.1.0.

To override the version of the AWS Java CDK library, use a <dependencyManagement> inside your pom.xml:

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>software.amazon.awscdk</groupId>
      <artifactId>aws-cdk-lib</artifactId>
      <version>2.8.0</version>
    </dependency>
  </dependencies>
</dependencyManagement>

Construct Overview

A short description of the constructs in this project. For a more details description have a look at the Javadocs.

Usage

An example usage of the database construct might look like this:

public class DatabaseApp {

  public static void main(final String[] args) {
  App app = new App();

  String environmentName = (String) app.getNode().tryGetContext("environmentName");
  requireNonEmpty(environmentName, "context variable 'environmentName' must not be null");

  String applicationName = (String) app.getNode().tryGetContext("applicationName");
  requireNonEmpty(applicationName, "context variable 'applicationName' must not be null");

  String accountId = (String) app.getNode().tryGetContext("accountId");
  requireNonEmpty(accountId, "context variable 'accountId' must not be null");

  String region = (String) app.getNode().tryGetContext("region");
  requireNonEmpty(region, "context variable 'region' must not be null");

  Environment awsEnvironment = makeEnv(accountId, region);

  ApplicationEnvironment applicationEnvironment = new ApplicationEnvironment(
    applicationName,
    environmentName
  );

  PostgresDatabase database = new PostgresDatabase(
    app,
    "Database",
    awsEnvironment,
    applicationEnvironment,
    new PostgresDatabase.DatabaseProperties());

  app.synth();
  }

  static Environment makeEnv(String account, String region) {
  return Environment.builder()
    .account(account)
    .region(region)
    .build();
  }

}

An instance of ApplicationEnvironment specifies which environment the construct should be deployed into. You can have multiple instances of each construct running, each with a different application (i.e. the name of the service you want to deploy) and a different environment (i.e. "test", "staging", "prod" or similar).

Most constructs take a properties object in the constructor. Use these to configure the constructs. They have sensible defaults.

Most constructs require certain parameters to be available in the SSM parameter store. The PostgresDatabase construct, for example, needs the parameters exposed by the Network construct to be available in the SSM parameter store. Read the Javadocs to see which parameters are required. You can set those parameters manually, but it's easiest to have deployed a Network construct in the same environment beforehand.

While it's totally possible to put all constructs into the same CDK app, we recommend to put each construct into its own CDK app. The reason for this is flexibility. You may want to deploy and destroy a jump host separately from the rest. Or you may want to move a database between two environments. In these cases, having everything in the same app makes you very inflexible.

Also, a monolithic CDK app would require you to pass the parameters for ALL constructs, even if you only want to deploy or destroy a single one.

Release Process

Head over to the release information document.