From 3cbf38b09a9e66a6c009f833481fb25b8c5fc26c Mon Sep 17 00:00:00 2001 From: Richie Hughes Date: Sat, 30 Jan 2021 00:54:23 +0000 Subject: [PATCH] feat(ecs-patterns): Add PlatformVersion option to ScheduledFargateTask props (#12676) Add the platformversion as an extra option to the Fargate Scheduled Task closes #12623 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- packages/@aws-cdk/aws-ecs-patterns/README.md | 14 +++++ .../lib/base/scheduled-task-base.ts | 11 +++- .../lib/fargate/scheduled-fargate-task.ts | 25 ++++++++- .../fargate/test.scheduled-fargate-task.ts | 54 +++++++++++++++++++ 4 files changed, 101 insertions(+), 3 deletions(-) diff --git a/packages/@aws-cdk/aws-ecs-patterns/README.md b/packages/@aws-cdk/aws-ecs-patterns/README.md index 7a80d93aad346..38915031f4346 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/README.md +++ b/packages/@aws-cdk/aws-ecs-patterns/README.md @@ -427,3 +427,17 @@ const loadBalancedFargateService = new ApplicationLoadBalancedFargateService(sta }, }); ``` + +### Set PlatformVersion for ScheduledFargateTask + +```ts +const scheduledFargateTask = new ScheduledFargateTask(stack, 'ScheduledFargateTask', { + cluster, + scheduledFargateTaskImageOptions: { + image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'), + memoryLimitMiB: 512, + }, + schedule: events.Schedule.expression('rate(1 minute)'), + platformVersion: ecs.FargatePlatformVersion.VERSION1_4, +}); +``` diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/base/scheduled-task-base.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/base/scheduled-task-base.ts index 259e375b1973c..38ac4e30a79af 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/base/scheduled-task-base.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/base/scheduled-task-base.ts @@ -165,11 +165,20 @@ export abstract class ScheduledTaskBase extends CoreConstruct { subnetSelection: this.subnetSelection, }); - this.eventRule.addTarget(eventRuleTarget); + this.addTaskAsTarget(eventRuleTarget); return eventRuleTarget; } + /** + * Adds task as a target of the scheduled event rule. + * + * @param ecsTaskTarget the EcsTask to add to the event rule + */ + protected addTaskAsTarget(ecsTaskTarget: EcsTask) { + this.eventRule.addTarget(ecsTaskTarget); + } + /** * Returns the default cluster. */ diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/scheduled-fargate-task.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/scheduled-fargate-task.ts index 8ad898693b6bd..27b9d8b6ad224 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/scheduled-fargate-task.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/scheduled-fargate-task.ts @@ -1,4 +1,5 @@ -import { FargateTaskDefinition } from '@aws-cdk/aws-ecs'; +import { FargateTaskDefinition, FargatePlatformVersion } from '@aws-cdk/aws-ecs'; +import { EcsTask } from '@aws-cdk/aws-events-targets'; import { Construct } from 'constructs'; import { ScheduledTaskBase, ScheduledTaskBaseProps, ScheduledTaskImageProps } from '../base/scheduled-task-base'; @@ -21,6 +22,17 @@ export interface ScheduledFargateTaskProps extends ScheduledTaskBaseProps { * @default none */ readonly scheduledFargateTaskImageOptions?: ScheduledFargateTaskImageOptions; + + /** + * The platform version on which to run your service. + * + * If one is not specified, the LATEST platform version is used by default. For more information, see + * [AWS Fargate Platform Versions](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/platform_versions.html) + * in the Amazon Elastic Container Service Developer Guide. + * + * @default Latest + */ + readonly platformVersion?: FargatePlatformVersion; } /** @@ -109,6 +121,15 @@ export class ScheduledFargateTask extends ScheduledTaskBase { throw new Error('You must specify one of: taskDefinition or image'); } - this.addTaskDefinitionToEventTarget(this.taskDefinition); + // Use the EcsTask as the target of the EventRule + const eventRuleTarget = new EcsTask( { + cluster: this.cluster, + taskDefinition: this.taskDefinition, + taskCount: this.desiredTaskCount, + subnetSelection: this.subnetSelection, + platformVersion: props.platformVersion, + }); + + this.addTaskAsTarget(eventRuleTarget); } } diff --git a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.scheduled-fargate-task.ts b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.scheduled-fargate-task.ts index 27367249ee3cb..fa62f079862f1 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.scheduled-fargate-task.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.scheduled-fargate-task.ts @@ -294,6 +294,60 @@ export = { ], })); + test.done(); + }, + 'Scheduled Fargate Task - with platformVersion defined'(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + const vpc = new ec2.Vpc(stack, 'Vpc', { maxAzs: 1 }); + const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc }); + + new ScheduledFargateTask(stack, 'ScheduledFargateTask', { + cluster, + scheduledFargateTaskImageOptions: { + image: ecs.ContainerImage.fromRegistry('henk'), + memoryLimitMiB: 512, + }, + schedule: events.Schedule.expression('rate(1 minute)'), + platformVersion: ecs.FargatePlatformVersion.VERSION1_4, + }); + + // THEN + expect(stack).to(haveResource('AWS::Events::Rule', { + Targets: [ + { + Arn: { 'Fn::GetAtt': ['EcsCluster97242B84', 'Arn'] }, + EcsParameters: { + LaunchType: 'FARGATE', + NetworkConfiguration: { + AwsVpcConfiguration: { + AssignPublicIp: 'DISABLED', + SecurityGroups: [ + { + 'Fn::GetAtt': [ + 'ScheduledFargateTaskScheduledTaskDefSecurityGroupE075BC19', + 'GroupId', + ], + }, + ], + Subnets: [ + { + Ref: 'VpcPrivateSubnet1Subnet536B997A', + }, + ], + }, + }, + PlatformVersion: '1.4.0', + TaskCount: 1, + TaskDefinitionArn: { Ref: 'ScheduledFargateTaskScheduledTaskDef521FA675' }, + }, + Id: 'Target0', + Input: '{}', + RoleArn: { 'Fn::GetAtt': ['ScheduledFargateTaskScheduledTaskDefEventsRole6CE19522', 'Arn'] }, + }, + ], + })); + test.done(); }, };