import * as ecs from '@aws-cdk/aws-ecs' ; import * as awslogs from '@aws-cdk/aws-logs' ; import * as cdk from '@aws-cdk/core' ; import * as cxapi from '@aws-cdk/cx-api' ; import { Service } from '../service' ; import { ServiceExtension } from './extension-interfaces' ; import { Construct } from '@aws-cdk/core' ; /**
 * Setting for the main application container of a service.
 */ export interface ContainerExtensionProps { /**
     * How much CPU the container requires.
     */ readonly cpu : number ; /**
     * How much memory in megabytes the container requires.
     */ readonly memoryMiB : number ; /**
     * The image to run.
     */ readonly image : ecs . ContainerImage ; /**
     * What port the image listen for traffic on.
     */ readonly trafficPort : number ; default readonly environment ? : { [ key ] : string ; } ; default readonly logGroup ? : awslogs . ILogGroup ; } /**
 * The main container of a service. This is generally the container
 * which runs your application business logic. Other extensions will attach
 * sidecars alongside this main container.
 */ export class Container extends ServiceExtension { /**
     * The port on which the container expects to receive network traffic
     */ public readonly trafficPort : number ; /**
     * The log group into which application container logs should be routed.
     */ public logGroup ? : awslogs . ILogGroup ; /**
     * The settings for the container.
     */ private props : ContainerExtensionProps ; constructor ( props ) { super ( 'service-container' ) ; this . props = props ; this . trafficPort = props . trafficPort ; this . logGroup = props . logGroup ; } public prehook ( service , scope ) { this . parentService = service ; this . scope = scope ; } public modifyTaskDefinitionProps ( props ) { return { ... props , cpu : this . props . cpu . toString ( ) , memoryMiB : this . props . memoryMiB . toString ( ) , } as ecs . TaskDefinitionProps ; } public useTaskDefinition ( taskDefinition ) { let containerProps = { image : this . props . image , cpu : Number ( this . props . cpu ) , memoryLimitMiB : Number ( this . props . memoryMiB ) , environment : this . props . environment , } as ecs . ContainerDefinitionOptions ; this . containerMutatingHooks . forEach ( ( hookProvider ) => { containerProps = hookProvider . mutateContainerDefinition ( containerProps ) ; } ) ; if ( ! containerProps . logging ) { if ( ! this . logGroup && this . parentService . node . tryGetContext ( cxapi . ECS_SERVICE_EXTENSIONS_ENABLE_DEFAULT_LOG_DRIVER ) ) { this . logGroup = new awslogs . LogGroup ( this . scope , `${ this . parentService . id }-logs` , { logGroupName : `${ this . parentService . id }-logs` , removalPolicy : cdk . RemovalPolicy . DESTROY , retention : awslogs . RetentionDays . ONE_MONTH , } ) ; } if ( this . logGroup ) { containerProps = { ... containerProps , logging : new ecs . AwsLogDriver ( { streamPrefix : this . parentService . id , logGroup : this . logGroup , } ) , } ; } } else { if ( this . logGroup ) { throw Error ( `Log configuration already specified. You cannot provide a log group for the application container of service '${ this . parentService . id }' while also adding log configuration separately using service extensions.` ) ; } } this . container = taskDefinition . addContainer ( 'app' , containerProps ) ; this . container . addPortMappings ( { containerPort : this . trafficPort , } ) ; this . container . addUlimits ( { softLimit : 1024000 , hardLimit : 1024000 , name : ecs . UlimitName . NOFILE , } ) ; } public resolveContainerDependencies ( ) { if ( ! this . container ) { throw new Error ( 'The container dependency hook was called before the container was created' ) ; } const firelens = this . parentService . serviceDescription . get ( 'firelens' ) ; if ( firelens && firelens . container ) { this . container . addContainerDependencies ( { container : firelens . container , condition : ecs . ContainerDependencyCondition . START , } ) ; } const appmeshextension = this . parentService . serviceDescription . get ( 'appmesh' ) ; if ( appmeshextension && appmeshextension . container ) { this . container . addContainerDependencies ( { container : appmeshextension . container , condition : ecs . ContainerDependencyCondition . HEALTHY , } ) ; } const cloudwatchextension = this . parentService . serviceDescription . get ( 'cloudwatchAgent' ) ; if ( cloudwatchextension && cloudwatchextension . container ) { this . container . addContainerDependencies ( { container : cloudwatchextension . container , condition : ecs . ContainerDependencyCondition . START , } ) ; } const xrayextension = this . parentService . serviceDescription . get ( 'xray' ) ; if ( xrayextension && xrayextension . container ) { this . container . addContainerDependencies ( { container : xrayextension . container , condition : ecs . ContainerDependencyCondition . HEALTHY , } ) ; } } }