forked from aws-samples/aws-cdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added ECS fargate-load-balanced-service Java example (aws-sampl…
…es#160) * Initial commit of Java fargate-load-balanced-service * Removed unused code and updated ECSFargateLoadBalancedStack * Linked Java version of fargate-load-balanced-service to the README.md * Updated expected CloudFormation for the JUnit test * Removed unused imports * Added some comments and did some code cleanup
- Loading branch information
1 parent
cb82f82
commit 7e0abe8
Showing
9 changed files
with
807 additions
and
0 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
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,14 @@ | ||
.classpath.txt | ||
target | ||
.classpath | ||
.project | ||
.idea | ||
.settings | ||
.vscode | ||
*.iml | ||
cdk.context.json | ||
|
||
# CDK asset staging directory | ||
.cdk.staging | ||
cdk.out | ||
|
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,22 @@ | ||
|
||
Welcome to your CDK Java project! | ||
|
||
It is a Maven-based project, so you can open this directory with any Maven-compatible Java IDE, | ||
and you should be able to build and run tests from your IDE. | ||
|
||
You should explore the contents of this template. It demonstrates a CDK app with two instances of | ||
a stack (`HelloStack`) which also uses a user-defined construct (`HelloConstruct`). | ||
|
||
The `cdk.json` file tells the CDK Toolkit how to execute your app. This example relies on maven | ||
to do that. | ||
|
||
# Useful commands | ||
|
||
* `mvn package` compile and run tests | ||
* `cdk ls` list all stacks in the app | ||
* `cdk synth` emits the synthesized CloudFormation template | ||
* `cdk deploy` deploy this stack to your default AWS account/region | ||
* `cdk diff` compare deployed stack with current state | ||
* `cdk docs` open CDK documentation | ||
|
||
Enjoy! |
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": "mvn -q exec: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,64 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<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> | ||
|
||
<groupId>com.amazonaws.cdk</groupId> | ||
<artifactId>fargate-load-balanced-service</artifactId> | ||
<version>0.1</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.codehaus.mojo</groupId> | ||
<artifactId>exec-maven-plugin</artifactId> | ||
<version>1.6.0</version> | ||
<configuration> | ||
<mainClass>com.amazonaws.cdk.examples.ECSFargateLoadBalancedApp</mainClass> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
<dependencies> | ||
<!-- AWS Cloud Development Kit --> | ||
<dependency> | ||
<groupId>software.amazon.awscdk</groupId> | ||
<artifactId>core</artifactId> | ||
<version>1.16.0.DEVPREVIEW</version> | ||
</dependency> | ||
|
||
<!-- Respective AWS Construct Libraries --> | ||
<dependency> | ||
<groupId>software.amazon.awscdk</groupId> | ||
<artifactId>ecs</artifactId> | ||
<version>1.16.0.DEVPREVIEW</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>software.amazon.awscdk</groupId> | ||
<artifactId>ecs-patterns</artifactId> | ||
<version>1.16.0.DEVPREVIEW</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.12</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
14 changes: 14 additions & 0 deletions
14
...-balanced-service/src/main/java/com/amazonaws/cdk/examples/ECSFargateLoadBalancedApp.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,14 @@ | ||
package com.amazonaws.cdk.examples; | ||
|
||
import software.amazon.awscdk.core.App; | ||
|
||
public class ECSFargateLoadBalancedApp { | ||
public static void main(final String argv[]) { | ||
App app = new App(); | ||
|
||
new ECSFargateLoadBalancedStack(app, "fargate-load-balanced-service"); | ||
|
||
// required until https://github.com/aws/jsii/issues/456 is resolved | ||
app.synth(); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...alanced-service/src/main/java/com/amazonaws/cdk/examples/ECSFargateLoadBalancedStack.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,39 @@ | ||
package com.amazonaws.cdk.examples; | ||
|
||
import software.amazon.awscdk.core.*; | ||
import software.amazon.awscdk.services.ec2.Vpc; | ||
import software.amazon.awscdk.services.ec2.VpcProps; | ||
import software.amazon.awscdk.services.ecs.Cluster; | ||
import software.amazon.awscdk.services.ecs.ClusterProps; | ||
import software.amazon.awscdk.services.ecs.ContainerImage; | ||
import software.amazon.awscdk.services.ecs.patterns.NetworkLoadBalancedFargateService; | ||
import software.amazon.awscdk.services.ecs.patterns.NetworkLoadBalancedFargateServiceProps; | ||
import software.amazon.awscdk.services.ecs.patterns.NetworkLoadBalancedTaskImageOptions; | ||
|
||
public class ECSFargateLoadBalancedStack extends Stack { | ||
|
||
public ECSFargateLoadBalancedStack(final Construct parent, final String id) { | ||
this(parent, id, null); | ||
} | ||
|
||
public ECSFargateLoadBalancedStack(final Construct parent, final String id, final StackProps props) { | ||
super(parent, id, props); | ||
|
||
// Create VPC with a AZ limit of two. | ||
Vpc vpc = new Vpc(this, "MyVpc", VpcProps.builder().maxAzs(2).build()); | ||
|
||
// Create the ECS Service | ||
Cluster cluster = new Cluster(this, "Ec2Cluster", ClusterProps.builder().vpc(vpc).build()); | ||
|
||
// Use the ECS Network Load Balanced Fargate Service construct to create a ECS service | ||
NetworkLoadBalancedFargateService fargateService = new NetworkLoadBalancedFargateService( | ||
this, | ||
"FargateService", | ||
NetworkLoadBalancedFargateServiceProps.builder() | ||
.cluster(cluster) | ||
.taskImageOptions(NetworkLoadBalancedTaskImageOptions.builder() | ||
.image(ContainerImage.fromRegistry("amazon/amazon-ecs-sample")) | ||
.build()) | ||
.build()); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...ced-service/src/test/java/com/amazonaws/cdk/examples/ECSFargateLoadBalancedStackTest.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,28 @@ | ||
package com.amazonaws.cdk.examples; | ||
|
||
import software.amazon.awscdk.core.App; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
|
||
import static junit.framework.TestCase.assertEquals; | ||
|
||
public class ECSFargateLoadBalancedStackTest { | ||
private final static ObjectMapper JSON = | ||
new ObjectMapper().configure(SerializationFeature.INDENT_OUTPUT, true); | ||
|
||
@Test | ||
public void testStack() throws IOException { | ||
App app = new App(); | ||
ECSFargateLoadBalancedStack stack = new ECSFargateLoadBalancedStack(app, "test"); | ||
|
||
// synthesize the stack to a CloudFormation template and compare against | ||
// a checked-in JSON file. | ||
JsonNode actual = JSON.valueToTree(app.synth().getStackArtifact(stack.getArtifactId()).getTemplate()); | ||
JsonNode expected = JSON.readTree(getClass().getResource("expected.cfn.json")); | ||
assertEquals(expected, actual); | ||
} | ||
} |
Oops, something went wrong.