CDK4J Maven Plugin

CDK4J Maven Plugin

The AWS CDK Maven plugin produces and deploys CloudFormation templates based on your cloud infrastructure defined by means of CDK. The goal of the project is to improve the experience of Java developers while working with CDK by eliminating the need for installing Node.js and interacting with the CDK application by means of CDK Toolkit.

Installation

Add the plugin to your Maven project:

<plugin>
    <groupId>org.datapith</groupId>
    <artifactId>cdk4j-maven-plugin</artifactId>
    <version>${cdk4j.version}</version>
    <executions>
        <execution>
            <id>synth</id>
            <goals>
                <goal>bootstrap</goal>
                <goal>synth</goal>
                <goal>deploy</goal>
            </goals>
            <configuration>
                <!-- Full class name of the app class defining your stacks -->
                <app>...</app>
            </configuration>
        </execution>
    </executions>
</plugin>

Authentication

To deploy stacks with the CDK4J Maven plugin, you must configure security credentials on your local machine by using the following environment variables AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and AWS_REGION.

Usage

The plugin provides the following goals:

  • synth - Synthesizes CloudFormation templates based on the resources defined in your CDK application.
  • bootstrap - Deploys toolkit stacks required by the CDK application to an AWS.
  • deploy - Deploys the CDK application to an AWS (based on the synthesized resources).
  • destroy - Destroys the CDK application on AWS.

Synthesize

Synthesize a CDK app to produce a cloud assembly, including an AWS CloudFormation template for each stack.

During the execution of the synth goal, a cloud assembly is synthesized. The cloud assembly is a directory (target/cdk.out by default) containing the artifacts required for the deployment, i.e. CloudFormation templates, AWS Lambda bundles, file and Docker image assets etc. The artifacts in the cloud assembly directory are later used by bootstrap and deploy goals.

The only mandatory parameter required by the goal is <app>, which is a full class name of the CDK app class defining the cloud infrastructure. The application class must either extend software.amazon.awscdk.App or define a main method which is supposed to create an instance of App, define cloud [constructs] and call App#synth() method in order to produce a cloud assembly with CloudFormation templates.

ParameterTypeDescription
<profile>StringA profile that will be used to find credentials and region.
<app>StringFull class name of the CDK app class defining the cloud infrastructure.
<cloudAssemblyDirectory>StringA directory where the cloud assembly will be synthesized.
<arguments>List<String>A list of arguments to be passed to the CDK application.
<skip>booleanEnables/disables the execution of the goal.

Bootstrap

CDK applications require a “toolkit stack” that includes the resources required for the application operation. The CDK4J Maven plugin will automatically deploy the toolkit stack (or update if needed) during the execution of bootstrap goal (provided that the required toolkit stack version wasn’t already deployed).

ParameterTypeDescription
<profile>StringA profile that will be used to find credentials and region.
<cloudAssemblyDirectory>StringA cloud assembly directory with the deployment artifacts (target/cdk.out by default).
<toolkitStackName>StringThe name of the CDK toolkit stack to use (CDKToolkit is used by default).
<bootstrapParameters>Map<String, String>Input parameters for the bootstrap stack. In the case of an update, existing values will be reused.
<bootstrapTags>Map<String, String>Tags that will be added to the bootstrap stack.
<stacks>List<String>Stacks to deploy. By default, all the stacks defined in your application will be deployed.
<skip>booleanEnables/disables the execution of the goal.

Deploy

Deploy one or more AWS CDK stacks into your AWS environment.

To deploy the synthesized application add the deploy goal to the execution (deploy and bootstrap goals are attached to the deploy Maven phase). During deployment, the CDK4J maven plugin will output progress indicators, similar to what can be observed from the AWS CloudFormation console.

ParameterTypeDescription
<profile>StringA profile that will be used to find credentials and region.
<cloudAssemblyDirectory>StringA cloud assembly directory with the deployment artifacts (target/cdk.out by default). Using the library, you can also pass the CloudAssembly directly.
<toolkitStackName>StringThe name of the CDK toolkit stack to use (CDKToolkit is used by default).
<stacks>List<String>Stacks to deploy. By default, all the stacks defined in your application will be deployed.
<parameters>Map<String, String>Input parameters for the stacks. For the new stacks, all the parameters without a default value must be specified. In the case of an update, existing values will be reused.
<tags>Map<String, String>Tags to be applied for all stacks.
<notificationArns>Set<String>SNS ARNs to publish stack related events.
<skip>booleanEnables/disables the execution of the goal.

Destroy

Delete one or more AWS CDK stacks from your AWS environment.

To destroy an existing application into an AWS, add destroy goal to the execution.

When you delete a stack, resources in the stack will be destroyed, unless they were configured with a DeletionPolicy of Retain.

During stack deletion, the CDK4J plugin will output progress information similar to what can be observed from the AWS CloudFormation console.

ParameterTypeDescription
<profile>StringA profile that will be used to find credentials and region.
<cloudAssemblyDirectory>StringA cloud assembly directory with the deployment artifacts (target/cdk.out by default).
<stacks>List<String>Stacks to deploy. By default, all the stacks defined in your application will be deployed.
<skip>booleanEnables/disables the execution of the goal.