Created
August 3, 2022 11:35
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ref: aws/aws-cdk#1490
Ref: aws/aws-cdk#3667