Skip to content

Commit 6935219

Browse files
authored
Merge pull request #27 from n3mawashi/feature/ecs_scheduler
ECS Services Scheduler
2 parents bd93ed6 + a024c34 commit 6935219

File tree

17 files changed

+508
-17
lines changed

17 files changed

+508
-17
lines changed

.github/workflows/CI.yml

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,66 @@ on:
33
push:
44
pull_request:
55
schedule:
6-
- cron: '0 18 * * sun'
6+
- cron: '0 18 * * SUN'
77

88
jobs:
9-
tflint:
9+
lint:
1010
name: Terraform validate ${{ matrix.terraform_version }}
1111
runs-on: ubuntu-latest
1212
strategy:
1313
fail-fast: false
1414
matrix:
1515
terraform_version:
1616
- latest
17-
- 0.14.0
18-
- 0.13.0
17+
- 1.2.9
18+
- 1.1.9
1919
steps:
2020
- uses: actions/checkout@master
21-
- name: Terraform validate
22-
run: tests/sanity/terraform_tests.sh
23-
env:
21+
- uses: hashicorp/setup-terraform@v2
22+
with:
2423
terraform_version: "${{ matrix.terraform_version }}"
2524

25+
- name: Terraform version
26+
id: version
27+
run: terraform version
28+
- name: Terraform fmt
29+
id: fmt
30+
run: terraform fmt -check
31+
continue-on-error: true
32+
- name: Terraform init
33+
id: init
34+
run: terraform init
35+
- name: Terraform Validate
36+
id: validate
37+
run: terraform validate -no-color
38+
2639
pythontest:
2740
name: ${{ matrix.config.toxenv }}
2841
runs-on: ubuntu-latest
2942
strategy:
3043
fail-fast: false
3144
matrix:
3245
config:
33-
- toxenv: py35
34-
python-version: 3.5
35-
- toxenv: py36
36-
python-version: 3.6
3746
- toxenv: py37
3847
python-version: 3.7
3948
- toxenv: py38
4049
python-version: 3.8
50+
- toxenv: py39
51+
python-version: 3.9
52+
- toxenv: py310
53+
python-version: '3.10'
54+
# - toxenv: py311
55+
# python-version: 3.11
4156
- toxenv: flake8
42-
python-version: 3.7
57+
python-version: 3.8
4358
- toxenv: pylint
44-
python-version: 3.7
59+
python-version: 3.8
4560
- toxenv: black
46-
python-version: 3.7
61+
python-version: 3.8
4762
- toxenv: mypy
48-
python-version: 3.7
63+
python-version: 3.8
4964
- toxenv: pytest
50-
python-version: 3.7
65+
python-version: 3.8
5166

5267
steps:
5368
- name: Checkout repository

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.9.13

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ If you are using Terraform 0.11 you can use versions v1.*.
1313

