import { FargateService , FargateTaskDefinition } from '@aws-cdk/aws-ecs' ; import { ApplicationTargetGroup } from '@aws-cdk/aws-elasticloadbalancingv2' ; import { Construct } from '@aws-cdk/core' ; import { IRole } from "@aws-cdk/aws-iam" ; import { ISecurityGroup } from "@aws-cdk/aws-ec2" ; import { ApplicationMultipleTargetGroupsServiceBase , ApplicationMultipleTargetGroupsServiceBaseProps } from './applicationMultipleTargetGroupsFargateServiceBase' ; /**
 * The properties for the ApplicationMultipleTargetGroupsFargateService service.
 */ export interface ApplicationMultipleTargetGroupsFargateServiceProps extends ApplicationMultipleTargetGroupsServiceBaseProps { securityGroup : ISecurityGroup ; default readonly taskDefinition ? : FargateTaskDefinition ; default readonly cpu ? : number ; default readonly memoryLimitMiB ? : number ; default readonly assignPublicIp ? : boolean ; default readonly executionRole ? : IRole ; default readonly taskRole ? : IRole ; default readonly family ? : string ; } /**
 * A Fargate service running on an ECS cluster fronted by an application load balancer.
 */ export class ApplicationMultipleTargetGroupsFargateService extends ApplicationMultipleTargetGroupsServiceBase { /**
     * Determines whether the service will be assigned a public IP address.
     */ readonly assignPublicIp : boolean ; /**
     * The Fargate service in this construct.
     */ readonly service : FargateService ; /**
     * The Fargate task definition in this construct.
     */ readonly taskDefinition : FargateTaskDefinition ; /**
     * The default target group for the service.
     */ readonly targetGroup : ApplicationTargetGroup ; /**
     * Constructs a new instance of the ApplicationMultipleTargetGroupsFargateService class.
     */ constructor ( scope , id , props ) { super ( scope , id , props ) ; this . assignPublicIp = props . assignPublicIp !== undefined ? props . assignPublicIp : false ; if ( props . taskDefinition && props . taskImageOptions ) { throw new Error ( 'You must specify only one of TaskDefinition or TaskImageOptions.' ) ; } else if ( props . taskDefinition ) { this . taskDefinition = props . taskDefinition ; } else if ( props . taskImageOptions ) { const taskImageOptions = props . taskImageOptions ; this . taskDefinition = new FargateTaskDefinition ( this , 'TaskDef' , { memoryLimitMiB : props . memoryLimitMiB , cpu : props . cpu , executionRole : props . executionRole , taskRole : props . taskRole , family : props . family , } ) ; for ( const taskImageOptionsProps of taskImageOptions ) { const containerName = taskImageOptionsProps . containerName !== undefined ? taskImageOptionsProps . containerName : 'web' ; const container = this . taskDefinition . addContainer ( containerName , { image : taskImageOptionsProps . image , logging : this . logDriver , environment : taskImageOptionsProps . environment , secrets : taskImageOptionsProps . secrets , command : [ "sh" , "-c" , "/bin/chamber exec $CHAMBER_SERVICE_NAME -- ./scripts/run.sh" ] } ) ; if ( taskImageOptionsProps . containerPorts ) { for ( const containerPort of taskImageOptionsProps . containerPorts ) { container . addPortMappings ( { containerPort } ) ; } } } } else { throw new Error ( 'You must specify one of: taskDefinition or image' ) ; } if ( ! this . taskDefinition . defaultContainer ) { throw new Error ( 'At least one essential container must be specified' ) ; } if ( this . taskDefinition . defaultContainer . portMappings . length === 0 ) { this . taskDefinition . defaultContainer . addPortMappings ( { containerPort : 80 } ) ; } this . service = this . createFargateService ( props ) ; if ( props . targetGroups ) { this . addPortMappingForTargets ( this . taskDefinition . defaultContainer , props . targetGroups ) ; this . targetGroup = this . registerECSTargets ( this . service , this . taskDefinition . defaultContainer , props . targetGroups ) ; } } private createFargateService ( props ) { return new FargateService ( this , "Service" , { cluster : this . cluster , desiredCount : this . desiredCount , taskDefinition : this . taskDefinition , assignPublicIp : this . assignPublicIp , serviceName : props . serviceName , healthCheckGracePeriod : props . healthCheckGracePeriod , propagateTags : props . propagateTags , enableECSManagedTags : props . enableECSManagedTags , cloudMapOptions : props . cloudMapOptions , securityGroup : props . securityGroup , } ) ; } ; }