https://github.com/czantoine/start-stop-ecs-fargate-scheduler-sns
Cost-effective FinOps solution for managing the costs associated with running Fargate tasks. Provided CloudFormation template, you can schedule the execution of your Fargate tasks only during business hours, which significantly reduces the costs of running tasks outside of those hours.
https://github.com/czantoine/start-stop-ecs-fargate-scheduler-sns
aws cloudformation cloudwatch-events ecs-fargate finops lambda
Last synced: 6 months ago
JSON representation
Cost-effective FinOps solution for managing the costs associated with running Fargate tasks. Provided CloudFormation template, you can schedule the execution of your Fargate tasks only during business hours, which significantly reduces the costs of running tasks outside of those hours.
- Host: GitHub
- URL: https://github.com/czantoine/start-stop-ecs-fargate-scheduler-sns
- Owner: czantoine
- License: other
- Created: 2023-02-25T10:24:09.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-09-17T05:27:30.000Z (about 1 year ago)
- Last Synced: 2025-03-23T20:23:12.697Z (7 months ago)
- Topics: aws, cloudformation, cloudwatch-events, ecs-fargate, finops, lambda
- Homepage:
- Size: 56.6 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
Awesome Lists containing this project
README
# Start Stop ECS Fargate Scheduler with SNS

This CloudFormation template deploys a Lambda function that starts and stops Fargate tasks in an Amazon ECS cluster during non-business hours using AWS CloudWatch Events. The function is triggered by two rules, one for starting the tasks in the morning and one for stopping them in the evening. An SNS topic is used to send notifications to an email address specified in the template.
## Key Components
This CloudFormation template deploys the following key components:
- AWS Lambda function: A Node.js 16.x function that starts and stops Fargate tasks in an ECS cluster based on the triggers set by the CloudWatch Events rules. The function is triggered by two CloudWatch Events rules: one for starting the tasks in the morning and one for stopping them in the evening.
- CloudWatch Events rules: Two rules that trigger the Lambda function to start or stop the Fargate tasks in the ECS cluster during non-business hours. The rules use a cron expression to specify the schedule of the triggers.
- Amazon SNS topic: An SNS topic that sends a notification to the specified email address when the Lambda function starts or stops the Fargate tasks in the ECS cluster. The SNS topic is created as part of the CloudFormation stack.
- AWS Identity and Access Management (IAM) role: An IAM role that allows the Lambda function to perform actions on Amazon ECS and Amazon SNS. The role is created as part of the CloudFormation stack.
With these key components, this CloudFormation template provides a simple way to automate the start and stop of Fargate tasks in an ECS cluster during non-business hours.## Parameters
The following parameters are required:
- EmailAddress: the email address to receive the notification
- ClusterRegion: the region where the ECS cluster is located
- ClusterECS: the name of the ECS cluster
- ServiceNameECS: the name of the ECS service
- ServiceECS: the name of the ECS task## Usage
1. Open the AWS CloudFormation console.
1. Create a new stack and upload the `stack.yml` file.
1. Confirm SNS subscription and Enjoy.## Testing the Lambda Function
To test the Lambda function, you can use the following example event:
``` json
{
"status": "start",
"ECS_CLUSTER": "",
"ECS_SERVICE_NAME": "",
"service": ""
}
```You can test the function in the AWS Management Console by navigating to the Lambda service and selecting the `StatStopECSFargateSchedulerFunction`. Then, select the `Test` button and use the example event above.
Replace ``, ``, and `` with the values used in the CloudFormation stack parameters. This event will trigger the Lambda function to start the Fargate tasks in the specified ECS service.
You can use these example events to test the Lambda function in the AWS Management Console or wait for the CloudWatch Events rules to trigger the function during the specified schedule. When the function is triggered, it will start or stop the Fargate tasks in the specified ECS service and send a notification to the email address specified in the `EmailAddress` parameter.
## Limitation
The template assumes that there is only one ECS task running in the specified ECS service.
## Security
See [CONTRIBUTING](CONTRIBUTING.md#security-issue-notifications) for more information.
## License
This repository is licensed under the Apache License 2.0. See the LICENSE file.