AWS CDK文档-Elbv2

时间:2018-09-07 02:40:32

标签: aws-cdk

我目前正在尝试使用新的aws-cdk,并且发现示例中确实缺少该文档。

是否有可能提供更多示例,尤其是在以下地方:

  • ELBv2
  • 带有用户数据的ASG / EC2

要准确一点: 我正在尝试在现有NLB+TargetGroup+ASG上构建VPC,并努力解决许多语法难题。

1 个答案:

答案 0 :(得分:1)

以下是一些示例:对于Java来说,这些是我创建的方法内部的内容,并不包含所有内容,也不是最佳实践。

NetworkLoadBalancer networkLoadBalancer = new NetworkLoadBalancer(this, "NLB01", NetworkLoadBalancerProps.builder()
            .withInternetFacing(false)

            .withLoadBalancerName(new FnSub("${ProjectID}NLB").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());

*这里是创建参数的功能*

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;
}