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

feat: re-adding eventbridge module #7

Merged
merged 1 commit into from
Jul 17, 2024
Merged
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
10 changes: 5 additions & 5 deletions .github/workflows/ebs-checker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ on:
paths:
- 'infrastructure/lambdas/ebs-checker/*'
- 'lambdas/ebs-checker/*'
# push:
# branches: [ "main" ]
# paths:
# - 'infrastructure/lambdas/ebs-checker/*'
# - 'lambdas/ebs-checker/*'
push:
branches: [ "main" ]
paths:
- 'infrastructure/lambdas/ebs-checker/*'
- 'lambdas/ebs-checker/*'

permissions:
contents: read
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/resizing-service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ on:
paths:
- 'infrastructure/lambdas/resizing-service/*'
- 'lambdas/resizing-service/*'
# push:
# branches: [ "main" ]
# paths:
# - 'infrastructure/lambdas/resizing-service/*'
# - 'lambdas/resizing-service/*'
push:
branches: [ "main" ]
paths:
- 'infrastructure/lambdas/resizing-service/*'
- 'lambdas/resizing-service/*'

permissions:
contents: read
Expand Down
63 changes: 63 additions & 0 deletions infrastructure/lambdas/ebs-checker/.terraform.lock.hcl

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

6 changes: 6 additions & 0 deletions infrastructure/lambdas/ebs-checker/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ module "complete" {
}
]

create_eventbridge_schedule_group = true
eventbridge_schedule_group_name = "ebs-checker-schedule-group"
create_eventbridge_schedule = true
eventbridge_schedule_name = "ebs-checker-schedule"
eventbridge_schedule_expression = "rate(1 day)"

depends_on = [aws_iam_policy.ebs_modify_policy]
}

Expand Down
22 changes: 22 additions & 0 deletions infrastructure/lambdas/resizing-service/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ module "scale_up_ecs" {

create_sns_topic = false

create_eventbridge_schedule_group = true
eventbridge_schedule_group_name = "resizing-services"
create_eventbridge_schedule = true
eventbridge_schedule_name = "scale-up-ecs"
eventbridge_schedule_expression = "cron(0 8 * * ? *)"
eventbridge_schedule_target_input = jsonencode({
"cluster_name" : "cluster-exemplo-ugf",
"service_name" : "nginx-example",
"desired_count" : 4
})

depends_on = [aws_iam_policy.ecs_modify_policy]
}

Expand Down Expand Up @@ -53,6 +64,17 @@ module "scale_down_ecs" {

create_sns_topic = false

create_eventbridge_schedule_group = false
eventbridge_schedule_group_name = "resizing-services"
create_eventbridge_schedule = true
eventbridge_schedule_name = "scale-down-ecs"
eventbridge_schedule_expression = "cron(0 19 * * ? *)"
eventbridge_schedule_target_input = jsonencode({
"cluster_name" : "cluster-exemplo-ugf",
"service_name" : "nginx-example",
"desired_count" : 1
})

depends_on = [aws_iam_policy.ecs_modify_policy]
}

Expand Down
62 changes: 62 additions & 0 deletions infrastructure/modules/complete/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,66 @@ resource "aws_iam_policy" "sns_publish" {

resource "random_id" "id" {
byte_length = 8
}

module "eventbridge_scheduler" {
source = "../eventbridge-schedule"

create_schedule_group = var.create_eventbridge_schedule_group
schedule_group_name = var.eventbridge_schedule_group_name

create_schedule = var.create_eventbridge_schedule
schedule_name = var.eventbridge_schedule_name
schedule_expression = var.eventbridge_schedule_expression
flexible_time_window = var.eventbridge_flexible_time_window
schedule_target = {
target_arn = module.lambda_function.lambda_function_arn
role_arn = aws_iam_role.eventbridge_scheduler.0.arn
input = try(var.eventbridge_schedule_target_input, null) != null ? var.eventbridge_schedule_target_input : null
}
tags = merge(var.eventbridge_tags, var.default_tags)
}

resource "aws_iam_role" "eventbridge_scheduler" {
count = var.create_eventbridge_schedule ? 1 : 0

name = "eventbridge-scheduler-role-${random_id.id.hex}"

assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Principal = {
Service = "scheduler.amazonaws.com"
}
Action = "sts:AssumeRole"
}
]
})
}

