-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeployment_tasks.tf
126 lines (109 loc) · 3.33 KB
/
deployment_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
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
/*
* == Cluster
*
* This is where the terraform container will be running.
*/
locals {
non_null_accounts = [for account in values(var.deployment_accounts) : account if account != null]
}
resource "aws_ecs_cluster" "cluster" {
name = "${var.name_prefix}-delivery-pipeline"
}
resource "aws_cloudwatch_log_group" "ecs" {
name = "/aws/ecs/${var.name_prefix}-delivery-pipeline"
}
# Execution Role to allow Fargate to run our tasks.
resource "aws_iam_role" "execution_role" {
name = "${var.name_prefix}-deployment-pipeline-ecs-execution"
assume_role_policy = data.aws_iam_policy_document.ecs_assume.json
}
resource "aws_iam_role_policy_attachment" "ECSTaskExecution" {
role = aws_iam_role.execution_role.id
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
}
data "aws_iam_policy_document" "ecs_assume" {
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
identifiers = ["ecs-tasks.amazonaws.com"]
type = "Service"
}
}
}
# Our general access rules for our terraform container
resource "aws_iam_role" "deployment_task" {
name = "${var.name_prefix}-deployment-task"
assume_role_policy = data.aws_iam_policy_document.ecs_assume.json
}
resource "aws_iam_role_policy" "ecs_allow_logging" {
role = aws_iam_role.deployment_task.id
policy = data.aws_iam_policy_document.ecs_allow_logging.json
}
data "aws_iam_policy_document" "ecs_allow_logging" {
statement {
effect = "Allow"
actions = [
"logs:CreateLogStream",
"logs:PutLogEvents"
]
resources = [
"${aws_cloudwatch_log_group.ecs.arn}:*"
]
}
}
resource "aws_iam_role_policy" "ecs_allow_step_function_invoke" {
role = aws_iam_role.deployment_task.id
policy = data.aws_iam_policy_document.ecs_allow_step_function_invoke.json
}
data "aws_iam_policy_document" "ecs_allow_step_function_invoke" {
statement {
effect = "Allow"
actions = [
"states:SendTaskSuccess",
"states:SendTaskFailure",
]
resources = [
"*"
]
}
}
resource "aws_iam_role_policy" "allow_artifact_bucket_access" {
role = aws_iam_role.deployment_task.id
policy = data.aws_iam_policy_document.allow_artifact_bucket_access.json
}
data "aws_iam_policy_document" "allow_artifact_bucket_access" {
statement {
effect = "Allow"
actions = ["s3:Get*", "s3:List*"]
resources = [aws_s3_bucket.artifacts.arn, "${aws_s3_bucket.artifacts.arn}/*"]
}
}
resource "aws_iam_role_policy" "allow_assume_deployment_role" {
role = aws_iam_role.deployment_task.id
policy = data.aws_iam_policy_document.allow_assume_deployment_role.json
}
data "aws_iam_policy_document" "allow_assume_deployment_role" {
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
resources = formatlist(
"arn:aws:iam::%s:role/${var.deployment_role}",
local.non_null_accounts
)
}
}
# Security Group for the task
resource "aws_security_group" "deployment_task" {
name = "deployment_task"
description = "Rules for deployment tasks"
vpc_id = var.vpc_id
}
resource "aws_security_group_rule" "allow_all_outbound" {
type = "egress"
security_group_id = aws_security_group.deployment_task.id
protocol = "all"
from_port = 0
to_port = 0
cidr_blocks = ["0.0.0.0/0"]
}