private void CreateContainerDefinition(EcsTaskDefinitionOptions definitionOptions, TaskDefinition taskDefinition) { foreach (var containerDef in definitionOptions.Containers) { var ecr = StackResources.EcrRepositories.FirstOrDefault(ecr => ecr.Key == containerDef.RepositoryId); if (ecr.Key == null || ecr.Value == null) { throw new ArgumentException("Please add a ECR definition option properly set up on your json configuration. No task definition could not be added."); } var portMapping = new List <PortMapping>(); if (containerDef.TCPPortMapping?.Any() == true) { foreach (var ports in containerDef.TCPPortMapping) { portMapping.Add(new PortMapping { ContainerPort = ports.ContainerPort, HostPort = ports.HostPort, Protocol = Amazon.CDK.AWS.ECS.Protocol.TCP }); } } var containerDefinitionProps = new ContainerDefinitionProps { TaskDefinition = taskDefinition, Image = ContainerImage.FromEcrRepository(ecr.Value, containerDef.ImageTag), MemoryLimitMiB = containerDef.MemoryLimitMiB, Cpu = containerDef.CpuUnits, StartTimeout = Duration.Minutes(containerDef.StartTimeOutMinutes), PortMappings = portMapping.ToArray(), Environment = containerDef.EnvironmentVariables, DnsServers = containerDef.DnsServers?.ToArray() }; var container = AwsCdkHandler.CreateContainerDefinitionByProps(containerDef.Id, containerDefinitionProps); if (definitionOptions.MountPoints?.Any() == true) { var mountPoints = new List <MountPoint>(); foreach (var mountPointOption in definitionOptions.MountPoints) { mountPoints.Add(new MountPoint { SourceVolume = mountPointOption.SourceVolume, ContainerPath = mountPointOption.ContainerPath }); } container.AddMountPoints(mountPoints.ToArray()); } } }
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); }