-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a Java CDK app example to demonstrate how CDK can be used from Java. The example demonstrates: * Configuration of `cdk.json` to allow idiomatic use of the toolkit (`cdk synth`, `cdk deploy`). * How to write a CDK app, stack, etc. * How to write a reusable construct (`SinkQueue`) along with unit tests See README file for more details. Misc: * Reorganize `examples/` directory to `cdk-examples-java` and `cdk-examples-typescript`
- Loading branch information
Elad Ben-Israel
committed
Jun 21, 2018
1 parent
b101d1c
commit d8ebfe1
Showing
39 changed files
with
673 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
!*.t.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# CDK Java Example | ||
|
||
This an example of a CDK program written in Java. | ||
|
||
> NOTE: Since this example currently resides inside the CDK repository, it takes | ||
a dependency on the CDK for Java maven package that's built under | ||
`packages/aws-cdk-java`. To enable this, we generate the `pom.xml` file, so we | ||
can plug in the locations of the local maven repositories. This is not | ||
something you will need to do if you are simply using the published CDK maven | ||
package. | ||
|
||
## Building | ||
|
||
To build this app, run `npm run prepare`. This will: | ||
|
||
1. Generate `project/pom.xml` with correct references to jsii and CDK | ||
dependencies. | ||
2. Run `mvn package`, which will compile, test and assemble an executable jar. | ||
|
||
## IDE | ||
|
||
Once the `pom.xml` file is generated, the [`./project`](./project) directory is | ||
fully functional Maven project, and you should be able to open it from any Java | ||
IDE which supports Maven. | ||
|
||
You can use the IDE to write code and unit tests, but you will need to use the | ||
CDK toolkit if you wish to synthesize/deploy stacks. | ||
|
||
## CDK Toolkit | ||
|
||
The [`cdk.json`](./cdk.json) file in the root of this repository includes | ||
instructions for the CDK toolkit on how to execute this program. Specifically, | ||
it will use `java -jar <executale-jar> app` for the `--app` switch. This means, | ||
you should be able to use the toolkit normally: | ||
|
||
``` | ||
$ cdk ls | ||
<list all stacks in this program> | ||
$ cdk synth | ||
<cloudformation template> | ||
$ cdk deploy | ||
<deploy stack to your account> | ||
$ cdk diff | ||
<diff against deployed stack> | ||
``` | ||
|
||
If you make modifications, make sure to rebuild the app, either by callign `mvn | ||
package` from `./project` or `npm run prepare` from the root. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"app": "java -jar project/target/cdk-examples-java-0.7.2-beta-jar-with-dependencies.jar app" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/bash | ||
set -euo pipefail | ||
node ./pom.xml.t.js > project/pom.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"name": "cdk-examples-java", | ||
"version": "0.7.2-beta", | ||
"description": "CDK examples in Java", | ||
"private": true, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/awslabs/aws-cdk" | ||
}, | ||
"pkglint": { | ||
"ignore": true | ||
}, | ||
"scripts": { | ||
"prepare": "/bin/bash generate.sh && cd project && mvn package", | ||
"cdk": "cdk", | ||
"test": "echo ok" | ||
}, | ||
"author": { | ||
"name": "Amazon Web Services", | ||
"url": "https://aws.amazon.com" | ||
}, | ||
"license": "Apache-2.0", | ||
"dependencies": { | ||
"aws-cdk-java": "^0.7.2-beta" | ||
}, | ||
"devDependencies": { | ||
"aws-cdk": "^0.7.2-beta", | ||
"pkgtools": "^0.7.2-beta" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
const path = require('path'); | ||
const version = require('./package.json').version; | ||
|
||
const mavenFromNpm = name => ({ | ||
version: require(`${name}/package.json`).version, | ||
repo: path.join(path.dirname(require.resolve(name)), 'maven-repo'), | ||
}); | ||
|
||
const cdk = mavenFromNpm('aws-cdk-java'); | ||
const jsii = mavenFromNpm('jsii-java-runtime'); | ||
|
||
process.stdout.write(` | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- Generated by ${__filename} at ${new Date().toISOString()} --> | ||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<repositories> | ||
<repository> | ||
<id>cdk</id> | ||
<url>file://${cdk.repo}</url> | ||
</repository> | ||
<repository> | ||
<id>jsii</id> | ||
<url>file://${jsii.repo}</url> | ||
</repository> | ||
</repositories> | ||
<groupId>com.amazonaws.cdk</groupId> | ||
<artifactId>cdk-examples-java</artifactId> | ||
<version>${version}</version> | ||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.7.0</version> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-assembly-plugin</artifactId> | ||
<version>3.1.0</version> | ||
<configuration> | ||
<descriptorRefs> | ||
<descriptorRef>jar-with-dependencies</descriptorRef> | ||
</descriptorRefs> | ||
<archive> | ||
<manifest> | ||
<mainClass>com.amazonaws.cdk.examples.HelloJavaApp</mainClass> | ||
</manifest> | ||
</archive> | ||
</configuration> | ||
<executions> | ||
<execution> | ||
<id>make-assembly</id> <!-- this is used for inheritance merges --> | ||
<phase>package</phase> <!-- bind to the packaging phase --> | ||
<goals> | ||
<goal>single</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.amazonaws</groupId> | ||
<artifactId>jsii-runtime</artifactId> | ||
<version>${jsii.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.amazonaws.cdk</groupId> | ||
<artifactId>aws-cdk</artifactId> | ||
<version>${cdk.version}</version> | ||
</dependency> | ||
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-core</artifactId> | ||
<version>2.9.5</version> | ||
</dependency> | ||
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-databind</artifactId> | ||
<version>2.9.5</version> | ||
</dependency> | ||
<!-- https://mvnrepository.com/artifact/junit/junit --> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.12</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> | ||
`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
target | ||
|
||
# generated by pom.xml.t.js | ||
pom.xml | ||
|
||
.idea | ||
.classpath | ||
.project | ||
.settings |
15 changes: 15 additions & 0 deletions
15
...ples/cdk-examples-java/project/src/main/java/com/amazonaws/cdk/examples/HelloJavaApp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.amazonaws.cdk.examples; | ||
|
||
import com.amazonaws.cdk.App; | ||
|
||
import java.util.Arrays; | ||
|
||
public class HelloJavaApp { | ||
public static void main(final String[] args) { | ||
App app = new App(Arrays.asList(args)); | ||
|
||
new HelloJavaStack(app, "hello-cdk"); | ||
|
||
System.out.println(app.run()); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
...es/cdk-examples-java/project/src/main/java/com/amazonaws/cdk/examples/HelloJavaStack.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.amazonaws.cdk.examples; | ||
|
||
import com.amazonaws.cdk.App; | ||
import com.amazonaws.cdk.Stack; | ||
import com.amazonaws.cdk.StackProps; | ||
import com.amazonaws.cdk.Construct; | ||
import com.amazonaws.cdk.ec2.Fleet; | ||
import com.amazonaws.cdk.ec2.FleetProps; | ||
import com.amazonaws.cdk.ec2.InstanceType; | ||
import com.amazonaws.cdk.ec2.VpcNetwork; | ||
import com.amazonaws.cdk.ec2.WindowsImage; | ||
import com.amazonaws.cdk.ec2.WindowsVersion; | ||
import com.amazonaws.cdk.resources.s3.BucketResource; | ||
import com.amazonaws.cdk.resources.s3.BucketResourceProps; | ||
import com.amazonaws.cdk.sns.Topic; | ||
import com.amazonaws.cdk.sqs.Queue; | ||
import com.amazonaws.cdk.sqs.QueueProps; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
/** | ||
* Hello, CDK for Java! | ||
*/ | ||
class HelloJavaStack extends Stack { | ||
public HelloJavaStack(final App parent, final String name) { | ||
super(parent, name); | ||
|
||
VpcNetwork vpc = new VpcNetwork(this, "VPC"); | ||
|
||
MyFleetProps fleetProps = new MyFleetProps(); | ||
fleetProps.vpc = vpc; | ||
|
||
int topicCount = 5; | ||
|
||
SinkQueue sinkQueue = new SinkQueue(this, "MySinkQueue", SinkQueueProps.builder().withRequiredTopicCount(5).build()); | ||
|
||
for (int i = 0; i < topicCount; ++i) { | ||
sinkQueue.subscribe(new Topic(this, "Topic" + (i+1))); | ||
} | ||
|
||
new MyFleet(this, "MyFleet", fleetProps); | ||
} | ||
|
||
static class MyFleetProps { | ||
public VpcNetwork vpc; | ||
} | ||
|
||
static class MyFleet extends Construct { | ||
MyFleet(final Construct parent, final String name, final MyFleetProps props) { | ||
super(parent, name); | ||
|
||
new Fleet(this, "Compute", FleetProps.builder() | ||
.withInstanceType(new InstanceType("t2.micro")) | ||
.withMachineImage(new WindowsImage(WindowsVersion.WindowsServer2016EnglishNanoBase)) | ||
.withVpc(props.vpc) | ||
.build()); | ||
} | ||
|
||
@Override | ||
public List<String> validate() { | ||
System.err.println("Validating MyFleet..."); | ||
return Collections.emptyList(); | ||
} | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
examples/cdk-examples-java/project/src/main/java/com/amazonaws/cdk/examples/SinkQueue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.amazonaws.cdk.examples; | ||
|
||
import com.amazonaws.cdk.Construct; | ||
import com.amazonaws.cdk.sns.Topic; | ||
import com.amazonaws.cdk.sqs.Queue; | ||
import com.amazonaws.cdk.sqs.QueueProps; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* A sink queue is a queue aggregates messages published to any number of SNS topics. | ||
*/ | ||
public class SinkQueue extends Construct { | ||
private final Queue queue; | ||
private final int expectedTopicCount; | ||
|
||
private int actualTopicCount = 0; | ||
|
||
/** | ||
* Defines a SinkQueue. | ||
* | ||
* @param parent Parent construct | ||
* @param name Logical name | ||
* @param props Props | ||
*/ | ||
public SinkQueue(final Construct parent, final String name, SinkQueueProps props) { | ||
super(parent, name); | ||
|
||
// ensure props is non-null | ||
props = props != null ? props : SinkQueueProps.builder().build(); | ||
|
||
// defaults | ||
QueueProps queueProps = props.getQueueProps(); | ||
this.expectedTopicCount = props.getRequiredTopicCount() != null ? props.getRequiredTopicCount().intValue() : 10; | ||
|
||
// WORKAROUND: https://github.com/awslabs/aws-cdk/issues/157 | ||
if (queueProps == null) { | ||
queueProps = QueueProps.builder().build(); | ||
} | ||
|
||
this.queue = new Queue(this, "Resource", queueProps); | ||
} | ||
|
||
/** | ||
* Defines a SinkQueue with default props. | ||
* @param parent Parent construct | ||
* @param name Logical name | ||
*/ | ||
public SinkQueue(final Construct parent, final String name) { | ||
this(parent, name, null); | ||
} | ||
|
||
/** | ||
* Subscribes this queue to receive messages published to the specified topics. | ||
* | ||
* @param topics The topics to subscribe to | ||
*/ | ||
public void subscribe(final Topic... topics) { | ||
for (Topic topic: topics) { | ||
if (actualTopicCount == expectedTopicCount) { | ||
throw new RuntimeException("Cannot add more topics to the sink. Maximum topics is configured to " + this.expectedTopicCount); | ||
} | ||
topic.subscribeQueue(this.queue); | ||
actualTopicCount++; | ||
} | ||
} | ||
|
||
@Override | ||
public List<String> validate() { | ||
if (actualTopicCount < expectedTopicCount) { | ||
return Arrays.asList( | ||
"There are not enough subscribers to the sink. Expecting " + | ||
this.expectedTopicCount + | ||
", actual is " + this.actualTopicCount); | ||
} | ||
|
||
return super.validate(); | ||
} | ||
} |
Oops, something went wrong.