diff --git a/packages/@aws-cdk/aws-events-targets/lib/ecs-task.ts b/packages/@aws-cdk/aws-events-targets/lib/ecs-task.ts index 3a31758c3a8c0..a4b19981dd0b1 100644 --- a/packages/@aws-cdk/aws-events-targets/lib/ecs-task.ts +++ b/packages/@aws-cdk/aws-events-targets/lib/ecs-task.ts @@ -69,6 +69,17 @@ export interface EcsTaskProps { * @default A new IAM role is created */ readonly role?: iam.IRole; + + /** + * The platform version on which to run your task + * + * Unless you have specific compatibility requirements, you don't need to specify this. + * + * @see https://docs.aws.amazon.com/AmazonECS/latest/developerguide/platform_versions.html + * + * @default - ECS will set the Fargate platform version to 'LATEST' + */ + readonly platformVersion?: ecs.FargatePlatformVersion; } /** @@ -95,6 +106,7 @@ export class EcsTask implements events.IRuleTarget { private readonly taskDefinition: ecs.TaskDefinition; private readonly taskCount: number; private readonly role: iam.IRole; + private readonly platformVersion?: ecs.FargatePlatformVersion; constructor(private readonly props: EcsTaskProps) { if (props.securityGroup !== undefined && props.securityGroups !== undefined) { @@ -104,6 +116,7 @@ export class EcsTask implements events.IRuleTarget { this.cluster = props.cluster; this.taskDefinition = props.taskDefinition; this.taskCount = props.taskCount !== undefined ? props.taskCount : 1; + this.platformVersion = props.platformVersion; if (props.role) { const role = props.role; @@ -151,6 +164,7 @@ export class EcsTask implements events.IRuleTarget { ? { ...baseEcsParameters, launchType: this.taskDefinition.isEc2Compatible ? 'EC2' : 'FARGATE', + platformVersion: this.platformVersion, networkConfiguration: { awsVpcConfiguration: { subnets: this.props.cluster.vpc.selectSubnets(subnetSelection).subnetIds, diff --git a/packages/@aws-cdk/aws-events-targets/test/ecs/event-rule-target.test.ts b/packages/@aws-cdk/aws-events-targets/test/ecs/event-rule-target.test.ts index bcac71ae05d6e..4ac8347d02ec3 100644 --- a/packages/@aws-cdk/aws-events-targets/test/ecs/event-rule-target.test.ts +++ b/packages/@aws-cdk/aws-events-targets/test/ecs/event-rule-target.test.ts @@ -403,3 +403,50 @@ test('uses existing IAM role', () => { ], }); }); + +test('uses the specific fargate platform version', () => { + // GIVEN + const stack = new cdk.Stack(); + const vpc = new ec2.Vpc(stack, 'Vpc', { maxAzs: 1 }); + const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc }); + const platformVersion = ecs.FargatePlatformVersion.VERSION1_4; + + const taskDefinition = new ecs.FargateTaskDefinition(stack, 'TaskDef'); + taskDefinition.addContainer('TheContainer', { + image: ecs.ContainerImage.fromRegistry('henk'), + }); + + const rule = new events.Rule(stack, 'Rule', { + schedule: events.Schedule.expression('rate(1 min)'), + }); + + // WHEN + rule.addTarget(new targets.EcsTask({ + cluster, + taskDefinition, + taskCount: 1, + containerOverrides: [{ + containerName: 'TheContainer', + command: ['echo', events.EventField.fromPath('$.detail.event')], + }], + platformVersion, + })); + + // THEN + expect(stack).toHaveResourceLike('AWS::Events::Rule', { + Targets: [ + { + Arn: { 'Fn::GetAtt': ['EcsCluster97242B84', 'Arn'] }, + EcsParameters: { + LaunchType: 'FARGATE', + TaskCount: 1, + TaskDefinitionArn: { + Ref: 'TaskDef54694570', + }, + PlatformVersion: '1.4.0', + }, + Id: 'Target0', + }, + ], + }); +}); diff --git a/packages/@aws-cdk/aws-events/README.md b/packages/@aws-cdk/aws-events/README.md index cdd45921fff57..3c0782339b665 100644 --- a/packages/@aws-cdk/aws-events/README.md +++ b/packages/@aws-cdk/aws-events/README.md @@ -97,7 +97,11 @@ new Rule(this, 'ScheduleRule', { }); ``` -More details in [ScheduledEvents](https://docs.aws.amazon.com/eventbridge/latest/userguide/scheduled-events.html) documentation page. +If you want to specify Fargate platform version, set `platformVersion` in EcsTask's props like the following example: +```ts +const platformVersion = ecs.FargatePlatformVersion.VERSION1_4; +const ecsTaskTarget = new EcsTask({ cluster, taskDefinition, role, platformVersion }); +``` ## Event Targets