Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ecs-patterns): Add PlatformVersion option to ScheduledFargateTask props #12676

Merged
merged 9 commits into from
Jan 30, 2021
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();
},
};