Skip to content

Commit 0aa4833

Browse files
djiten75NetaNirNGL321
authored
feat: Java :implementation of classic load balancer sample (#187)
* Java implementation of classic load balancer Co-authored-by: netanir <[email protected]> Co-authored-by: Noah Litov <[email protected]>
1 parent 0612ac0 commit 0aa4833

File tree

5 files changed

+197
-0
lines changed

5 files changed

+197
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# CDK Java Example
2+
3+
This is an example of a CDK program written in Java.
4+
5+
## Building
6+
7+
To build this app, run `mvn compile`. This will download the required
8+
dependencies to compile the Java code.
9+
10+
You can use your IDE to write code and unit tests, but you will need to use the
11+
CDK toolkit if you wish to synthesize/deploy stacks.
12+
13+
## CDK Toolkit
14+
15+
The [`cdk.json`](./cdk.json) file in the root of this repository includes
16+
instructions for the CDK toolkit on how to execute this program.
17+
18+
Specifically, it will tell the toolkit to use the `mvn exec:java` command as the
19+
entry point of your application. After changing your Java code, you will be able
20+
to run the CDK toolkit commands as usual (Maven will recompile as needed):
21+
22+
$ cdk ls
23+
<list all stacks in this program>
24+
25+
$ cdk synth
26+
<cloudformation template>
27+
28+
$ cdk deploy
29+
<deploy stack to your account>
30+
31+
$ cdk diff
32+
<diff against deployed stack>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"app": "mvn exec:java -Dexec.mainClass=software.amazon.awscdk.examples.ClassicLoadBalancerApp"
3+
}

