-
Notifications
You must be signed in to change notification settings - Fork 2.3k
feat: Java :implementation of classic load balancer sample #187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f41bffb
0cc37b3
83688c0
5da1ed5
480a857
c53f6fa
db29352
df10dc7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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> |
| 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" | ||
| } |
| 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"); | ||
|
|
||
| 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) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
}
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
|
|
||
| } | ||
| } | ||
There was a problem hiding this comment.
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
MyLBStackor myClassicLoadBalancerStackThere was a problem hiding this comment.
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