Skip to content

Instantly share code, notes, and snippets.

@whereisaaron
Created August 3, 2022 11:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whereisaaron/fffcb1082180e375ca4fd5a027cf65e8 to your computer and use it in GitHub Desktop.
Save whereisaaron/fffcb1082180e375ca4fd5a027cf65e8 to your computer and use it in GitHub Desktop.
Route two different AWS ECS/Fargate container ports (80 & 443) via two different Network Load Balancer Target Groups with AWS CDKv2
var myService = new FargateService(this, "MyService", new FargateServiceProps {
Cluster = myCluster,
DesiredCount = 2,
AssignPublicIp = true,
TaskDefinition = myTaskDef,
EnableExecuteCommand = true,
});
targetGroupPort80.AddTarget(new [] {
service.LoadBalancerTarget(new LoadBalancerTargetOptions {
ContainerName = myContainer.ContainerName,
ContainerPort = 80,
Protocol = Protocol.TCP
})
});
service.Connections.SecurityGroups[0].AddIngressRule(
Peer.Ipv4(myVpc.VpcCidrBlock),
Port.Tcp(80),
"Allow NLB traffic from VPC CIDR to port 80 Target Group"
);
targetGroupPort443.AddTarget(new [] {
service.LoadBalancerTarget(new LoadBalancerTargetOptions {
ContainerName = myContainer.ContainerName,
ContainerPort = 443,
Protocol = Protocol.TCP
})
});
myService.Connections.SecurityGroups[0].AddIngressRule(
Peer.Ipv4(myVpc.VpcCidrBlock),
Port.Tcp(443),
"Allow NLB traffic from VPC CIDR to port 443 Target Group"
@whereisaaron
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment