private void CreateEc2Service(EcsServiceOptions service, EcsTaskDefinitionOptions definitionOptions) { var taskDefinition = StackResources.EcsTaskDefinitions?.FirstOrDefault(t => t.Key == service.EcsTaskDefinitionId).Value; if (taskDefinition == null) { throw new ArgumentException("Please add a task definition option properly set up on your json configuration. No task definition could be added."); } var cluster = StackResources.EcsClusters.FirstOrDefault(c => c.Key == service.EcsClusterId).Value; if (cluster == null) { throw new ArgumentException("Please add a cluster definition option properly set up on your json configuration. No cluster could be added."); } List <CapacityProviderStrategy> strategyItems = null; List <AsgCapacityProvider> capacityProviders = null; if (service.CapacityProviderStrategy?.Any() == true) { strategyItems = new List <CapacityProviderStrategy>(); capacityProviders = new List <AsgCapacityProvider>(); foreach (var strategy in service.CapacityProviderStrategy) { var capacityProvider = LocateAsgCapacityProvider(strategy.ProviderId, "Could not find capacity provider for ecs service"); var strategyItem = AwsCdkHandler.CreateCapacityProviderStrategy(capacityProvider, strategy.Weigth, strategy.Base); strategyItems.Add(strategyItem); capacityProviders.Add(capacityProvider); } } var ecsService = AwsCdkHandler.AddElasticContainerEc2Service(service.Id, service.ServiceName, cluster, taskDefinition, service.HealthCheckGracePeriod, strategyItems, service.DesiredCount, service.UseDistinctInstances, service.PlacementStrategy); AwsCdkHandler.AddEc2ServiceECSDependencies(ecsService, capacityProviders); CreateContainerDefinition(definitionOptions, taskDefinition); if (service.TargetGroups?.Any() == true) { foreach (var targetGroupConfig in service.TargetGroups) { switch (targetGroupConfig.LoadBalancerType) { case "Network": var targetGroup = LocateNetworkTargetGroup(targetGroupConfig.NetworkTargetGroupId, $"The NetworkTargetGroup {targetGroupConfig.NetworkTargetGroupId} could not be found for the service {service.Id}."); AwsCdkHandler.AddEc2ServiceToNetworkTargetGroup(ecsService, targetGroup, targetGroupConfig.ContainerName, targetGroupConfig.Port); break; case "Application": throw new NotImplementedException(); case "Gateway": throw new NotImplementedException(); default: throw new ArgumentException("Load balancer type in service definition not recognized"); } } } StackResources.EcsServices.Add(service.Id, ecsService); }