-
Notifications
You must be signed in to change notification settings - Fork 15
Issue 5425 - Add new shut down ECS cluster Lambda into the CDK #5504
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
Merged
patchwork01
merged 14 commits into
develop
from
5425-add-new-shut-down-ecs-cluster-lambda-into-the-cdk
Sep 1, 2025
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
0f23635
5425: Initial commit of new class
Rob9786 1fc6218
5425: Commit of logging and update to stack
Rob9786 d99c18c
5425: Add permissions to ECS
Rob9786 aa5c8d1
4525: Added spotbugs suppression from IngestStack
Rob9786 fce7982
5425: Try with no sleep
Rob9786 f2b641f
5425: Changed code to use ShutdownSystemProcesses version
Rob9786 9081aed
4525: Re-instate sleep and maxResults
Rob9786 ce968d1
4525: Tidied up logrefs and added extra logging
Rob9786 12f552b
5425: Removed any form of sleeping or throttling.
Rob9786 74994b8
5425: Re-instated sleep
Rob9786 b9f6743
5425: Add dependency to custom resource
Rob9786 cd711d0
5425: Test without removal policy
Rob9786 bd31bb2
5425: Removed commented out code
Rob9786 14e19ff
5425: Re-saved LambdaHandler
Rob9786 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
java/cdk/src/main/java/sleeper/cdk/util/AutoStopEcsClusterTasks.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| /* | ||
| * Copyright 2022-2025 Crown Copyright | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package sleeper.cdk.util; | ||
|
|
||
| import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
| import software.amazon.awscdk.CustomResource; | ||
| import software.amazon.awscdk.Duration; | ||
| import software.amazon.awscdk.customresources.Provider; | ||
| import software.amazon.awscdk.services.ecs.ICluster; | ||
| import software.amazon.awscdk.services.iam.IRole; | ||
| import software.amazon.awscdk.services.iam.ManagedPolicy; | ||
| import software.amazon.awscdk.services.iam.PolicyStatement; | ||
| import software.amazon.awscdk.services.lambda.IFunction; | ||
| import software.amazon.awscdk.services.logs.ILogGroup; | ||
| import software.constructs.Construct; | ||
|
|
||
| import sleeper.cdk.jars.LambdaCode; | ||
| import sleeper.core.deploy.LambdaHandler; | ||
| import sleeper.core.properties.instance.InstanceProperties; | ||
| import sleeper.core.util.EnvironmentUtils; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
|
|
||
| @SuppressFBWarnings("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE") | ||
| public class AutoStopEcsClusterTasks { | ||
Rob9786 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private AutoStopEcsClusterTasks() { | ||
| } | ||
|
|
||
| public static void autoStopTasksOnEcsCluster( | ||
| Construct scope, InstanceProperties instanceProperties, LambdaCode lambdaCode, | ||
| ICluster cluster, String clusterName, | ||
| ILogGroup logGroup, | ||
| ILogGroup providerLogGroup) { | ||
|
|
||
| String id = cluster.getNode().getId() + "-AutoStop"; | ||
| String functionName = clusterName + "-autostop"; | ||
|
|
||
| IFunction lambda = lambdaCode.buildFunction(scope, LambdaHandler.AUTO_STOP_ECS_CLUSTER_TASKS, id + "Lambda", builder -> builder | ||
Rob9786 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .functionName(functionName) | ||
| .memorySize(2048) | ||
| .environment(EnvironmentUtils.createDefaultEnvironmentNoConfigBucket(instanceProperties)) | ||
| .description("Lambda for auto-stopping ECS tasks") | ||
| .logGroup(logGroup) | ||
| .timeout(Duration.minutes(10))); | ||
|
|
||
| // Grant this function permission to list tasks and stop tasks | ||
| PolicyStatement policyStatement = PolicyStatement.Builder | ||
| .create() | ||
| .resources(List.of("*")) | ||
| .actions(List.of("ecs:ListTasks", "ecs:StopTask", "iam:PassRole")) | ||
| .build(); | ||
| IRole role = Objects.requireNonNull(lambda.getRole()); | ||
| role.addToPrincipalPolicy(policyStatement); | ||
| role.addManagedPolicy(ManagedPolicy.fromAwsManagedPolicyName("service-role/AmazonECSTaskExecutionRolePolicy")); | ||
|
|
||
| Provider propertiesWriterProvider = Provider.Builder.create(scope, id + "Provider") | ||
| .onEventHandler(lambda) | ||
| .logGroup(providerLogGroup) | ||
| .build(); | ||
|
|
||
| CustomResource customResource = CustomResource.Builder.create(scope, id) | ||
| .resourceType("Custom::AutoStopEcsClusterTasks") | ||
| .properties(Map.of("cluster", clusterName)) | ||
| .serviceToken(propertiesWriterProvider.getServiceToken()) | ||
| .build(); | ||
|
|
||
| customResource.getNode().addDependency(cluster); | ||
|
|
||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.