Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-scheduler-targets-alpha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ declare const taskDefinition: ecs.FargateTaskDefinition;

new Schedule(this, 'Schedule', {
schedule: ScheduleExpression.rate(cdk.Duration.minutes(60)),
target: targets.EcsRunTask.onFargate(cluster, {
target: new targets.EcsRunFargateTask(cluster, {
taskDefinition,
}),
});
Expand All @@ -347,7 +347,7 @@ declare const taskDefinition: ecs.Ec2TaskDefinition;

new Schedule(this, 'Schedule', {
schedule: ScheduleExpression.rate(cdk.Duration.minutes(60)),
target: targets.EcsRunTask.onEc2(cluster, {
target: new targets.EcsRunEc2Task(cluster, {
taskDefinition,
}),
});
Expand Down
49 changes: 26 additions & 23 deletions packages/@aws-cdk/aws-scheduler-targets-alpha/lib/ecs-run-task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,20 +155,6 @@ export interface Ec2TaskProps extends EcsRunTaskBaseProps {
* Schedule an ECS Task using AWS EventBridge Scheduler.
*/
export abstract class EcsRunTask extends ScheduleTargetBase implements IScheduleTarget {
/**
* Schedule an ECS Task on Fargate using AWS EventBridge Scheduler.
*/
public static onFargate(cluster: ecs.ICluster, props: FargateTaskProps): IScheduleTarget {
return new FargateTask(cluster, props);
}

/**
* Schedule an ECS Task on EC2 using AWS EventBridge Scheduler.
*/
public static onEc2(cluster: ecs.ICluster, props: Ec2TaskProps): IScheduleTarget {
return new Ec2Task(cluster, props);
}

constructor(
protected readonly cluster: ecs.ICluster,
protected readonly props: EcsRunTaskBaseProps,
Expand Down Expand Up @@ -207,9 +193,11 @@ export abstract class EcsRunTask extends ScheduleTargetBase implements ISchedule
}
}

class FargateTask extends EcsRunTask {
/**
* Schedule an ECS Task on Fargate using AWS EventBridge Scheduler.
*/
export class EcsRunFargateTask extends EcsRunTask {
private readonly subnetSelection?: ec2.SubnetSelection;
private readonly securityGroups?: ec2.ISecurityGroup[];
private readonly assignPublicIp?: boolean;
private readonly platformVersion?: string;
private readonly capacityProviderStrategies?: ecs.CapacityProviderStrategy[];
Expand All @@ -220,7 +208,6 @@ class FargateTask extends EcsRunTask {
) {
super(cluster, props);
this.subnetSelection = props.vpcSubnets;
this.securityGroups = props.securityGroups;
this.assignPublicIp = props.assignPublicIp;
this.platformVersion = props.platformVersion;
this.capacityProviderStrategies = props.capacityProviderStrategies;
Expand Down Expand Up @@ -258,14 +245,21 @@ class FargateTask extends EcsRunTask {
awsvpcConfiguration: {
assignPublicIp,
subnets: this.cluster.vpc.selectSubnets(subnetSelection).subnetIds,
securityGroups: this.securityGroups?.map((sg) => sg.securityGroupId),
securityGroups: (this.props.securityGroups && this.props.securityGroups.length > 0)
?
this.props.securityGroups?.map((sg) => sg.securityGroupId)
: undefined,
},
},
},
};
}
}
class Ec2Task extends EcsRunTask {

/**
* Schedule an ECS Task on EC2 using AWS EventBridge Scheduler.
*/
export class EcsRunEc2Task extends EcsRunTask {
private readonly capacityProviderStrategies?: ecs.CapacityProviderStrategy[];
private readonly placementConstraints?: ecs.PlacementConstraint[];
private readonly placementStrategies?: ecs.PlacementStrategy[];
Expand All @@ -289,12 +283,18 @@ class Ec2Task extends EcsRunTask {
// See https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_RunTask.html#ECS-RunTask-request-launchType
const launchType = this.capacityProviderStrategies ? undefined : ecs.LaunchType.EC2;

const taskDefinitionUsesAwsVpc = this.props.taskDefinition.networkMode === ecs.NetworkMode.AWS_VPC;

// Security groups are only configurable with the "awsvpc" network mode.
// See https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_RunTask.html#ECS-RunTask-request-networkConfiguration
if (this.props.taskDefinition.networkMode !== ecs.NetworkMode.AWS_VPC && (this.props.securityGroups || this.props.vpcSubnets)) {
if (!taskDefinitionUsesAwsVpc && (this.props.securityGroups || this.props.vpcSubnets)) {
throw new ValidationError('Security groups and subnets can only be used with awsvpc network mode', _schedule);
}

const subnetSelection =
taskDefinitionUsesAwsVpc ? this.props.vpcSubnets || { subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS }
: undefined;

const bindBaseTargetConfigParameters = super.bindBaseTargetConfig(_schedule).ecsParameters!;

return {
Expand All @@ -317,11 +317,14 @@ class Ec2Task extends EcsRunTask {
: undefined;
},
}, { omitEmptyArray: true }),
... (this.props.taskDefinition.networkMode === ecs.NetworkMode.AWS_VPC && {
... (taskDefinitionUsesAwsVpc && {
networkConfiguration: {
awsvpcConfiguration: {
subnets: this.cluster.vpc.selectSubnets(this.props.vpcSubnets).subnetIds,
securityGroups: this.props.securityGroups?.map((sg) => sg.securityGroupId),
subnets: this.cluster.vpc.selectSubnets(subnetSelection).subnetIds,
securityGroups: (this.props.securityGroups && this.props.securityGroups.length > 0)
?
this.props.securityGroups.map((sg) => sg.securityGroupId)
: undefined,
},
Copy link
Member

@godwingrs22 godwingrs22 Mar 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

qq: For my understanding, is assignPublicIp feature not supported for EC2 based tasks ? I see we have it supported for Fargate based tasks. For ec2 based tasks, if the network mode is aws_vpc and subnet is a public subnet then users can still enable assignPublicIp right if they want to connect via public ip?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I based the implementation on the CloudFormation docs for AWS::Scheduler::Schedule AwsVpcConfiguration, it says under AssignPublicIp:

You can specify ENABLED only when LaunchType in EcsParameters is set to FARGATE.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the clarification.

},
}),
Expand Down
Loading