-
Notifications
You must be signed in to change notification settings - Fork 290
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
Research and PoC - Turn off instances in an environment overnight #1091
Comments
One solution that I got working on Sprinkler is by using the third-party terraform module terraform-aws-lambda-scheduler-stop-start as follows: #------------------------------------------------------------------------------
# Schedule stop/start EC2 instances
#------------------------------------------------------------------------------
module "stop_ec2_instance_nights" {
source = "github.com/diodonfrost/terraform-aws-lambda-scheduler-stop-start?ref=3.1.3"
name = "stop_ec2_instance_nights"
cloudwatch_schedule_expression = "cron(0 21 ? * * *)" # Everyday at 21:00 GMT
schedule_action = "stop"
autoscaling_schedule = "false"
ec2_schedule = "true"
rds_schedule = "false"
cloudwatch_alarm_schedule = "false"
scheduler_tag = {
key = "stop_nights"
value = "test" # only for the test environment
}
}
module "start_ec2_instance_mornings" {
source = "github.com/diodonfrost/terraform-aws-lambda-scheduler-stop-start?ref=3.1.3"
name = "start_ec2_instance_mornings"
cloudwatch_schedule_expression = "cron(0 6 ? * * *)" # Everyday at 6:00 GMT
schedule_action = "start"
autoscaling_schedule = "false"
ec2_schedule = "true"
rds_schedule = "false"
cloudwatch_alarm_schedule = "false"
scheduler_tag = {
key = "stop_nights"
value = "test" # only for the test environment
}
}
resource "aws_kms_grant" "stop_start_scheduler" {
key_id = aws_kms_key.ebs.id
grantee_principal = module.start_ec2_instance_mornings.lambda_iam_role_arn
operations = [
"Decrypt",
"DescribeKey",
"CreateGrant"
]
} |
We had a conversation with the team after today's scrum and decided that AWS Instance Scheduler is another alternative which might be the closest to what we will need in the end: https://aws.amazon.com/solutions/implementations/instance-scheduler/. While the terraform module would work, AWS Instance Scheduler allows cross-account and flexible scheduling, which might be beneficial for configuration at platform level. Only downside is that it's a new feature and there doesn't seem to be terraform module for it yet. We need a separate POC to see how this solution would work. |
Having used AWS Instance Scheduler previously, it's supplied as a cloudformation template by default. It could be migrated into terraform with some work, although there are (from memory) some interactions with dynamodb which are configured using an AWS-supplied CLI python package. Again (from memory) I believe those can be conducted either manually, or ported into Terraform as they're just DynamoDB entries ( relevant resources in the AWS provider are probably aws_dynamodb_table / aws_dynamodb_table_entry ). |
Having looked into this the following things stand out:
|
|
At present we have two CFTs deployed and can see CloudWatch logs indicating that we're successfully assuming a role in a remote account and querying instances appropriately. We also see an error message indicating that the schedule we've set up is not matching the schedule name being read despite the two appearing identical. |
Resolved issues we had with schedules not reading. |
Successfully demonstrated. Will raise issues to move this along into production use |
User Story
There is no reason to have non production instances (EC2, RDS, etc) constantly running, outside of working hours they should be shut down.
Value
Save money and increase the sustainability of the platform. This also encourages work life balance if instances are not accessible at night.
Questions / Assumptions
There should be some way of excluding instances eg a tag.
Production instances should not be included.
This would need to be across all the mp accounts, could this be done centrally or would it need to be done on a per account basis?
Possible solutions include systems manager automation documents or a lambda.
We need to ensure instances can be started and stopped on a schedule without human intervention, application teams will also need to ensure this.
https://aws.amazon.com/blogs/mt/systems-manager-automation-documents-manage-instances-cut-costs-off-hours/
Definition of done
Reference
How to write good user stories
The text was updated successfully, but these errors were encountered: