diff --git a/packages/@aws-cdk/aws-ecs/lib/base/task-definition.ts b/packages/@aws-cdk/aws-ecs/lib/base/task-definition.ts index 7c10a4cbc3653..1f9b01a7a4e29 100644 --- a/packages/@aws-cdk/aws-ecs/lib/base/task-definition.ts +++ b/packages/@aws-cdk/aws-ecs/lib/base/task-definition.ts @@ -141,7 +141,7 @@ export class TaskDefinition extends cdk.Construct { /** * All volumes */ - private readonly volumes: CfnTaskDefinition.VolumeProperty[] = []; + private readonly volumes: Volume[] = []; /** * Execution role for this task definition @@ -345,8 +345,12 @@ export interface Volume { /** * A name for the volume */ - name?: string; - // FIXME add dockerVolumeConfiguration + name: string; + + /** + * Specifies this configuration when using Docker volumes + */ + dockerVolumeConfiguration?: DockerVolumeConfiguration; } /** @@ -359,6 +363,50 @@ export interface Host { sourcePath?: string; } +/** + * A configuration of a Docker volume + */ +export interface DockerVolumeConfiguration { + /** + * If true, the Docker volume is created if it does not already exist + * + * @default false + */ + autoprovision?: boolean; + /** + * The Docker volume driver to use + */ + driver: string; + /** + * A map of Docker driver specific options passed through + * + * @default No options + */ + driverOpts?: string[]; + /** + * Custom metadata to add to your Docker volume + * + * @default No labels + */ + labels?: string[]; + /** + * The scope for the Docker volume which determines it's lifecycle + */ + scope: Scope; +} + +export enum Scope { + /** + * Docker volumes are automatically provisioned when the task starts and destroyed when the task stops + */ + Task = "task", + + /** + * Docker volumes are persist after the task stops + */ + Shared = "shared" +} + /** * A constraint on how instances should be placed */ diff --git a/packages/@aws-cdk/aws-ecs/test/ec2/test.ec2-task-definition.ts b/packages/@aws-cdk/aws-ecs/test/ec2/test.ec2-task-definition.ts index 04042794afbc2..25d30242c7821 100644 --- a/packages/@aws-cdk/aws-ecs/test/ec2/test.ec2-task-definition.ts +++ b/packages/@aws-cdk/aws-ecs/test/ec2/test.ec2-task-definition.ts @@ -237,5 +237,41 @@ export = { test.done(); }, + + "correctly sets dockerVolumeConfiguration"(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + const volume = { + name: "scratch", + dockerVolumeConfiguration: { + driver: "local", + scope: ecs.Scope.Task + } + }; + + const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'Ec2TaskDef', { + volumes: [volume] + }); + + taskDefinition.addContainer("web", { + image: ecs.ContainerImage.fromDockerHub("amazon/amazon-ecs-sample"), + memoryLimitMiB: 512 + }); + + // THEN + expect(stack).to(haveResourceLike("AWS::ECS::TaskDefinition", { + Family: "Ec2TaskDef", + Volumes: [{ + Name: "scratch", + DockerVolumeConfiguration: { + Driver: "local", + Scope: 'task' + } + }] + })); + + test.done(); + }, + } };