Skip to content

Commit

Permalink
feat(ecs-patterns): Add PlatformVersion option to ScheduledFargateTas…
Browse files Browse the repository at this point in the history
…k props (aws#12676)

Add the platformversion as an extra option to the Fargate Scheduled Task

closes aws#12623

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
richardhughes authored Jan 30, 2021
1 parent 6e2c0e9 commit 3cbf38b
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 3 deletions.
14 changes: 14 additions & 0 deletions packages/@aws-cdk/aws-ecs-patterns/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
```
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
},
};

0 comments on commit 3cbf38b

Please sign in to comment.