resource "aws_iam_policy" "eventbridge_scheduler_lambda_policy" {
count = var.create_eventbridge_schedule ? 1 : 0

name = "eventbridge-scheduler-lambda-policy-${random_id.id.hex}"
description = "IAM policy for the Lambda function to be invoked by the EventBridge scheduler"

policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = "lambda:InvokeFunction"
Resource = module.lambda_function.lambda_function_arn
},
]
})
}

resource "aws_iam_role_policy_attachment" "eventbridge_scheduler_lambda_policy_attachment" {
count = var.create_eventbridge_schedule ? 1 : 0

role = aws_iam_role.eventbridge_scheduler.0.name
policy_arn = aws_iam_policy.eventbridge_scheduler_lambda_policy.0.arn
}
54 changes: 54 additions & 0 deletions infrastructure/modules/complete/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,57 @@ variable "sns_topic_tags" {
type = map(string)
default = {}
}

variable "create_eventbridge_schedule_group" {
description = "Whether to create the schedule group"
type = bool
default = true
}

variable "eventbridge_schedule_group_name" {
description = "The name of the schedule group"
type = string
default = "default"
}

variable "create_eventbridge_schedule" {
description = "Whether to create the schedule"
type = bool
default = true
}

variable "eventbridge_schedule_name" {
description = "The name of the schedule"
type = string
default = null
}

variable "eventbridge_flexible_time_window" {
description = "The flexible time window"
type = object({
maximum_window_in_minutes = optional(number, null)
mode = optional(string, "OFF")
})
default = {
maximum_window_in_minutes = null
mode = "OFF"
}
}

variable "eventbridge_schedule_expression" {
description = "The schedule expression"
type = string
default = "rate(1 hours)"
}

variable "eventbridge_schedule_target_input" {
description = "The input for the schedule target"
type = string
default = null
}

variable "eventbridge_tags" {
description = "The tags to apply to the schedule"
type = map(string)
default = {}
}
27 changes: 27 additions & 0 deletions infrastructure/modules/eventbridge-schedule/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
resource "aws_scheduler_schedule_group" "group" {
count = var.create_schedule_group ? 1 : 0

name = var.schedule_group_name
tags = var.tags
}

resource "aws_scheduler_schedule" "this" {
count = var.create_schedule ? 1 : 0

name = var.schedule_name
group_name = try(aws_scheduler_schedule_group.group.0.name, var.schedule_group_name)

flexible_time_window {
maximum_window_in_minutes = var.flexible_time_window.maximum_window_in_minutes
mode = var.flexible_time_window.mode
}

schedule_expression = var.schedule_expression

target {
arn = var.schedule_target.target_arn
role_arn = var.schedule_target.role_arn

input = try(var.schedule_target.input, null) != null ? var.schedule_target.input : null
}
}
55 changes: 55 additions & 0 deletions infrastructure/modules/eventbridge-schedule/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
variable "create_schedule_group" {
description = "Whether to create the schedule group"
type = bool
default = false
}

variable "schedule_group_name" {
description = "Default name for the schedule group"
type = string
default = "default"
}

variable "create_schedule" {
description = "Whether to create the schedule"
type = bool
default = true
}

variable "schedule_name" {
description = "Name of the schedule"
type = string
}

variable "flexible_time_window" {
description = "The flexible time window"
type = object({
maximum_window_in_minutes = optional(number, null)
mode = optional(string, null)
})
default = {
maximum_window_in_minutes = null
mode = "OFF"
}
}

variable "schedule_expression" {
description = "The schedule expression"
type = string
default = "rate(1 hours)"
}

variable "schedule_target" {
description = "The target for the schedule"
type = object({
target_arn = string
role_arn = string
input = optional(string, null)
})
}

variable "tags" {
description = "A map of tags to add to all resources"
type = map(string)
default = {}
}
Loading