-
Notifications
You must be signed in to change notification settings - Fork 29
/
main.tf
129 lines (119 loc) · 3.2 KB
/
main.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}
}
}
// Region speficied in AWS provider
data "aws_region" "current" {}
resource "aws_secretsmanager_secret" "prefect_api_key" {
name = "prefect-api-key-${var.name}"
recovery_window_in_days = var.secrets_manager_recovery_in_days
}
resource "aws_secretsmanager_secret_version" "prefect_api_key_version" {
secret_id = aws_secretsmanager_secret.prefect_api_key.id
secret_string = var.prefect_api_key
}
resource "aws_iam_role" "prefect_agent_execution_role" {
name = "prefect-agent-execution-role-${var.name}"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "ecs-tasks.amazonaws.com"
}
},
]
})
inline_policy {
name = "ssm-allow-read-prefect-api-key-${var.name}"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = [
"kms:Decrypt",
"secretsmanager:GetSecretValue",
"ssm:GetParameters"
]
Effect = "Allow"
Resource = [
aws_secretsmanager_secret.prefect_api_key.arn
]
}
]
})
}
inline_policy {
name = "logs-allow-create-log-group-${var.name}"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = [
"logs:CreateLogGroup",
]
Effect = "Allow"
Resource = "*"
}
]
})
}
// AmazonECSTaskExecutionRolePolicy is an AWS managed role for creating ECS tasks and services
managed_policy_arns = ["arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"]
}
resource "aws_iam_role" "prefect_agent_task_role" {
name = "prefect-agent-task-role-${var.name}"
count = var.agent_task_role_arn == null ? 1 : 0
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "ecs-tasks.amazonaws.com"
}
},
]
})
inline_policy {
name = "prefect-agent-allow-ecs-task-${var.name}"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = [
"ec2:DescribeSubnets",
"ec2:DescribeVpcs",
"ecr:BatchCheckLayerAvailability",
"ecr:BatchGetImage",
"ecr:GetAuthorizationToken",
"ecr:GetDownloadUrlForLayer",
"ecs:DeregisterTaskDefinition",
"ecs:DescribeTasks",
"ecs:RegisterTaskDefinition",
"ecs:RunTask",
"ecs:StopTask",
"iam:PassRole",
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:GetLogEvents",
"logs:PutLogEvents"
]
Effect = "Allow"
Resource = "*"
}
]
})
}
}
resource "aws_cloudwatch_log_group" "prefect_agent_log_group" {
name = "prefect-agent-log-group-${var.name}"
retention_in_days = var.agent_log_retention_in_days
}