From f41bffbd82c228e54fa805a24dda27f5e779e544 Mon Sep 17 00:00:00 2001 From: EC2 Default User Date: Mon, 25 Nov 2019 20:19:46 +0000 Subject: [PATCH 1/5] Java implementation of classic load balancer --- java/classic-load-balancer/README.md | 32 +++++++ java/classic-load-balancer/cdk.json | 3 + java/classic-load-balancer/pom.xml | 93 +++++++++++++++++++ .../examples/ClassicLoadBalancerApp.java | 13 +++ .../examples/ClassicLoadBalancerStack.java | 57 ++++++++++++ 5 files changed, 198 insertions(+) create mode 100644 java/classic-load-balancer/README.md create mode 100644 java/classic-load-balancer/cdk.json create mode 100644 java/classic-load-balancer/pom.xml create mode 100644 java/classic-load-balancer/src/main/java/software/amazon/awscdk/examples/ClassicLoadBalancerApp.java create mode 100644 java/classic-load-balancer/src/main/java/software/amazon/awscdk/examples/ClassicLoadBalancerStack.java diff --git a/java/classic-load-balancer/README.md b/java/classic-load-balancer/README.md new file mode 100644 index 0000000000..bc6f26c12a --- /dev/null +++ b/java/classic-load-balancer/README.md @@ -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 + + + $ cdk synth + + + $ cdk deploy + + + $ cdk diff + diff --git a/java/classic-load-balancer/cdk.json b/java/classic-load-balancer/cdk.json new file mode 100644 index 0000000000..2213821a71 --- /dev/null +++ b/java/classic-load-balancer/cdk.json @@ -0,0 +1,3 @@ +{ + "app": "mvn exec:java -Dexec.mainClass=software.amazon.awscdk.examples.ClassicLoadBalancerApp" +} diff --git a/java/classic-load-balancer/pom.xml b/java/classic-load-balancer/pom.xml new file mode 100644 index 0000000000..b11cf106fd --- /dev/null +++ b/java/classic-load-balancer/pom.xml @@ -0,0 +1,93 @@ + + + + 4.0.0 + + com.amazonaws.cdk + classic-load-balancer + 1.0.0 + + + UTF-8 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 1.8 + 1.8 + + + + + org.apache.maven.plugins + maven-assembly-plugin + 3.1.0 + + + jar-with-dependencies + + + + com.amazonaws.cdk.examples.ClassicLoadBalancerApp + + + + + + make-assembly + package + + single + + + + + + + + + + + software.amazon.awscdk + core + [1.17.0.DEVPREVIEW, 2) + + + software.amazon.awscdk + ec2 + [1.17.0.DEVPREVIEW, 2) + + + + software.amazon.awscdk + autoscaling + [1.17.0.DEVPREVIEW, 2) + + + + junit + junit + 4.12 + test + + + org.hamcrest + hamcrest-core + + + + + org.hamcrest + hamcrest-library + 1.3 + test + + + + + diff --git a/java/classic-load-balancer/src/main/java/software/amazon/awscdk/examples/ClassicLoadBalancerApp.java b/java/classic-load-balancer/src/main/java/software/amazon/awscdk/examples/ClassicLoadBalancerApp.java new file mode 100644 index 0000000000..3c8d8c4698 --- /dev/null +++ b/java/classic-load-balancer/src/main/java/software/amazon/awscdk/examples/ClassicLoadBalancerApp.java @@ -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"); + + app.synth(); + } +} diff --git a/java/classic-load-balancer/src/main/java/software/amazon/awscdk/examples/ClassicLoadBalancerStack.java b/java/classic-load-balancer/src/main/java/software/amazon/awscdk/examples/ClassicLoadBalancerStack.java new file mode 100644 index 0000000000..299e39beea --- /dev/null +++ b/java/classic-load-balancer/src/main/java/software/amazon/awscdk/examples/ClassicLoadBalancerStack.java @@ -0,0 +1,57 @@ +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.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; + + +/* +import software.amazon.awscdk.services.events.Rule; +import software.amazon.awscdk.services.events.RuleProps; +import software.amazon.awscdk.services.events.Schedule; +import software.amazon.awscdk.services.events.targets.LambdaFunction; +import software.amazon.awscdk.services.lambda.Code; +import software.amazon.awscdk.services.lambda.Runtime; +import software.amazon.awscdk.services.lambda.SingletonFunction; + +import java.util.UUID; +*/ + +/** + * Lambda Cron CDK example for Java! + */ +class ClassicLoadBalancerStack extends Stack { + public ClassicLoadBalancerStack(final Construct parent, final String name) { + super(parent, name); + Vpc vpc = new Vpc(this, "VPC"); + + //AmazonLinuxImage amazonLinuxImage = AmazonLinuxImage.Builder.create() + 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"); + + } +} From 0cc37b3b964cb9137441d8584d6bb7cf14e46524 Mon Sep 17 00:00:00 2001 From: EC2 Default User Date: Mon, 25 Nov 2019 20:26:38 +0000 Subject: [PATCH 2/5] Commented code removed --- .../awscdk/examples/ClassicLoadBalancerStack.java | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/java/classic-load-balancer/src/main/java/software/amazon/awscdk/examples/ClassicLoadBalancerStack.java b/java/classic-load-balancer/src/main/java/software/amazon/awscdk/examples/ClassicLoadBalancerStack.java index 299e39beea..a061d786df 100644 --- a/java/classic-load-balancer/src/main/java/software/amazon/awscdk/examples/ClassicLoadBalancerStack.java +++ b/java/classic-load-balancer/src/main/java/software/amazon/awscdk/examples/ClassicLoadBalancerStack.java @@ -16,18 +16,6 @@ import software.amazon.awscdk.services.elasticloadbalancing.ListenerPort; -/* -import software.amazon.awscdk.services.events.Rule; -import software.amazon.awscdk.services.events.RuleProps; -import software.amazon.awscdk.services.events.Schedule; -import software.amazon.awscdk.services.events.targets.LambdaFunction; -import software.amazon.awscdk.services.lambda.Code; -import software.amazon.awscdk.services.lambda.Runtime; -import software.amazon.awscdk.services.lambda.SingletonFunction; - -import java.util.UUID; -*/ - /** * Lambda Cron CDK example for Java! */ @@ -36,7 +24,6 @@ public ClassicLoadBalancerStack(final Construct parent, final String name) { super(parent, name); Vpc vpc = new Vpc(this, "VPC"); - //AmazonLinuxImage amazonLinuxImage = AmazonLinuxImage.Builder.create() AutoScalingGroup asg = AutoScalingGroup.Builder.create(this,"ASG") .vpc(vpc) .instanceType(InstanceType.of(InstanceClass.BURSTABLE2, InstanceSize.MICRO)) From c53f6fafee01710963ce1f182c5907d2e7acb316 Mon Sep 17 00:00:00 2001 From: djiten75 Date: Mon, 3 Feb 2020 22:37:29 +0000 Subject: [PATCH 3/5] Updated version of cdk in pom.xml Changed the comments in ClassicLoadBalancerStack --- java/classic-load-balancer/pom.xml | 12 ++++++++---- .../awscdk/examples/ClassicLoadBalancerStack.java | 3 ++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/java/classic-load-balancer/pom.xml b/java/classic-load-balancer/pom.xml index b11cf106fd..ac3d3aa664 100644 --- a/java/classic-load-balancer/pom.xml +++ b/java/classic-load-balancer/pom.xml @@ -55,18 +55,22 @@ software.amazon.awscdk core - [1.17.0.DEVPREVIEW, 2) + [1.0.0,) software.amazon.awscdk ec2 - [1.17.0.DEVPREVIEW, 2) + [1.0.0,) - software.amazon.awscdk autoscaling - [1.17.0.DEVPREVIEW, 2) + [1.0.0,) + + + software.amazon.awscdk + elasticloadbalancing + [1.0.0,) diff --git a/java/classic-load-balancer/src/main/java/software/amazon/awscdk/examples/ClassicLoadBalancerStack.java b/java/classic-load-balancer/src/main/java/software/amazon/awscdk/examples/ClassicLoadBalancerStack.java index a061d786df..4ad5588173 100644 --- a/java/classic-load-balancer/src/main/java/software/amazon/awscdk/examples/ClassicLoadBalancerStack.java +++ b/java/classic-load-balancer/src/main/java/software/amazon/awscdk/examples/ClassicLoadBalancerStack.java @@ -3,6 +3,7 @@ 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; @@ -17,7 +18,7 @@ /** - * Lambda Cron CDK example for Java! + * Classic Load balance CDK example for Java! */ class ClassicLoadBalancerStack extends Stack { public ClassicLoadBalancerStack(final Construct parent, final String name) { From db29352aad800b128ec350a1c9293359f227e4cd Mon Sep 17 00:00:00 2001 From: djiten75 Date: Mon, 3 Feb 2020 23:52:07 +0000 Subject: [PATCH 4/5] Added another constructor --- .../amazon/awscdk/examples/ClassicLoadBalancerStack.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/java/classic-load-balancer/src/main/java/software/amazon/awscdk/examples/ClassicLoadBalancerStack.java b/java/classic-load-balancer/src/main/java/software/amazon/awscdk/examples/ClassicLoadBalancerStack.java index 4ad5588173..cfce2b3218 100644 --- a/java/classic-load-balancer/src/main/java/software/amazon/awscdk/examples/ClassicLoadBalancerStack.java +++ b/java/classic-load-balancer/src/main/java/software/amazon/awscdk/examples/ClassicLoadBalancerStack.java @@ -22,7 +22,14 @@ */ class ClassicLoadBalancerStack extends Stack { public ClassicLoadBalancerStack(final Construct parent, final String name) { - super(parent, name); + + super(scope, name, null); + } + + public ClassicLoadBalancerStack(final Construct parent, final String name, final StackProps props) { + + super(scope, name, props); + Vpc vpc = new Vpc(this, "VPC"); AutoScalingGroup asg = AutoScalingGroup.Builder.create(this,"ASG") From df10dc7140ece9bfdcdb2781a826a7ba9d808057 Mon Sep 17 00:00:00 2001 From: djiten75 Date: Mon, 3 Feb 2020 23:54:32 +0000 Subject: [PATCH 5/5] Fixed variable name --- .../amazon/awscdk/examples/ClassicLoadBalancerStack.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/java/classic-load-balancer/src/main/java/software/amazon/awscdk/examples/ClassicLoadBalancerStack.java b/java/classic-load-balancer/src/main/java/software/amazon/awscdk/examples/ClassicLoadBalancerStack.java index cfce2b3218..64c8f5d7c0 100644 --- a/java/classic-load-balancer/src/main/java/software/amazon/awscdk/examples/ClassicLoadBalancerStack.java +++ b/java/classic-load-balancer/src/main/java/software/amazon/awscdk/examples/ClassicLoadBalancerStack.java @@ -23,12 +23,12 @@ class ClassicLoadBalancerStack extends Stack { public ClassicLoadBalancerStack(final Construct parent, final String name) { - super(scope, name, null); + super(parent, name, null); } public ClassicLoadBalancerStack(final Construct parent, final String name, final StackProps props) { - super(scope, name, props); + super(parent, name, props); Vpc vpc = new Vpc(this, "VPC");