-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from n3mawashi/feature/ecs_scheduler
ECS Services Scheduler
- Loading branch information
Showing
17 changed files
with
508 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
3.9.13 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
resource "aws_cloudwatch_metric_alarm" "service_count" { | ||
alarm_name = "ecs-cluster-hello-service-count" | ||
comparison_operator = "LessThanThreshold" | ||
evaluation_periods = "2" | ||
metric_name = "CPUUtilization" | ||
namespace = "AWS/ECS" | ||
period = "60" | ||
statistic = "SampleCount" | ||
threshold = "2" | ||
alarm_description = "Less than 2 Running Service on cluster" | ||
dimensions = { | ||
ClusterName = aws_ecs_cluster.hello.id | ||
} | ||
|
||
tags = { | ||
tostop = "true" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
resource "aws_ecs_cluster" "hello" { | ||
name = "ecs-scheduler-test-cluster" | ||
|
||
setting { | ||
name = "containerInsights" | ||
value = "disabled" | ||
} | ||
} | ||
|
||
resource "aws_ecs_service" "hello" { | ||
name = "ecs-scheduler-test-service" | ||
cluster = aws_ecs_cluster.hello.id | ||
task_definition = aws_ecs_task_definition.hello.arn | ||
desired_count = 1 | ||
launch_type = "FARGATE" | ||
|
||
network_configuration { | ||
subnets = [aws_subnet.primary.id] | ||
} | ||
|
||
tags = { | ||
tostop = "true", | ||
terratest_tag = var.random_tag | ||
} | ||
lifecycle { | ||
ignore_changes = [ | ||
desired_count, | ||
tags | ||
] | ||
} | ||
} | ||
|
||
resource "aws_ecs_service" "hello-false" { | ||
name = "ecs-scheduler-test-false-service" | ||
cluster = aws_ecs_cluster.hello.id | ||
task_definition = aws_ecs_task_definition.hello.arn | ||
desired_count = 1 | ||
launch_type = "FARGATE" | ||
|
||
network_configuration { | ||
subnets = [aws_subnet.primary.id] | ||
} | ||
|
||
tags = { | ||
tostop = "false", | ||
terratest_tag = var.random_tag | ||
} | ||
lifecycle { | ||
ignore_changes = [ | ||
desired_count, | ||
tags | ||
] | ||
} | ||
} | ||
|
||
resource "aws_ecs_task_definition" "hello" { | ||
family = "hello-world-1" | ||
|
||
# Refer to https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-cpu-memory-error.html | ||
# for cpu and memory values | ||
cpu = 256 | ||
memory = 512 | ||
|
||
requires_compatibilities = ["FARGATE"] | ||
network_mode = "awsvpc" | ||
|
||
# execution_role_arn = aws_iam_role.ecs_service.arn | ||
task_role_arn = aws_iam_role.hello_ecs_task_execution_role.arn | ||
|
||
container_definitions = jsonencode([ | ||
{ | ||
name = "hello-world-rest" | ||
image = "public.ecr.aws/docker/library/busybox:latest" | ||
essential = true | ||
} | ||
]) | ||
|
||
tags = { | ||
terratest_tag = var.random_tag | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
resource "aws_iam_role" "hello_ecs_task_execution_role" { | ||
name = "hello-ecsTaskExecutionRole" | ||
|
||
assume_role_policy = <<EOF | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Action": "sts:AssumeRole", | ||
"Principal": { | ||
"Service": "ecs-tasks.amazonaws.com" | ||
}, | ||
"Effect": "Allow", | ||
"Sid": "" | ||
} | ||
] | ||
} | ||
EOF | ||
} | ||
|
||
resource "aws_iam_role_policy_attachment" "ecs-task-execution-role-policy-attachment" { | ||
role = aws_iam_role.hello_ecs_task_execution_role.name | ||
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Terraform ecs with lambda scheduler | ||
|
||
# Get aws availability zones | ||
data "aws_availability_zones" "available" {} | ||
|
||
### Terraform modules ### | ||
|
||
module "ecs-stop-friday" { | ||
source = "../../" | ||
name = "stop-ecs" | ||
cloudwatch_schedule_expression = "cron(0 23 ? * FRI *)" | ||
schedule_action = "stop" | ||
ec2_schedule = "false" | ||
ecs_schedule = "true" | ||
rds_schedule = "false" | ||
autoscaling_schedule = "false" | ||
cloudwatch_alarm_schedule = "true" | ||
|
||
scheduler_tag = { | ||
key = "tostop" | ||
value = "true" | ||
} | ||
} | ||
|
||
module "ecs-start-monday" { | ||
source = "../../" | ||
name = "start-ecs" | ||
cloudwatch_schedule_expression = "cron(0 07 ? * MON *)" | ||
schedule_action = "start" | ||
ec2_schedule = "false" | ||
ecs_schedule = "true" | ||
autoscaling_schedule = "false" | ||
cloudwatch_alarm_schedule = "true" | ||
|
||
scheduler_tag = { | ||
key = "tostop" | ||
value = "true" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Terraform ex2-schedule outputs | ||
|
||
output "lambda_stop_name" { | ||
value = module.ecs-stop-friday.scheduler_lambda_name | ||
} | ||
|
||
output "lambda_stop_arn" { | ||
value = module.ecs-stop-friday.scheduler_lambda_arn | ||
} | ||
|
||
output "lambda_start_name" { | ||
value = module.ecs-start-monday.scheduler_lambda_name | ||
} | ||
|
||
output "lambda_start_arn" { | ||
value = module.ecs-start-monday.scheduler_lambda_arn | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
terraform { | ||
required_providers { | ||
aws = { | ||
source = "hashicorp/aws" | ||
version = "4.20.0" | ||
} | ||
archive = { | ||
source = "hashicorp/archive" | ||
version = "2.2.0" | ||
} | ||
} | ||
required_version = ">= 1.0.11" | ||
} | ||
|
||
provider "aws" { | ||
region = "us-west-1" | ||
default_tags { | ||
tags = { | ||
Created_by = "Terraform" | ||
Project = "esc-scheduler-testing-example" | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
variable "random_tag" { | ||
description = "aws tag use during integration tests" | ||
default = "terratest_random_tag" | ||
} |
Oops, something went wrong.