Skip to content

Commit ff96f3f

Browse files
cohalzrix0rrr
authored andcommitted
feat(aws-ecs): add additional configuration to Volume (#1357)
1 parent e291d37 commit ff96f3f

File tree

2 files changed

+87
-3
lines changed

2 files changed

+87
-3
lines changed

packages/@aws-cdk/aws-ecs/lib/base/task-definition.ts

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ export class TaskDefinition extends cdk.Construct {
141141
/**
142142
* All volumes
143143
*/
144-
private readonly volumes: CfnTaskDefinition.VolumeProperty[] = [];
144+
private readonly volumes: Volume[] = [];
145145

146146
/**
147147
* Execution role for this task definition
@@ -345,8 +345,12 @@ export interface Volume {
345345
/**
346346
* A name for the volume
347347
*/
348-
name?: string;
349-
// FIXME add dockerVolumeConfiguration
348+
name: string;
349+
350+
/**
351+
* Specifies this configuration when using Docker volumes
352+
*/
353+
dockerVolumeConfiguration?: DockerVolumeConfiguration;
350354
}
351355

352356
/**
@@ -359,6 +363,50 @@ export interface Host {
359363
sourcePath?: string;
360364
}
361365

366+
/**
367+
* A configuration of a Docker volume
368+
*/
369+
export interface DockerVolumeConfiguration {
370+
/**
371+
* If true, the Docker volume is created if it does not already exist
372+
*
373+
* @default false
374+
*/
375+
autoprovision?: boolean;
376+
/**
377+
* The Docker volume driver to use
378+
*/
379+
driver: string;
380+
/**
381+
* A map of Docker driver specific options passed through
382+
*
383+
* @default No options
384+
*/
385+
driverOpts?: string[];
386+
/**
387+
* Custom metadata to add to your Docker volume
388+
*
389+
* @default No labels
390+
*/
391+
labels?: string[];
392+
/**
393+
* The scope for the Docker volume which determines it's lifecycle
394+
*/
395+
scope: Scope;
396+
}
397+
398+
export enum Scope {
399+
/**
400+
* Docker volumes are automatically provisioned when the task starts and destroyed when the task stops
401+
*/
402+
Task = "task",
403+
404+
/**
405+
* Docker volumes are persist after the task stops
406+
*/
407+
Shared = "shared"
408+
}
409+
362410
/**
363411
* A constraint on how instances should be placed
364412
*/

packages/@aws-cdk/aws-ecs/test/ec2/test.ec2-task-definition.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,5 +237,41 @@ export = {
237237

238238
test.done();
239239
},
240+
241+
"correctly sets dockerVolumeConfiguration"(test: Test) {
242+
// GIVEN
243+
const stack = new cdk.Stack();
244+
const volume = {
245+
name: "scratch",
246+
dockerVolumeConfiguration: {
247+
driver: "local",
248+
scope: ecs.Scope.Task
249+
}
250+
};
251+
252+
const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'Ec2TaskDef', {
253+
volumes: [volume]
254+
});
255+
256+
taskDefinition.addContainer("web", {
257+
image: ecs.ContainerImage.fromDockerHub("amazon/amazon-ecs-sample"),
258+
memoryLimitMiB: 512
259+
});
260+
261+
// THEN
262+
expect(stack).to(haveResourceLike("AWS::ECS::TaskDefinition", {
263+
Family: "Ec2TaskDef",
264+
Volumes: [{
265+
Name: "scratch",
266+
DockerVolumeConfiguration: {
267+
Driver: "local",
268+
Scope: 'task'
269+
}
270+
}]
271+
}));
272+
273+
test.done();
274+
},
275+
240276
}
241277
};

0 commit comments

Comments
 (0)