diff --git a/nodegroup/README.md b/nodegroup/README.md index b4cd273..267fab3 100644 --- a/nodegroup/README.md +++ b/nodegroup/README.md @@ -108,6 +108,14 @@ Type: `bool` Default: `true` +### create\_schedule + +Description: Schedule shutdown and startup of ASG instances + +Type: `bool` + +Default: `false` + ### detailed\_monitoring Description: Enable EC2 detailed monitoring @@ -211,6 +219,22 @@ Type: `number` Default: `100` +### scheduled\_shutdown + +Description: Scheduled shutdown of ASG instances in UTC + +Type: `string` + +Default: `"00 09 * * MON-FRI"` + +### scheduled\_startup + +Description: Scheduled startup of ASG instances in UTC + +Type: `string` + +Default: `"00 21 * * SUN-THU"` + ### spot\_allocation\_strategy Description: How to allocate capacity across the Spot pools diff --git a/nodegroup/asg-schedule.tf b/nodegroup/asg-schedule.tf new file mode 100644 index 0000000..913ae9c --- /dev/null +++ b/nodegroup/asg-schedule.tf @@ -0,0 +1,21 @@ +# Stop all instances each weekday at 6pm +resource "aws_autoscaling_schedule" "node_shutdown" { + count = var.create_schedule ? length(var.vpc_subnets) : 0 + scheduled_action_name = "eks-${var.cluster_name}-nodes-shutdown-${var.nodegroup_name}-${count.index}" + min_size = 0 + max_size = 0 + desired_capacity = 0 + recurrence = var.scheduled_shutdown + autoscaling_group_name = aws_autoscaling_group.node[count.index].name +} + +# Startup 1 instance each weekday at 9am +resource "aws_autoscaling_schedule" "node_startup" { + count = var.create_schedule ? length(var.vpc_subnets) : 0 + scheduled_action_name = "eks-${var.cluster_name}-nodes-startup-${var.nodegroup_name}-${count.index}" + min_size = var.asg_min_size + max_size = var.asg_max_size + desired_capacity = var.asg_desired_capacity + recurrence = var.scheduled_startup + autoscaling_group_name = aws_autoscaling_group.node[count.index].name +} \ No newline at end of file diff --git a/nodegroup/variables.tf b/nodegroup/variables.tf index 10e8fb0..1e51f2e 100644 --- a/nodegroup/variables.tf +++ b/nodegroup/variables.tf @@ -165,6 +165,24 @@ variable "proxy_bypass" { default = "169.254.169.254,.eks.amazonaws.com" } +variable "create_schedule" { + type = bool + description = "Schedule shutdown and startup of ASG instances" + default = false +} + +variable "scheduled_shutdown" { + description = "Scheduled shutdown of ASG instances in UTC" + type = string + default = "00 09 * * MON-FRI" # 7pm Mon-Fri AEST +} + +variable "scheduled_startup" { + description = "Scheduled startup of ASG instances in UTC" + type = string + default = "00 21 * * SUN-THU" # 7am Mon-Fri AEST +} + variable "tags" { description = "Tags to apply to created resources" type = map(string)