This repository has been archived by the owner on Jun 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathschedule.tf
90 lines (81 loc) · 2.23 KB
/
schedule.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
data "aws_subnet" "subnets" {
count = length(var.subnet_ids)
id = var.subnet_ids[count.index]
}
resource "aws_security_group" "allow_outbound_traffic" {
name_prefix = "${var.service_name}-${var.environment_name}"
description = "${var.service_name} Allow outbound traffic"
vpc_id = data.aws_subnet.subnets[0].vpc_id
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_cloudwatch_event_rule" "schedule_rule" {
name = "${var.service_name}-${var.environment_name}"
schedule_expression = var.schedule_expression
is_enabled = true
}
resource "aws_cloudwatch_event_target" "fargate_scheduled_task" {
rule = aws_cloudwatch_event_rule.schedule_rule.name
arn = "arn:aws:ecs:${local.region}:${local.account_id}:cluster/${var.cluster_name}"
role_arn = aws_iam_role.schedule_role.arn
ecs_target {
task_definition_arn = aws_ecs_task_definition.letsencrypt.arn
task_count = 1
launch_type = "FARGATE"
network_configuration {
subnets = var.subnet_ids
security_groups = [aws_security_group.allow_outbound_traffic.id]
}
}
}
resource "aws_iam_role_policy" "schedule_role_policy" {
name = "${var.service_name}-${var.environment_name}-_schedule_policy"
role = aws_iam_role.schedule_role.id
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ecs:RunTask"
],
"Resource": [
"${aws_ecs_task_definition.letsencrypt.arn}"
]
},
{
"Effect": "Allow",
"Action": [
"iam:PassRole"
],
"Resource": [
"${aws_iam_role.ecs_task.arn}",
"${aws_iam_role.ecs_execution.arn}"
]
}
]
}
EOF
}
resource "aws_iam_role" "schedule_role" {
name = "${var.service_name}-${var.environment_name}_schedule_role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "events.amazonaws.com"
},
"Effect": "Allow"
}
]
}
EOF
}