-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcron.tf
72 lines (62 loc) · 1.63 KB
/
cron.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
60
61
62
63
64
65
66
67
68
69
70
71
72
# Runs the sync on a schedule using EventBridge
resource "aws_cloudwatch_event_rule" "cron_aws-reposync" {
name = "cron_aws-repsoync"
description = "An event on a cron schedule, triggering a reposync task in ECS"
schedule_expression = "cron(${var.cron})"
tags = var.tags
}
resource "aws_iam_role" "ecs_events" {
name = "ecs_events"
assume_role_policy = <<DOC
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "events.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
DOC
tags = var.tags
}
resource "aws_iam_role_policy" "ecs_events_run_task_with_any_role" {
name = "ecs_events_run_task_with_any_role"
role = aws_iam_role.ecs_events.id
policy = <<DOC
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "iam:PassRole",
"Resource": "*"
},
{
"Effect": "Allow",
"Action": "ecs:RunTask",
"Resource": "${replace(aws_ecs_task_definition.aws-reposync.arn, "/:\\d+$/", ":*")}"
}
]
}
DOC
}
resource "aws_cloudwatch_event_target" "ecs_scheduled_task" {
arn = aws_ecs_cluster.aws-reposync.arn
rule = aws_cloudwatch_event_rule.cron_aws-reposync.name
role_arn = aws_iam_role.ecs_events.arn
ecs_target {
launch_type = "FARGATE"
task_count = 1
task_definition_arn = aws_ecs_task_definition.aws-reposync.arn
network_configuration {
subnets = var.subnets
assign_public_ip = true
}
tags = var.tags
}
}