-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscheduled_tasks.tf
59 lines (46 loc) · 1.7 KB
/
scheduled_tasks.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
resource "aws_cloudwatch_event_rule" "this" {
count = length(var.scheduled_tasks)
name = "${local.id}-${element(var.scheduled_tasks, count.index).name}"
description = element(var.scheduled_tasks, count.index).description
schedule_expression = element(var.scheduled_tasks, count.index).schedule
is_enabled = lookup(element(var.scheduled_tasks, count.index), "is_enabled", true)
}
resource "aws_cloudwatch_event_target" "this" {
count = length(var.scheduled_tasks)
target_id = "${local.id}-${element(var.scheduled_tasks, count.index).name}"
arn = data.aws_ecs_cluster.this.arn
rule = "${local.id}-${element(var.scheduled_tasks, count.index).name}"
role_arn = data.aws_iam_role.ecs_events.0.arn
ecs_target {
launch_type = "EC2"
task_count = 1
task_definition_arn = local.task != "" ? local.task : "arn:aws:ecs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:task-definition/${aws_ecs_task_definition.this.0.family}"
dynamic "network_configuration" {
for_each = var.network_mode == "awsvpc" ? ["awsvpc"] : []
content {
subnets = var.subnets
security_groups = concat(aws_security_group.this.*.id, var.outbound_security_groups)
}
}
}
input = <<DOC
{
"containerOverrides": [
{
"name": "${element(var.scheduled_tasks, count.index).container_name}",
"command": ${element(var.scheduled_tasks, count.index).command}
}
]
}
DOC
depends_on = [
aws_cloudwatch_event_rule.this
]
}
data "aws_ecs_cluster" "this" {
cluster_name = var.cluster
}
data "aws_iam_role" "ecs_events" {
count = length(var.scheduled_tasks) > 0 ? 1 : 0
name = "ecsEventsRole"
}