Skip to content

Commit

Permalink
feat: added ECS fargate-load-balanced-service Java example (aws-sampl…
Browse files Browse the repository at this point in the history
…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
vinayselvaraj authored and mergify[bot] committed Dec 13, 2019
1 parent cb82f82 commit 7e0abe8
Show file tree
Hide file tree
Showing 9 changed files with 807 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ $ cdk destroy

| Example | Description |
|---------|-------------|
| [fargate-load-balanced-service](https://github.com/aws-samples/aws-cdk-examples/tree/master/java/ecs/fargate-load-balanced-service/) | Starting a container fronted by a load balancer on Fargate |
| [hello-world](https://github.com/aws-samples/aws-cdk-examples/tree/master/java/hello-world/) | A demo application that uses the CDK in Java |
| [lambda-cron](https://github.com/aws-samples/aws-cdk-examples/tree/master/java/lambda-cron/) | Running a Lambda on a schedule |
| [resource-overrides](https://github.com/aws-samples/aws-cdk-examples/tree/master/java/resource-overrides/) | Use of the resource overrides (aka ["escape hatch"](https://docs.aws.amazon.com/cdk/latest/guide/cfn_layer.html)) mechanism. |
Expand Down
14 changes: 14 additions & 0 deletions java/ecs/fargate-load-balanced-service/.gitignore
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

22 changes: 22 additions & 0 deletions java/ecs/fargate-load-balanced-service/README.md
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!
3 changes: 3 additions & 0 deletions java/ecs/fargate-load-balanced-service/cdk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"app": "mvn -q exec:java"
}
64 changes: 64 additions & 0 deletions java/ecs/fargate-load-balanced-service/pom.xml
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>
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();
}
}
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());
}
}
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);
}
}
Loading

0 comments on commit 7e0abe8

Please sign in to comment.