in src/AWS.Deploy.Recipes/CdkTemplates/AspNetAppEcsFargate/Generated/Recipe.cs [200:304]
private void ConfigureLoadBalancer(Configuration settings)
{
if (AppVpc == null)
throw new InvalidOperationException($"{nameof(AppVpc)} has not been set. The {nameof(ConfigureVpc)} method should be called before {nameof(ConfigureLoadBalancer)}");
if (EcsCluster == null)
throw new InvalidOperationException($"{nameof(EcsCluster)} has not been set. The {nameof(ConfigureECSClusterAndService)} method should be called before {nameof(ConfigureLoadBalancer)}");
if (AppFargateService == null)
throw new InvalidOperationException($"{nameof(AppFargateService)} has not been set. The {nameof(ConfigureECSClusterAndService)} method should be called before {nameof(ConfigureLoadBalancer)}");
if (settings.LoadBalancer.CreateNew)
{
ServiceLoadBalancer = new ApplicationLoadBalancer(this, nameof(ServiceLoadBalancer), InvokeCustomizeCDKPropsEvent(nameof(ServiceLoadBalancer), this, new ApplicationLoadBalancerProps
{
Vpc = AppVpc,
InternetFacing = settings.LoadBalancer.InternetFacing
}));
LoadBalancerListener = ServiceLoadBalancer.AddListener(nameof(LoadBalancerListener), InvokeCustomizeCDKPropsEvent(nameof(LoadBalancerListener), this, new ApplicationListenerProps
{
Protocol = ApplicationProtocol.HTTP,
Port = 80,
Open = true
}));
ServiceTargetGroup = LoadBalancerListener.AddTargets(nameof(ServiceTargetGroup), InvokeCustomizeCDKPropsEvent(nameof(ServiceTargetGroup), this, new AddApplicationTargetsProps
{
Protocol = ApplicationProtocol.HTTP,
DeregistrationDelay = Duration.Seconds(settings.LoadBalancer.DeregistrationDelayInSeconds)
}));
}
else
{
ServiceLoadBalancer = ApplicationLoadBalancer.FromLookup(this, nameof(ServiceLoadBalancer), InvokeCustomizeCDKPropsEvent(nameof(ServiceLoadBalancer), this, new ApplicationLoadBalancerLookupOptions
{
LoadBalancerArn = settings.LoadBalancer.ExistingLoadBalancerArn
}));
LoadBalancerListener = ApplicationListener.FromLookup(this, nameof(LoadBalancerListener), InvokeCustomizeCDKPropsEvent(nameof(LoadBalancerListener), this, new ApplicationListenerLookupOptions
{
LoadBalancerArn = settings.LoadBalancer.ExistingLoadBalancerArn,
ListenerPort = 80
}));
ServiceTargetGroup = new ApplicationTargetGroup(this, nameof(ServiceTargetGroup), InvokeCustomizeCDKPropsEvent(nameof(ServiceTargetGroup), this, new ApplicationTargetGroupProps
{
Port = 80,
Vpc = EcsCluster.Vpc,
}));
var addApplicationTargetGroupsProps = new AddApplicationTargetGroupsProps
{
TargetGroups = new[] { ServiceTargetGroup }
};
if(settings.LoadBalancer.ListenerConditionType != LoadBalancerConfiguration.ListenerConditionTypeEnum.None)
{
addApplicationTargetGroupsProps.Priority = settings.LoadBalancer.ListenerConditionPriority;
}
if (settings.LoadBalancer.ListenerConditionType == LoadBalancerConfiguration.ListenerConditionTypeEnum.Path)
{
if(settings.LoadBalancer.ListenerConditionPathPattern == null)
{
throw new ArgumentNullException("Listener condition type was set to \"Path\" but no value was set for the \"TargetPathPattern\"");
}
addApplicationTargetGroupsProps.Conditions = new ListenerCondition[]
{
ListenerCondition.PathPatterns(new []{ settings.LoadBalancer.ListenerConditionPathPattern })
};
}
LoadBalancerListener.AddTargetGroups("AddTargetGroup", InvokeCustomizeCDKPropsEvent("AddTargetGroup", this, addApplicationTargetGroupsProps));
}
// Configure health check for ALB Target Group
var healthCheck = new Amazon.CDK.AWS.ElasticLoadBalancingV2.HealthCheck();
if(settings.LoadBalancer.HealthCheckPath != null)
{
var path = settings.LoadBalancer.HealthCheckPath;
if (!path.StartsWith("/"))
path = "/" + path;
healthCheck.Path = path;
}
if(settings.LoadBalancer.HealthCheckInternval.HasValue)
{
healthCheck.Interval = Duration.Seconds(settings.LoadBalancer.HealthCheckInternval.Value);
}
if (settings.LoadBalancer.HealthyThresholdCount.HasValue)
{
healthCheck.HealthyThresholdCount = settings.LoadBalancer.HealthyThresholdCount.Value;
}
if (settings.LoadBalancer.UnhealthyThresholdCount.HasValue)
{
healthCheck.UnhealthyThresholdCount = settings.LoadBalancer.UnhealthyThresholdCount.Value;
}
if (settings.LoadBalancer.HealthCheckTimeout.HasValue)
{
healthCheck.Timeout = Duration.Seconds(settings.LoadBalancer.HealthCheckTimeout.Value);
}
ServiceTargetGroup.ConfigureHealthCheck(healthCheck);
ServiceTargetGroup.AddTarget(AppFargateService);
}