java/classic-load-balancer/pom.xml

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<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">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.amazonaws.cdk</groupId>
7+
<artifactId>classic-load-balancer</artifactId>
8+
<version>1.0.0</version>
9+
10+
<properties>
11+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12+
</properties>
13+
14+
<build>
15+
<plugins>
16+
<plugin>
17+
<groupId>org.apache.maven.plugins</groupId>
18+
<artifactId>maven-compiler-plugin</artifactId>
19+
<version>3.8.1</version>
20+
<configuration>
21+
<source>1.8</source>
22+
<target>1.8</target>
23+
</configuration>
24+
</plugin>
25+
26+
<plugin>
27+
<groupId>org.apache.maven.plugins</groupId>
28+
<artifactId>maven-assembly-plugin</artifactId>
29+
<version>3.1.0</version>
30+
<configuration>
31+
<descriptorRefs>
32+
<descriptorRef>jar-with-dependencies</descriptorRef>
33+
</descriptorRefs>
34+
<archive>
35+
<manifest>
36+
<mainClass>com.amazonaws.cdk.examples.ClassicLoadBalancerApp</mainClass>
37+
</manifest>
38+
</archive>
39+
</configuration>
40+
<executions>
41+
<execution>
42+
<id>make-assembly</id> <!-- this is used for inheritance merges -->
43+
<phase>package</phase> <!-- bind to the packaging phase -->
44+
<goals>
45+
<goal>single</goal>
46+
</goals>
47+
</execution>
48+
</executions>
49+
</plugin>
50+
51+
</plugins>
52+
</build>
53+
54+
<dependencies>
55+
<dependency>
56+
<groupId>software.amazon.awscdk</groupId>
57+
<artifactId>core</artifactId>
58+
<version>[1.0.0,)</version>
59+
</dependency>
60+
<dependency>
61+
<groupId>software.amazon.awscdk</groupId>
62+
<artifactId>ec2</artifactId>
63+
<version>[1.0.0,)</version>
64+
</dependency>
65+
<dependency>
66+
<groupId>software.amazon.awscdk</groupId>
67+
<artifactId>autoscaling</artifactId>
68+
<version>[1.0.0,)</version>
69+
</dependency>
70+
<dependency>
71+
<groupId>software.amazon.awscdk</groupId>
72+
<artifactId>elasticloadbalancing</artifactId>
73+
<version>[1.0.0,)</version>
74+
</dependency>
75+
<!-- https://mvnrepository.com/artifact/junit/junit -->
76+
<dependency>
77+
<groupId>junit</groupId>
78+
<artifactId>junit</artifactId>
79+
<version>4.12</version>
80+
<scope>test</scope>
81+
<exclusions>
82+
<exclusion>
83+
<groupId>org.hamcrest</groupId>
84+
<artifactId>hamcrest-core</artifactId>
85+
</exclusion>
86+
</exclusions>
87+
</dependency>
88+
<dependency>
89+
<groupId>org.hamcrest</groupId>
90+
<artifactId>hamcrest-library</artifactId>
91+
<version>1.3</version>
92+
<scope>test</scope>
93+
</dependency>
94+
</dependencies>
95+
96+
97+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package software.amazon.awscdk.examples;
2+
3+
import software.amazon.awscdk.core.App;
4+
5+
public class ClassicLoadBalancerApp{
6+
public static void main(final String[] args) {
7+
App app = new App();
8+
9+
new ClassicLoadBalancerStack(app, "cdk-classic-load-balancer-example");
10+
11+
app.synth();
12+
}
13+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package software.amazon.awscdk.examples;
2+
3+
import software.amazon.awscdk.core.Construct;
4+
import software.amazon.awscdk.core.Duration;
5+
import software.amazon.awscdk.core.Stack;
6+
import software.amazon.awscdk.core.StackProps;
7+
import software.amazon.awscdk.services.ec2.Vpc;
8+
import software.amazon.awscdk.services.autoscaling.AutoScalingGroup;
9+
import software.amazon.awscdk.services.ec2.AmazonLinuxImage;
10+
import software.amazon.awscdk.services.autoscaling.AutoScalingGroup.Builder;
11+
import software.amazon.awscdk.services.ec2.InstanceType;
12+
import software.amazon.awscdk.services.ec2.InstanceClass;
13+
import software.amazon.awscdk.services.ec2.InstanceSize;
14+
import software.amazon.awscdk.services.elasticloadbalancing.LoadBalancer;
15+
import software.amazon.awscdk.services.elasticloadbalancing.HealthCheck;
16+
import software.amazon.awscdk.services.elasticloadbalancing.LoadBalancerListener;
17+
import software.amazon.awscdk.services.elasticloadbalancing.ListenerPort;
18+
19+
20+
/**
21+
* Classic Load balance CDK example for Java!
22+
*/
23+
class ClassicLoadBalancerStack extends Stack {
24+
public ClassicLoadBalancerStack(final Construct parent, final String name) {
25+
26+
super(parent, name, null);
27+
}
28+
29+
public ClassicLoadBalancerStack(final Construct parent, final String name, final StackProps props) {
30+
31+
super(parent, name, props);
32+
33+
Vpc vpc = new Vpc(this, "VPC");
34+
35+
AutoScalingGroup asg = AutoScalingGroup.Builder.create(this,"ASG")
36+
.vpc(vpc)
37+
.instanceType(InstanceType.of(InstanceClass.BURSTABLE2, InstanceSize.MICRO))
38+
.machineImage(new AmazonLinuxImage())
39+
.build();
40+
HealthCheck.Builder healthCheckBuilder = new HealthCheck.Builder();
41+
HealthCheck healthCheck = healthCheckBuilder.port(80).build();
42+
LoadBalancer lb = LoadBalancer.Builder.create(this,"LB")
43+
.vpc(vpc)
44+
.internetFacing(Boolean.TRUE)
45+
.healthCheck(healthCheck)
46+
.build();
47+
lb.addTarget(asg);
48+
ListenerPort listenerPort = lb.addListener(LoadBalancerListener.builder().externalPort(80).build());
49+
listenerPort.getConnections().allowDefaultPortFromAnyIpv4("Open to the world");
50+
51+
}
52+
}

0 commit comments

Comments
 (0)