Skip to content

Commit 3cbf38b

Browse files
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*
1 parent 6e2c0e9 commit 3cbf38b

File tree

4 files changed

+101
-3
lines changed

4 files changed

+101
-3
lines changed

packages/@aws-cdk/aws-ecs-patterns/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,3 +427,17 @@ const loadBalancedFargateService = new ApplicationLoadBalancedFargateService(sta
427427
},
428428
});
429429
```
430+
431+
### Set PlatformVersion for ScheduledFargateTask
432+
433+
```ts
434+
const scheduledFargateTask = new ScheduledFargateTask(stack, 'ScheduledFargateTask', {
435+
cluster,
436+
scheduledFargateTaskImageOptions: {
437+
image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
438+
memoryLimitMiB: 512,
439+
},
440+
schedule: events.Schedule.expression('rate(1 minute)'),
441+
platformVersion: ecs.FargatePlatformVersion.VERSION1_4,
442+
});
443+
```

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,20 @@ export abstract class ScheduledTaskBase extends CoreConstruct {
165165
subnetSelection: this.subnetSelection,
166166
});
167167

168-
this.eventRule.addTarget(eventRuleTarget);
168+
this.addTaskAsTarget(eventRuleTarget);
169169

170170
return eventRuleTarget;
171171
}
172172

173+
/**
174+
* Adds task as a target of the scheduled event rule.
175+
*
176+
* @param ecsTaskTarget the EcsTask to add to the event rule
177+
*/
178+
protected addTaskAsTarget(ecsTaskTarget: EcsTask) {
179+
this.eventRule.addTarget(ecsTaskTarget);
180+
}
181+
173182
/**
174183
* Returns the default cluster.
175184
*/

packages/@aws-cdk/aws-ecs-patterns/lib/fargate/scheduled-fargate-task.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { FargateTaskDefinition } from '@aws-cdk/aws-ecs';
1+
import { FargateTaskDefinition, FargatePlatformVersion } from '@aws-cdk/aws-ecs';
2+
import { EcsTask } from '@aws-cdk/aws-events-targets';
23
import { Construct } from 'constructs';
34
import { ScheduledTaskBase, ScheduledTaskBaseProps, ScheduledTaskImageProps } from '../base/scheduled-task-base';
45

@@ -21,6 +22,17 @@ export interface ScheduledFargateTaskProps extends ScheduledTaskBaseProps {
2122
* @default none
2223
*/
2324
readonly scheduledFargateTaskImageOptions?: ScheduledFargateTaskImageOptions;
25+
26+
/**
27+
* The platform version on which to run your service.
28+
*
29+
* If one is not specified, the LATEST platform version is used by default. For more information, see
30+
* [AWS Fargate Platform Versions](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/platform_versions.html)
31+
* in the Amazon Elastic Container Service Developer Guide.
32+
*
33+
* @default Latest
34+
*/
35+
readonly platformVersion?: FargatePlatformVersion;
2436
}
2537

2638
/**
@@ -109,6 +121,15 @@ export class ScheduledFargateTask extends ScheduledTaskBase {
109121
throw new Error('You must specify one of: taskDefinition or image');
110122
}
111123

112-
this.addTaskDefinitionToEventTarget(this.taskDefinition);
124+
// Use the EcsTask as the target of the EventRule
125+
const eventRuleTarget = new EcsTask( {
126+
cluster: this.cluster,
127+
taskDefinition: this.taskDefinition,
128+
taskCount: this.desiredTaskCount,
129+
subnetSelection: this.subnetSelection,
130+
platformVersion: props.platformVersion,
131+
});
132+
133+
this.addTaskAsTarget(eventRuleTarget);
113134
}
114135
}

packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.scheduled-fargate-task.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,60 @@ export = {
294294
],
295295
}));
296296

297+
test.done();
298+
},
299+
'Scheduled Fargate Task - with platformVersion defined'(test: Test) {
300+
// GIVEN
301+
const stack = new cdk.Stack();
302+
const vpc = new ec2.Vpc(stack, 'Vpc', { maxAzs: 1 });
303+
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
304+
305+
new ScheduledFargateTask(stack, 'ScheduledFargateTask', {
306+
cluster,
307+
scheduledFargateTaskImageOptions: {
308+
image: ecs.ContainerImage.fromRegistry('henk'),
309+
memoryLimitMiB: 512,
310+
},
311+
schedule: events.Schedule.expression('rate(1 minute)'),
312+
platformVersion: ecs.FargatePlatformVersion.VERSION1_4,
313+
});
314+
315+
// THEN
316+
expect(stack).to(haveResource('AWS::Events::Rule', {
317+
Targets: [
318+
{
319+
Arn: { 'Fn::GetAtt': ['EcsCluster97242B84', 'Arn'] },
320+
EcsParameters: {
321+
LaunchType: 'FARGATE',
322+
NetworkConfiguration: {
323+
AwsVpcConfiguration: {
324+
AssignPublicIp: 'DISABLED',
325+
SecurityGroups: [
326+
{
327+
'Fn::GetAtt': [
328+
'ScheduledFargateTaskScheduledTaskDefSecurityGroupE075BC19',
329+
'GroupId',
330+
],
331+
},
332+
],
333+
Subnets: [
334+
{
335+
Ref: 'VpcPrivateSubnet1Subnet536B997A',
336+
},
337+
],
338+
},
339+
},
340+
PlatformVersion: '1.4.0',
341+
TaskCount: 1,
342+
TaskDefinitionArn: { Ref: 'ScheduledFargateTaskScheduledTaskDef521FA675' },
343+
},
344+
Id: 'Target0',
345+
Input: '{}',
346+
RoleArn: { 'Fn::GetAtt': ['ScheduledFargateTaskScheduledTaskDefEventsRole6CE19522', 'Arn'] },
347+
},
348+
],
349+
}));
350+
297351
test.done();
298352
},
299353
};

0 commit comments

Comments
 (0)