Skip to content
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

Add instance scheduler #88

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions provision/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions provision/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ to skip the prompt.
| <a name="module_get_ami"></a> [get\_ami](#module\_get\_ami) | ./modules/get_ami | n/a |
| <a name="module_get_my_ip"></a> [get\_my\_ip](#module\_get\_my\_ip) | ./modules/get_my_ip | n/a |
| <a name="module_setup_vpc"></a> [setup\_vpc](#module\_setup\_vpc) | terraform-aws-modules/vpc/aws | n/a |
| <a name="module_stop_scheduler"></a> [stop\_scheduler](#module\_stop\_scheduler) | diodonfrost/lambda-scheduler-stop-start/aws | n/a |
| <a name="module_web_server"></a> [web\_server](#module\_web\_server) | ./modules/web-server | n/a |

## Resources
Expand Down Expand Up @@ -192,6 +193,7 @@ to skip the prompt.
| <a name="input_instance_os"></a> [instance\_os](#input\_instance\_os) | OS to use for the instance - will determine the AMI to use | `string` | `"rocky9"` | no |
| <a name="input_instance_private_ips"></a> [instance\_private\_ips](#input\_instance\_private\_ips) | Private IP addresses for each instance | `map(any)` | <pre>{<br> "xnat_cserv": "192.168.56.14",<br> "xnat_web": "192.168.56.10"<br>}</pre> | no |
| <a name="input_root_block_device_size"></a> [root\_block\_device\_size](#input\_root\_block\_device\_size) | Storage space on the root block device (GB) | `number` | `30` | no |
| <a name="input_schedule_expression"></a> [schedule\_expression](#input\_schedule\_expression) | cron expression to determine when the EC2 and RDS instances are scheduled to stop. | `string` | `"cron(0 0 18 * * *)"` | no |
| <a name="input_smtp_private_ip"></a> [smtp\_private\_ip](#input\_smtp\_private\_ip) | Private IP address to use to the SMTP mail server | `string` | `"192.168.56.101"` | no |
| <a name="input_subnet_cidr_blocks"></a> [subnet\_cidr\_blocks](#input\_subnet\_cidr\_blocks) | CIDR block for the VPC and subnets | `map(any)` | <pre>{<br> "private": [<br> "192.168.100.0/24",<br> "192.168.101.0/24"<br> ],<br> "public": [<br> "192.168.56.0/24"<br> ]<br>}</pre> | no |
| <a name="input_vpc_cidr_block"></a> [vpc\_cidr\_block](#input\_vpc\_cidr\_block) | CIDR block for the VPC | `string` | `"192.168.0.0/16"` | no |
Expand Down
17 changes: 17 additions & 0 deletions provision/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ module "web_server" {
ssh_cidr = concat([module.get_my_ip.my_public_cidr], var.extend_ssh_cidr)
http_cidr = concat([module.get_my_ip.my_public_cidr], var.extend_http_cidr)
https_cidr = concat([module.get_my_ip.my_public_cidr], var.extend_https_cidr)
scheduled = true
}

# Create EFS
Expand All @@ -113,6 +114,7 @@ module "database" {
availability_zone = var.availability_zones[0]
subnet_ids = module.setup_vpc.private_subnets
webserver_sg_id = module.web_server.webserver_sg_id
scheduled = true
}

# Appstream
Expand Down Expand Up @@ -177,6 +179,21 @@ resource "local_file" "ansible-hosts" {
file_permission = "0644"
}

# Instance scheduler; automatically stop the EC2 and RDS instances every day at 6pm
module "stop_scheduler" {
source = "diodonfrost/lambda-scheduler-stop-start/aws"
name = "ec2_stop"
cloudwatch_schedule_expression = var.schedule_expression
schedule_action = "stop"
ec2_schedule = "true"
rds_schedule = "true"

scheduler_tag = {
key = "scheduled"
value = "true"
}
}

locals {
# AWS
ssh_key_name = "aws-rsa"
Expand Down
3 changes: 2 additions & 1 deletion provision/modules/database/database.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ resource "aws_db_subnet_group" "xnat-db" {
subnet_ids = var.subnet_ids

tags = {
Name = var.name
Name = var.name
scheduled = var.scheduled
}
}

Expand Down
6 changes: 6 additions & 0 deletions provision/modules/database/vars.tf
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,9 @@ variable "webserver_sg_id" {
type = string
description = "The ID of the security group for the web server. To allow access to the database from the web server only."
}

variable "scheduled" {
type = bool
description = "Whether to schedule the instance to stop and start automatically"
default = false
}
6 changes: 6 additions & 0 deletions provision/modules/web-server/vars.tf
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,9 @@ variable "https_cidr" {
type = list(string)
description = "The CIDR block to grant HTTSP access to the web server"
}

variable "scheduled" {
type = bool
description = "Whether to schedule the instance to stop and start automatically"
default = false
}
3 changes: 2 additions & 1 deletion provision/modules/web-server/web-server.tf
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ resource "aws_instance" "servers" {
}

tags = {
Name = var.names[each.key]
Name = var.names[each.key]
scheduled = var.scheduled
}
}

Expand Down
3 changes: 3 additions & 0 deletions provision/terraform.tfvars_sample
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,6 @@ instance_os = "rocky9"
as2_image_name = "IDEAS-FSL-AmazonLinux2-EFSMount-2023-08-31"
as2_instance_type = "stream.standard.medium"
as2_desired_instance_num = 1

# Scheduler
schedule_expression = "cron(0 0 18 * * *)"
6 changes: 6 additions & 0 deletions provision/vars.tf
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,9 @@ variable "as2_image_name" {
description = "Name of the AppStream image"
default = "IDEAS-FSL-AmazonLinux2-EFSMount-2023-08-30"
}

variable "schedule_expression" {
type = string
description = "cron expression to determine when the EC2 and RDS instances are scheduled to stop."
default = "cron(0 0 18 * * *)" # 6pm evrery day
}