1414
* Aws lambda runtine Python 3.7
1515
* ec2 instances scheduling
16+
* ecs service scheduling
1617
* rds clusters scheduling
1718
* rds instances scheduling
1819
* autoscalings scheduling
@@ -29,6 +30,7 @@ module "stop_ec2_instance" {
2930
schedule_action = "stop"
3031
autoscaling_schedule = "false"
3132
ec2_schedule = "true"
33+
ecs_schedule = "false"
3234
rds_schedule = "false"
3335
cloudwatch_alarm_schedule = "false"
3436
scheduler_tag = {
@@ -44,6 +46,7 @@ module "start_ec2_instance" {
4446
schedule_action = "start"
4547
autoscaling_schedule = "false"
4648
ec2_schedule = "true"
49+
ecs_schedule = "false"
4750
rds_schedule = "false"
4851
cloudwatch_alarm_schedule = "false"
4952
scheduler_tag = {
@@ -72,6 +75,7 @@ module "start_ec2_instance" {
7275
| cloudwatch_schedule_expression | The scheduling expression | string | `"cron(0 22 ? * MON-FRI *)"` | yes |
7376
| autoscaling_schedule | Enable scheduling on autoscaling resources | string | `"false"` | no |
7477
| ec2_schedule | Enable scheduling on ec2 instance resources | string | `"false"` | no |
78+
| ecs_schedule | Enable scheduling on ecs services resources | string | `"false"` | no |
7579
| rds_schedule | Enable scheduling on rds resources | string | `"false"` | no |
7680
| cloudwatch_alarm_schedule | Enable scheduleding on cloudwatch alarm resources | string | `"false"` | no |
7781
| schedule_action | Define schedule action to apply on resources | string | `"stop"` | yes |
@@ -157,6 +161,7 @@ Apache 2 Licensed. See LICENSE for full details.
157161

158162
* [cloudwatch schedule expressions](https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html)
159163
* [Python boto3 ec2](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html)
164+
* [Python boto3 ecs](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ecs.html)
160165
* [Python boto3 rds](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/rds.html)
161166
* [Python boto3 autoscaling](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/autoscaling.html)
162167
* [Terratest](https://github.com/gruntwork-io/terratest)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
resource "aws_cloudwatch_metric_alarm" "service_count" {
2+
alarm_name = "ecs-cluster-hello-service-count"
3+
comparison_operator = "LessThanThreshold"
4+
evaluation_periods = "2"
5+
metric_name = "CPUUtilization"
6+
namespace = "AWS/ECS"
7+
period = "60"
8+
statistic = "SampleCount"
9+
threshold = "2"
10+
alarm_description = "Less than 2 Running Service on cluster"
11+
dimensions = {
12+
ClusterName = aws_ecs_cluster.hello.id
13+
}
14+
15+
tags = {
16+
tostop = "true"
17+
}
18+
}

examples/ecs-scheduler/ecs.tf

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
resource "aws_ecs_cluster" "hello" {
2+
name = "ecs-scheduler-test-cluster"
3+
4+
setting {
5+
name = "containerInsights"
6+
value = "disabled"
7+
}
8+
}
9+
10+
resource "aws_ecs_service" "hello" {
11+
name = "ecs-scheduler-test-service"
12+
cluster = aws_ecs_cluster.hello.id
13+
task_definition = aws_ecs_task_definition.hello.arn
14+
desired_count = 1
15+
launch_type = "FARGATE"
16+
17+
network_configuration {
18+
subnets = [aws_subnet.primary.id]
19+
}
20+
21+
tags = {
22+
tostop = "true",
23+
terratest_tag = var.random_tag
24+
}
25+
lifecycle {
26+
ignore_changes = [
27+
desired_count,
28+
tags
29+
]
30+
}
31+
}
32+
33+
resource "aws_ecs_service" "hello-false" {
34+
name = "ecs-scheduler-test-false-service"
35+
cluster = aws_ecs_cluster.hello.id
36+
task_definition = aws_ecs_task_definition.hello.arn
37+
desired_count = 1
38+
launch_type = "FARGATE"
39+
40+
network_configuration {
41+
subnets = [aws_subnet.primary.id]
42+
}
43+
44+
tags = {
45+
tostop = "false",
46+
terratest_tag = var.random_tag
47+
}
48+
lifecycle {
49+
ignore_changes = [
50+
desired_count,
51+
tags
52+
]
53+
}
54+
}
55+
56+
resource "aws_ecs_task_definition" "hello" {
57+
family = "hello-world-1"
58+
59+
# Refer to https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-cpu-memory-error.html
60+
# for cpu and memory values
61+
cpu = 256
62+
memory = 512
63+
64+
requires_compatibilities = ["FARGATE"]
65+
network_mode = "awsvpc"
66+
67+
# execution_role_arn = aws_iam_role.ecs_service.arn
68+
task_role_arn = aws_iam_role.hello_ecs_task_execution_role.arn
69+
70+
container_definitions = jsonencode([
71+
{
72+
name = "hello-world-rest"
73+
image = "public.ecr.aws/docker/library/busybox:latest"
74+
essential = true
75+
}
76+
])
77+
78+
tags = {
79+
terratest_tag = var.random_tag
80+
}
81+
}

examples/ecs-scheduler/iam.tf

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
resource "aws_iam_role" "hello_ecs_task_execution_role" {
2+
name = "hello-ecsTaskExecutionRole"
3+
4+
assume_role_policy = <<EOF
5+
{
6+
"Version": "2012-10-17",
7+
"Statement": [
8+
{
9+
"Action": "sts:AssumeRole",
10+
"Principal": {
11+
"Service": "ecs-tasks.amazonaws.com"
12+
},
13+
"Effect": "Allow",
14+
"Sid": ""
15+
}
16+
]
17+
}
18+
EOF
19+
}
20+
21+
resource "aws_iam_role_policy_attachment" "ecs-task-execution-role-policy-attachment" {
22+
role = aws_iam_role.hello_ecs_task_execution_role.name
23+
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
24+
}

examples/ecs-scheduler/main.tf

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Terraform ecs with lambda scheduler
2+
3+
# Get aws availability zones
4+
data "aws_availability_zones" "available" {}
5+
6+
### Terraform modules ###
7+
8+
module "ecs-stop-friday" {
9+
source = "../../"
10+
name = "stop-ecs"
11+
cloudwatch_schedule_expression = "cron(0 23 ? * FRI *)"
12+
schedule_action = "stop"
13+
ec2_schedule = "false"
14+
ecs_schedule = "true"
15+
rds_schedule = "false"
16+
autoscaling_schedule = "false"
17+
cloudwatch_alarm_schedule = "true"
18+
19+
scheduler_tag = {
20+
key = "tostop"
21+
value = "true"
22+
}
23+
}
24+
25+
module "ecs-start-monday" {
26+
source = "../../"
27+
name = "start-ecs"
28+
cloudwatch_schedule_expression = "cron(0 07 ? * MON *)"
29+
schedule_action = "start"
30+
ec2_schedule = "false"
31+
ecs_schedule = "true"
32+
autoscaling_schedule = "false"
33+
cloudwatch_alarm_schedule = "true"
34+
35+
scheduler_tag = {
36+
key = "tostop"
37+
value = "true"
38+
}
39+
}

examples/ecs-scheduler/outputs.tf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Terraform ex2-schedule outputs
2+
3+
output "lambda_stop_name" {
4+
value = module.ecs-stop-friday.scheduler_lambda_name
5+
}
6+
7+
output "lambda_stop_arn" {
8+
value = module.ecs-stop-friday.scheduler_lambda_arn
9+
}
10+
11+
output "lambda_start_name" {
12+
value = module.ecs-start-monday.scheduler_lambda_name
13+
}
14+
15+
output "lambda_start_arn" {
16+
value = module.ecs-start-monday.scheduler_lambda_arn
17+
}

examples/ecs-scheduler/provider.tf

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
terraform {
2+
required_providers {
3+
aws = {
4+
source = "hashicorp/aws"
5+
version = "4.20.0"
6+
}
7+
archive = {
8+
source = "hashicorp/archive"
9+
version = "2.2.0"
10+
}
11+
}
12+
required_version = ">= 1.0.11"
13+
}
14+
15+
provider "aws" {
16+
region = "us-west-1"
17+
default_tags {
18+
tags = {
19+
Created_by = "Terraform"
20+
Project = "esc-scheduler-testing-example"
21+
}
22+
}
23+
}
24+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
variable "random_tag" {
2+
description = "aws tag use during integration tests"
3+
default = "terratest_random_tag"
4+
}

0 commit comments

Comments
 (0)