We need to create an ECS cluster with EC2, and use an existing Application Load Balancer.
After many trials and errors I managed to make it work, however it always create an extra default target group on port 80.
This is the code I have (using Java as per requirements):
cluster = Cluster.Builder.create(scope, "my-ecs-cluster")
.vpc(awsNetwork.getVpc())
.capacity(
AddCapacityOptions.builder()
.instanceType(new InstanceType("m5n.xlarge"))
.minCapacity(1)
.maxCapacity(1)
.desiredCapacity(1)
.associatePublicIpAddress(true)
.vpcSubnets(
SubnetSelection.builder()
.subnetType(SubnetType.PUBLIC)
.availabilityZones(Arrays.asList("us-west-2a", "us-west-2c"))
.build()
)
.build()
).build();
taskDefinition = Ec2TaskDefinition.Builder.create(scope, "my-taskDefinition")
.executionRole(Role.fromRoleArn(scope, "my-exec-role", "role-arn..."))
.build();
containerDef = ContainerDefinition.Builder.create(scope, "my-ecs-container")
.essential(true)
.memoryLimitMiB(2048)
.memoryReservationMiB(2048)
.image(ContainerImage.fromEcrRepository(
Repository.fromRepositoryName(scope,
"my-ecr",
"my-image"
),
"latest")
)
.taskDefinition(taskDefinition)
.build();
containerDef.addPortMappings(PortMapping.builder()
.hostPort(0)
.containerPort(443)
.protocol(Protocol.TCP)
.build());
taskDefinition.setDefaultContainer(containerDef);
ApplicationLoadBalancerLookupOptions balancerLookupOptions = ApplicationLoadBalancerLookupOptions.builder()
.loadBalancerArn("arn:aws:my-alb-arn")
.build();
loadBalancer = ApplicationLoadBalancer.fromLookup(scope, "my-alb", balancerLookupOptions);
balancedEc2Service = ApplicationLoadBalancedEc2Service.Builder.create(scope, "my-ec2-service")
.cluster(cluster)
.desiredCount(1)
.taskDefinition(taskDefinition)
.loadBalancer(loadBalancer)
.cpu(1)
.memoryLimitMiB(2048)
.build();
balancedEc2Service.getService().addPlacementStrategies(PlacementStrategy.spreadAcross("attribute:ecs.availability-zone", "instanceId"));
HealthCheck healthCheck = HealthCheck.builder()
.path("/healthCheck")
.protocol(software.amazon.awscdk.services.elasticloadbalancingv2.Protocol.HTTPS)
.port("traffic-port")
.unhealthyThresholdCount(2)
.interval(Duration.seconds(30))
.healthyThresholdCount(5)
.timeout(Duration.seconds(5))
.build();
balancedEc2Service.getTargetGroup().setHealthCheck(healthCheck);
ListenerCertificate listenerCertificate = ListenerCertificate.fromCertificateManager(Certificate.fromCertificateArn(scope, "dis-certificate-1", "arn:aws:acm:us-west-2:245459132561:certificate/d2962594-8a40-4f36-abd4-62a6d91d0250"));
ApplicationTargetGroup targetGroup = ApplicationTargetGroup.Builder.create(scope, "dis-receiver-targetGroup")
.protocol(ApplicationProtocol.HTTPS)
.port(443)
.healthCheck(healthCheck)
.vpc(awsNetwork.getVpc())
.build();
ApplicationListener.Builder.create(scope, "dis-receiver-listener")
.loadBalancer(loadBalancer)
.defaultTargetGroups(Collections.singletonList(targetGroup))
.protocol(ApplicationProtocol.HTTPS)
.port(443)
.certificates(Collections.singletonList(listenerCertificate))
.build();
balancedEc2Service.getService().attachToApplicationTargetGroup(targetGroup);
One of the target groups is the one I created in code.
The other one is created automatically, and it points to port 80:
If instead of using balancedEc2Service.getService().attachToApplicationTargetGroup(targetGroup); I set it directly into the service, like:
balancedEc2Service = ApplicationLoadBalancedEc2Service.Builder.create(scope, "my-ec2-service")
.cluster(cluster)
.desiredCount(1)
.taskDefinition(taskDefinition)
.loadBalancer(loadBalancer)
.cpu(1)
.memoryLimitMiB(2048)
// New code
.listenerPort(443)
.protocol(ApplicationProtocol.HTTPS)
.certificate(myCertificate)
.domainName(myDomainName)
.domainZone(myDomainZone)
//
.build();
It creates only one targetGroup, and it is linked to port 443 on ECS:
However the target group itself is on port 80, and so the application gets timed out:
How can I configure a single target group using HTTPS?



