in packages/@aws-cdk/aws-ecs/lib/base/task-definition.ts [387:484]
constructor(scope: Construct, id: string, props: TaskDefinitionProps) {
super(scope, id);
this.family = props.family || Names.uniqueId(this);
this.compatibility = props.compatibility;
if (props.volumes) {
props.volumes.forEach(v => this.addVolume(v));
}
this.networkMode = props.networkMode ?? (this.isFargateCompatible ? NetworkMode.AWS_VPC : NetworkMode.BRIDGE);
if (this.isFargateCompatible && this.networkMode !== NetworkMode.AWS_VPC) {
throw new Error(`Fargate tasks can only have AwsVpc network mode, got: ${this.networkMode}`);
}
if (props.proxyConfiguration && this.networkMode !== NetworkMode.AWS_VPC) {
throw new Error(`ProxyConfiguration can only be used with AwsVpc network mode, got: ${this.networkMode}`);
}
if (props.placementConstraints && props.placementConstraints.length > 0 && this.isFargateCompatible) {
throw new Error('Cannot set placement constraints on tasks that run on Fargate');
}
if (this.isFargateCompatible && (!props.cpu || !props.memoryMiB)) {
throw new Error(`Fargate-compatible tasks require both CPU (${props.cpu}) and memory (${props.memoryMiB}) specifications`);
}
if (props.inferenceAccelerators && props.inferenceAccelerators.length > 0 && this.isFargateCompatible) {
throw new Error('Cannot use inference accelerators on tasks that run on Fargate');
}
if (this.isExternalCompatible && this.networkMode !== NetworkMode.BRIDGE) {
throw new Error(`External tasks can only have Bridge network mode, got: ${this.networkMode}`);
}
if (!this.isFargateCompatible && props.runtimePlatform) {
throw new Error('Cannot specify runtimePlatform in non-Fargate compatible tasks');
}
this._executionRole = props.executionRole;
this.taskRole = props.taskRole || new iam.Role(this, 'TaskRole', {
assumedBy: new iam.ServicePrincipal('ecs-tasks.amazonaws.com'),
});
if (props.inferenceAccelerators) {
props.inferenceAccelerators.forEach(ia => this.addInferenceAccelerator(ia));
}
this.ephemeralStorageGiB = props.ephemeralStorageGiB;
// validate the cpu and memory size for the Windows operation system family.
if (props.runtimePlatform?.operatingSystemFamily?._operatingSystemFamily.includes('WINDOWS')) {
// We know that props.cpu and props.memoryMiB are defined because an error would have been thrown previously if they were not.
// But, typescript is not able to figure this out, so using the `!` operator here to let the type-checker know they are defined.
this.checkFargateWindowsBasedTasksSize(props.cpu!, props.memoryMiB!, props.runtimePlatform!);
}
this.runtimePlatform = props.runtimePlatform;
const taskDef = new CfnTaskDefinition(this, 'Resource', {
containerDefinitions: Lazy.any({ produce: () => this.renderContainers() }, { omitEmptyArray: true }),
volumes: Lazy.any({ produce: () => this.renderVolumes() }, { omitEmptyArray: true }),
executionRoleArn: Lazy.string({ produce: () => this.executionRole && this.executionRole.roleArn }),
family: this.family,
taskRoleArn: this.taskRole.roleArn,
requiresCompatibilities: [
...(isEc2Compatible(props.compatibility) ? ['EC2'] : []),
...(isFargateCompatible(props.compatibility) ? ['FARGATE'] : []),
...(isExternalCompatible(props.compatibility) ? ['EXTERNAL'] : []),
],
networkMode: this.renderNetworkMode(this.networkMode),
placementConstraints: Lazy.any({
produce: () =>
!isFargateCompatible(this.compatibility) ? this.placementConstraints : undefined,
}, { omitEmptyArray: true }),
proxyConfiguration: props.proxyConfiguration ? props.proxyConfiguration.bind(this.stack, this) : undefined,
cpu: props.cpu,
memory: props.memoryMiB,
ipcMode: props.ipcMode,
pidMode: props.pidMode,
inferenceAccelerators: Lazy.any({
produce: () =>
!isFargateCompatible(this.compatibility) ? this.renderInferenceAccelerators() : undefined,
}, { omitEmptyArray: true }),
ephemeralStorage: this.ephemeralStorageGiB ? {
sizeInGiB: this.ephemeralStorageGiB,
} : undefined,
runtimePlatform: this.isFargateCompatible && this.runtimePlatform ? {
cpuArchitecture: this.runtimePlatform?.cpuArchitecture?._cpuArchitecture,
operatingSystemFamily: this.runtimePlatform?.operatingSystemFamily?._operatingSystemFamily,
} : undefined,
});
if (props.placementConstraints) {
props.placementConstraints.forEach(pc => this.addPlacementConstraint(pc));
}
this.taskDefinitionArn = taskDef.ref;
}