-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor ECS scheduled tasks #1992
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| data "aws_caller_identity" "current" {} | ||
| data "aws_region" "current" {} | ||
|
|
||
| resource "aws_ecs_task_definition" "this" { | ||
| family = var.task_name | ||
|
|
||
| execution_role_arn = var.execution_role_arn | ||
| task_role_arn = var.task_role_arn | ||
| requires_compatibilities = var.requires_compatibilities | ||
| cpu = var.cpu | ||
| memory = var.memory | ||
| network_mode = "awsvpc" | ||
| track_latest = true | ||
|
|
||
| runtime_platform { | ||
| operating_system_family = "LINUX" | ||
| cpu_architecture = "ARM64" | ||
| } | ||
|
|
||
| container_definitions = jsonencode([merge( | ||
| var.base_task_container_definition, | ||
| { | ||
| name = "main" | ||
| command = var.command | ||
| logConfiguration = { | ||
| logDriver = "awslogs", | ||
| options = { | ||
| awslogs-group = var.application_log_group_name, | ||
| awslogs-region = data.aws_region.current.name, | ||
| awslogs-stream-prefix = var.task_name | ||
| } | ||
| } | ||
| } | ||
| )]) | ||
| } | ||
|
|
||
| resource "aws_cloudwatch_event_rule" "this" { | ||
| name = var.task_name | ||
| description = "Trigger the ${var.task_name} ECS task on a schedule" | ||
| schedule_expression = var.schedule_expression | ||
| } | ||
|
|
||
| resource "aws_cloudwatch_event_target" "this" { | ||
| arn = var.ecs_cluster_arn | ||
| rule = aws_cloudwatch_event_rule.this.name | ||
| role_arn = var.scheduler_role_arn | ||
|
|
||
| ecs_target { | ||
| # EventBridge must target task family ARN without revision to always run latest. | ||
| task_definition_arn = "arn:aws:ecs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:task-definition/${aws_ecs_task_definition.this.family}" | ||
| launch_type = "FARGATE" | ||
| platform_version = var.platform_version | ||
|
|
||
| network_configuration { | ||
| assign_public_ip = false | ||
| security_groups = var.network_security_groups | ||
| subnets = var.network_subnets | ||
| } | ||
| } | ||
|
|
||
| dead_letter_config { | ||
| arn = var.eventbridge_dead_letter_queue_arn | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| output "event_rule_name" { | ||
| value = aws_cloudwatch_event_rule.this.name | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| variable "task_name" { | ||
| type = string | ||
| description = "The scheduled task name." | ||
| } | ||
|
|
||
| variable "schedule_expression" { | ||
| type = string | ||
| description = "EventBridge schedule expression, for example cron(...) or rate(...)." | ||
| } | ||
|
|
||
| variable "command" { | ||
| type = list(string) | ||
| description = "Container command override for the scheduled task." | ||
| } | ||
|
|
||
| variable "ecs_cluster_arn" { | ||
| type = string | ||
| description = "ECS cluster ARN targeted by EventBridge." | ||
| } | ||
|
|
||
| variable "scheduler_role_arn" { | ||
| type = string | ||
| description = "Shared IAM role ARN used by EventBridge to run ECS scheduled tasks." | ||
| } | ||
|
|
||
| variable "eventbridge_dead_letter_queue_arn" { | ||
| type = string | ||
| description = "EventBridge dead-letter queue ARN." | ||
| } | ||
|
|
||
| variable "base_task_container_definition" { | ||
| type = any | ||
| description = "Base container definition to clone from the app ECS service." | ||
| } | ||
|
|
||
| variable "application_log_group_name" { | ||
| type = string | ||
| description = "CloudWatch Logs group name used by the task container." | ||
| } | ||
|
|
||
| variable "execution_role_arn" { | ||
| type = string | ||
| description = "Execution role ARN for the ECS task definition." | ||
| } | ||
|
|
||
| variable "task_role_arn" { | ||
| type = string | ||
| description = "Task role ARN for the ECS task definition." | ||
| } | ||
|
|
||
| variable "requires_compatibilities" { | ||
| type = list(string) | ||
| description = "Task definition launch compatibilities." | ||
| } | ||
|
|
||
| variable "cpu" { | ||
| type = any | ||
| description = "Task definition CPU value." | ||
| } | ||
|
|
||
| variable "memory" { | ||
| type = any | ||
| description = "Task definition memory value." | ||
| } | ||
|
|
||
| variable "network_security_groups" { | ||
| type = list(string) | ||
| description = "Security groups for the scheduled ECS task network config." | ||
| } | ||
|
|
||
| variable "network_subnets" { | ||
| type = list(string) | ||
| description = "Subnets for the scheduled ECS task network config." | ||
| } | ||
|
|
||
| variable "platform_version" { | ||
| type = string | ||
| description = "ECS Fargate platform version." | ||
| default = "1.4.0" | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,27 @@ | ||||||
| locals { | ||||||
| scheduled_tasks = {} | ||||||
| } | ||||||
|
|
||||||
| module "scheduled_tasks" { | ||||||
| for_each = { | ||||||
| for task_name, task in local.scheduled_tasks : task_name => task | ||||||
| if task.enabled | ||||||
| } | ||||||
| source = "../ecs-scheduled-task" | ||||||
|
|
||||||
| task_name = "forms-admin-${replace(each.key, "_", "-")}" | ||||||
|
||||||
| task_name = "forms-admin-${replace(each.key, "_", "-")}" | |
| task_name = "${var.env_name}_forms-admin-${replace(each.key, "_", "-")}" |
Uh oh!
There was an error while loading. Please reload this page.