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..ac3d3aa664 --- /dev/null +++ b/java/classic-load-balancer/pom.xml @@ -0,0 +1,97 @@ + + + + 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.0.0,) + + + software.amazon.awscdk + ec2 + [1.0.0,) + + + software.amazon.awscdk + autoscaling + [1.0.0,) + + + software.amazon.awscdk + elasticloadbalancing + [1.0.0,) + + + + 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..64c8f5d7c0 --- /dev/null +++ b/java/classic-load-balancer/src/main/java/software/amazon/awscdk/examples/ClassicLoadBalancerStack.java @@ -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) { + + 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"); + + } +}