aws-cdk documentation - elbv2
aws-cdk documentation - elbv2
I'm currently experimenting with the new aws-cdk, and I am finding that the documentation is quite lacking in examples.
aws-cdk
Would it be possible to provide more examples, especially around:
To be slightly more accurate:
I'm trying to build a NLB+TargetGroup+ASG on an existing VPC, and struggling on many syntax gotchas.
NLB+TargetGroup+ASG
VPC
addUserData()
AutoScalingGroup
1 Answer
1
Here are some examples: For Java , These are what's inside the methods I created and does not include everything, also may not be best practice.
NetworkLoadBalancer networkLoadBalancer = new NetworkLoadBalancer(this, "NLB01", NetworkLoadBalancerProps.builder()
.withInternetFacing(false)
.withLoadBalancerName(new FnSub("$ProjectIDNLB").toString())
.withVpc(vpcNetworkRef)
.build());
AutoScalingGroupResource autoScalingGroupResource = new AutoScalingGroupResource(this, "AutoScalingGroup",
AutoScalingGroupResourceProps.builder()
.withAutoScalingGroupName(new FnSub("$ProjectID-ServerGroup"))
.withLaunchConfigurationName(launchConfigurationResource.getLaunchConfigurationName())
.withMinSize(minSizeParam.getValue().toString())
.withMaxSize(maxSizeParam.getValue().toString())
.withDesiredCapacity(desiredCapacityParam.getValue().toString())
.withTargetGroupArns(Collections.singletonList(targetGroupResource.getTargetGroupArn()))
.withVpcZoneIdentifier(subnets)
.withHealthCheckType("ELB")
.withHealthCheckGracePeriod(1020)
.build());
new LaunchConfigurationResource(this, "LaunchConfiguration",
LaunchConfigurationResourceProps.builder()
.withLaunchConfigurationName(new FnSub("$ProjectID-LaunchConfiguration"))
.withImageId(amiIDParam.getValue().toString())
.withKeyName(keyNameParam.getValue().toString())
.withSecurityGroups(securityGroupList)
.withInstanceType(instanceTypeParam.getValue().toString())
.withAssociatePublicIpAddress(false)
.build());
List<Object> defaultActionsList = new ArrayList<>();
defaultActionsList.add(ListenerResource.ActionProperty.builder()
.withTargetGroupArn(targetGroupResource.getRef().toString())
.withType("forward")
.build());
ListenerResource listenerResource = new ListenerResource(this, "Listener",
ListenerResourceProps.builder()
.withPort(80)
.withProtocol("TCP")
.withLoadBalancerArn(loadBalancerResource.getRef().toString())
.withDefaultActions(defaultActionsList)
.build());
listenerResource.addDependency( IDependable.builder()
.withDependencyElements(loadBalancerResource.getDependencyElements())
.build());
new TargetGroupResource(this, "TargetGroup",
TargetGroupResourceProps.builder()
.withHealthCheckIntervalSeconds(30) //FUTURE Parameter with limit as only [10,30] valid
.withHealthCheckProtocol("TCP")
.withHealthyThresholdCount(2)
.withPort(80)
.withProtocol("TCP")
.withUnhealthyThresholdCount(2)
.withVpcId(fnImportValueVpcId)
.withTargetGroupName("Target-Group")
.withTags(tagsTargetGroup.addTag("Owner", ownerParam.getValue().toString()))
.build());
TagList tagsNLB = new TagList();
LoadBalancerResource loadBalancerResource = new LoadBalancerResource(this, "NLB",
LoadBalancerResourceProps.builder()
.withLoadBalancerName("Network-Load-Balancer")
.withScheme("internal")
.withType("network")
.withTags(tagsNLB.addTag("Owner", ownerParam.getValue().toString()))
.withSubnets(subnets)
.build());
tagsNLB.addTag("Name", "NLB");
Output restAPIoutput =
new Output(this, "LoadBalancerArn", OutputProps.builder()
.withDescription("NLB")
.withExport(fnSubProjectID.toString())
.withValue(loadBalancerResource.getRef())
.build());
*Here is a function to create parameter *
private Parameter createParameter(String name, String type, String defaultTo, String description)
Parameter parameter =
new Parameter( this,name,
ParameterProps.builder()
.withType(type)
.withDefault(defaultTo)
.withDescription(description)
.build());
return parameter;
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Hi Jerome. For ELBv2 we don't have higher level constructs yet, so for now you'll have to use the cloudformation-level resources to write whatever you would write in CFN. As for ASG, you can call
addUserData()on theAutoScalingGroupobject.– rix0rrr
Sep 10 '18 at 7:51