Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions java/classic-load-balancer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# CDK Java Example

This is an example of a CDK program written in Java.

## Building

To build this app, run `mvn compile`. This will download the required
dependencies to compile the Java code.

You can use your 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 tell the toolkit to use the `mvn exec:java` command as the
entry point of your application. After changing your Java code, you will be able
to run the CDK toolkit commands as usual (Maven will recompile as needed):

$ 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>
3 changes: 3 additions & 0 deletions java/classic-load-balancer/cdk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"app": "mvn exec:java -Dexec.mainClass=software.amazon.awscdk.examples.ClassicLoadBalancerApp"
}
97 changes: 97 additions & 0 deletions java/classic-load-balancer/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?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>classic-load-balancer</artifactId>
<version>1.0.0</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.8.1</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.ClassicLoadBalancerApp</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>software.amazon.awscdk</groupId>
<artifactId>core</artifactId>
<version>[1.0.0,)</version>
</dependency>
<dependency>
<groupId>software.amazon.awscdk</groupId>
<artifactId>ec2</artifactId>
<version>[1.0.0,)</version>
</dependency>
<dependency>
<groupId>software.amazon.awscdk</groupId>
<artifactId>autoscaling</artifactId>
<version>[1.0.0,)</version>
</dependency>
<dependency>
<groupId>software.amazon.awscdk</groupId>
<artifactId>elasticloadbalancing</artifactId>
<version>[1.0.0,)</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
</dependencies>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package software.amazon.awscdk.examples;

import software.amazon.awscdk.core.App;

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

new ClassicLoadBalancerStack(app, "cdk-classic-load-balancer-example");

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use CamelCase for the Stack id cdk-classic-load-balancer-example, can be shorter MyLBStack or my ClassicLoadBalancerStack

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have checked the other examples where are in master and none of them are camel case and hence I want to be consistent with them. Let me know if we are now drifting from this and if so would the other examples get fixed as well?
Examples are : hello-world, lambda-cron

app.synth();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package software.amazon.awscdk.examples;

import software.amazon.awscdk.core.Construct;
import software.amazon.awscdk.core.Duration;
import software.amazon.awscdk.core.Stack;
import software.amazon.awscdk.core.StackProps;
import software.amazon.awscdk.services.ec2.Vpc;
import software.amazon.awscdk.services.autoscaling.AutoScalingGroup;
import software.amazon.awscdk.services.ec2.AmazonLinuxImage;
import software.amazon.awscdk.services.autoscaling.AutoScalingGroup.Builder;
import software.amazon.awscdk.services.ec2.InstanceType;
import software.amazon.awscdk.services.ec2.InstanceClass;
import software.amazon.awscdk.services.ec2.InstanceSize;
import software.amazon.awscdk.services.elasticloadbalancing.LoadBalancer;
import software.amazon.awscdk.services.elasticloadbalancing.HealthCheck;
import software.amazon.awscdk.services.elasticloadbalancing.LoadBalancerListener;
import software.amazon.awscdk.services.elasticloadbalancing.ListenerPort;


/**
* Classic Load balance CDK example for Java!
*/
class ClassicLoadBalancerStack extends Stack {
public ClassicLoadBalancerStack(final Construct parent, final String name) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a parameterized constructor and call it with null:

    public ClassicLoadBalancerStack(final Construct parent, final String name, final StackProps props) {
        super(scope, name, props);
    }

    public ClassicLoadBalancerStack(final Construct parent, final String name) {
        super(scope, name, null); 
        // code 
     }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use scope instead of parent

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the constructor


super(parent, name, null);
}

public ClassicLoadBalancerStack(final Construct parent, final String name, final StackProps props) {

super(parent, name, props);

Vpc vpc = new Vpc(this, "VPC");

AutoScalingGroup asg = AutoScalingGroup.Builder.create(this,"ASG")
.vpc(vpc)
.instanceType(InstanceType.of(InstanceClass.BURSTABLE2, InstanceSize.MICRO))
.machineImage(new AmazonLinuxImage())
.build();
HealthCheck.Builder healthCheckBuilder = new HealthCheck.Builder();
HealthCheck healthCheck = healthCheckBuilder.port(80).build();
LoadBalancer lb = LoadBalancer.Builder.create(this,"LB")
.vpc(vpc)
.internetFacing(Boolean.TRUE)
.healthCheck(healthCheck)
.build();
lb.addTarget(asg);
ListenerPort listenerPort = lb.addListener(LoadBalancerListener.builder().externalPort(80).build());
listenerPort.getConnections().allowDefaultPortFromAnyIpv4("Open to the world");

}
}