-
Notifications
You must be signed in to change notification settings - Fork 4.3k
feat(scheduler-targets): EcsRunTask scheduler target #33697
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
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #33697 +/- ##
=======================================
Coverage 82.38% 82.38%
=======================================
Files 120 120
Lines 6937 6937
Branches 1170 1170
=======================================
Hits 5715 5715
Misses 1119 1119
Partials 103 103
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
e97c70c to
109b25f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @gracelu0 for this great work. LGTM overall. Left few nits and comments for my understanding.
packages/@aws-cdk/aws-scheduler-targets-alpha/lib/ecs-run-task.ts
Outdated
Show resolved
Hide resolved
packages/@aws-cdk/aws-scheduler-targets-alpha/lib/ecs-run-task.ts
Outdated
Show resolved
Hide resolved
packages/@aws-cdk/aws-scheduler-targets-alpha/lib/ecs-run-task.ts
Outdated
Show resolved
Hide resolved
packages/@aws-cdk/aws-scheduler-targets-alpha/test/ecs-run-task.test.ts
Outdated
Show resolved
Hide resolved
| new scheduler.Schedule(stack, 'Schedule', { | ||
| schedule: scheduler.ScheduleExpression.rate(cdk.Duration.minutes(5)), | ||
| target: targets.EcsRunTask.onFargate(cluster, { | ||
| taskDefinition, | ||
| subnetSelection: { subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS }, | ||
| securityGroups: [securityGroup], | ||
| }), | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we a unit test for both fargate and ec2 task by calling like below also?
new scheduler.Schedule(stack, 'Schedule', {
schedule: scheduler.ScheduleExpression.rate(cdk.Duration.minutes(5)),
target: new targets.FargateTask(...)
});
new scheduler.Schedule(stack, 'Schedule', {
schedule: scheduler.ScheduleExpression.rate(cdk.Duration.minutes(5)),
target: new targets.EC2Task(...)
});
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The API is intentionally designed to only support the static factory methods (EcsRunTask.onFargate() and EcsRunTask.onEc2()). The concrete classes (FargateTask and EC2Task) are implementation details and not part of the public API, so users can't directly instantiate them.
I recognize this isn't consistent with the other scheduler targets though (e.g. new targets.SqsSendMessage()) - so for consistency we could refactor as new targets.EcsRunFargateTask() and new targets.EcsRunEc2Task()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes i think we should expose this as public api as well so users can directly instantiate to be consistent with other scheduler targets. I'm good with the naming you suggested.
947c4c6 to
b0eb93a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @gracelu0 for addressing the earlier comments. Left two comments to clarify on the validation.
| if (this.props.taskDefinition.networkMode !== ecs.NetworkMode.AWS_VPC && (this.props.securityGroups || this.props.vpcSubnets)) { | ||
| throw new ValidationError('Security groups and subnets can only be used with awsvpc network mode', _schedule); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if this check is valid. Based on the CFN doc, it doesn't explicitly state that subnets or security groups should not be provided for other network modes.
I think it says as networkConfiguration is required for task definitions that use the awsvpc network mode. I know AWS_VPC network mode needs subnet and security group. But other network modes such as host or bridge i'm not sure if these subnets or secruity group can be used.
Please correct me if my understanding is wrong.
In that case, may be we should validate to check if the vpcSubnets is provided if aws_vpc network mode is used. since subnet is a required property on conditional basis.
if (this.props.taskDefinition.networkMode === ecs.NetworkMode.AWS_VPC && !this.props.vpcSubnets) {
throw new ValidationError('Subnets must be specified when using awsvpc network mode', _schedule);
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The info seems to be scattered across multiple docs 😅 but here's another doc for AwsvpcConfiguration that says:
Properties
AwsvpcConfiguration
Specifies the Amazon VPC subnets and security groups for the task, and whether a public IP address is to be used.
**This structure is relevant only for ECS tasks that use the awsvpc network mode.**
Based on that, my understanding is that for other network modes like 'host' or 'bridge', subnets and securityGroups should not be specified. I see in the stepfunctions-tasks module for ecs run-task we have some similar validation logic.
In that case, may be we should validate to check if the vpcSubnets is provided if aws_vpc network mode is used. since subnet is a required property on conditional basis.
We could add the validation or default to using the private subnets if user doesn't pass in any subnets and task definition is using AWS_VPC network mode:
const subnetSelection = this.subnetSelection || { subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS };
Setting a default for subnets is the current behaviour for Fargate, I can update EC2 with the same unless you think we should not set any default and instead enforce the user to provide a value.
packages/@aws-cdk/aws-scheduler-targets-alpha/lib/ecs-run-task.ts
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(This review is outdated)
1c9c62c to
08fc170
Compare
✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @gracelu0. LGTM overall. Just one question for my understanding before i approve the PR.
| 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, | ||
| }, |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork). |
|
This pull request has been removed from the queue for the following reason: The pull request can't be updated You should update or rebase your pull request manually. If you want to requeue this pull request, you can post a |
AWS CodeBuild CI Report
Powered by github-codebuild-logs, available on the AWS Serverless Application Repository |
|
Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork). |
|
Comments on closed issues and PRs are hard for our team to see. |
Issue # (if applicable)
Closes #27456
Reason for this change
Currently the module supports all templated targets for EventBridge scheduler except for
EcsRunTask.Description of changes
EcsRunTaskwith subclassesEcsRunFargateTaskandEcsRunEc2Taskdepending on where user wants to schedule their ECS task. Decided on this design since some of the parameters inEcsParametersonly apply one of Fargate or EC2.Describe any new or updated permissions being added
ecs:RunTaskto the schedule execution role for the task definition andiam:passRoleusing existinggrantRun()method (docs)ecs:TagResourceto the schedule execution tole for tasks in the clusterDescription of how you validated changes
Added unit tests and integration tests with assertions.
Checklist
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license