-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcentral_account.tf
264 lines (226 loc) · 6.73 KB
/
central_account.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
* = Components needed for the central account
*
* These components are used and accessed by the central deployment account.
*/
/*
* == Deployment Account Permissions
*
*lambda_ecs_trigger Allow the deployment account to make changes to this account,
* and read the pipelines.
*/
// This allows the deployment account to see incoming triggers and pipeline events
data "aws_iam_policy_document" "allow_subscription" {
statement {
principals {
type = "AWS"
identifiers = [var.central_account]
}
resources = ["*"]
actions = ["SNS:Subscribe"]
}
}
data "aws_iam_policy_document" "allow_central_account_assume" {
statement {
principals {
type = "AWS"
identifiers = [var.central_account]
}
actions = ["sts:AssumeRole"]
}
}
resource "aws_iam_role" "central_account" {
name = "deployment-access"
description = "Access for the central deployment account"
assume_role_policy = data.aws_iam_policy_document.allow_central_account_assume.json
}
resource "aws_iam_role_policy" "central_account_allow_pass_role_to_step_functions" {
role = aws_iam_role.central_account.id
policy = data.aws_iam_policy_document.central_account_allow_pass_role_to_step_functions.json
}
data "aws_iam_policy_document" "central_account_allow_pass_role_to_step_functions" {
statement {
effect = "Allow"
actions = ["iam:PassRole"]
resources = [aws_iam_role.sfn.arn]
}
}
resource "aws_iam_role_policy" "central_account_allow_ecs" {
role = aws_iam_role.central_account.id
policy = data.aws_iam_policy_document.central_account_allow_ecs.json
}
data "aws_iam_policy_document" "central_account_allow_ecs" {
statement {
effect = "Allow"
actions = [
"ecs:DeregisterTaskDefinition",
"ecs:RegisterTaskDefinition",
]
resources = ["*"]
}
statement {
effect = "Allow"
actions = ["iam:PassRole"]
resources = [
aws_iam_role.deployment_task.arn,
aws_iam_role.execution_role.arn
]
}
statement {
effect = "Allow"
actions = ["ecs:RunTask"]
resources = ["*"]
condition {
test = "ArnEquals"
variable = "ecs:cluster"
values = [aws_ecs_cluster.cluster.arn]
}
}
}
// The deployment account is responsible for managing everything around SFN.
data "aws_iam_policy_document" "allow_sfn" {
statement {
resources = ["*"]
actions = ["states:*"]
}
}
resource "aws_iam_role_policy" "allow_sfn" {
role = aws_iam_role.central_account.id
name = "allow-sfn-access"
policy = data.aws_iam_policy_document.allow_sfn.json
}
// The deployment account has to be able to read all artifacts
data "aws_iam_policy_document" "allow_read_artifacts" {
statement {
resources = ["*"]
actions = [
"s3:List*",
"s3:Get*",
"s3:Describe*",
"ecr:List*",
"ecr:Get*",
"ecr:Describe*",
]
}
}
resource "aws_iam_role_policy" "allow_read_artifacts" {
role = aws_iam_role.central_account.id
name = "allow-read-artifacts"
policy = data.aws_iam_policy_document.allow_read_artifacts.json
}
/*
* == Outgoing Messages
*
* A way for the deployment agents to figure out what has changed.
*/
data "aws_iam_policy_document" "eventbridge_in_org_assume" {
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["events.amazonaws.com"]
}
}
}
resource "aws_iam_role" "eventbridge_send_cross_account" {
name = "deployment-eventbridge-cross-account-role"
assume_role_policy = data.aws_iam_policy_document.eventbridge_in_org_assume.json
}
data "aws_iam_policy_document" "eventbridge_send_message_to_eventbridge" {
statement {
effect = "Allow"
actions = ["events:PutEvents"]
resources = ["*"]
}
}
resource "aws_iam_role_policy" "eventbridge_send_message_to_eventbridge" {
role = aws_iam_role.eventbridge_send_cross_account.id
policy = data.aws_iam_policy_document.eventbridge_send_message_to_eventbridge.json
}
/*
* === Configuration of eventbridge rules
*/
resource "aws_cloudwatch_event_rule" "sfn_status" {
description = "Triggers when a State Machine changes status"
event_pattern = <<-EOF
{
"source": [
"aws.states"
],
"detail-type": [
"Step Functions Execution Status Change"
],
"detail": {
"status": ["RUNNING", "FAILED", "SUCCEEDED", "TIMED_OUT", "ABORTED"],
"stateMachineArn": [
{
"prefix": ""
}
]
}
}
EOF
}
resource "aws_cloudwatch_event_target" "sfn_events" {
arn = "arn:aws:events:eu-west-1:${var.central_account}:event-bus/cross-account-events"
rule = aws_cloudwatch_event_rule.sfn_status.name
role_arn = aws_iam_role.eventbridge_send_cross_account.arn
}
/*
* == Attach to the central pipeline
*
* This notifies the central account of our topics, so it can subscribe to them.
*/
resource "vy_deployment_account" "this" {
slack_channel = var.slack_channel
}
/*
* == Account Information
*
* This is info about the account that will be used by the pipeline.
*/
locals {
ssm_base_path = "deployment-pipeline"
account_information = {
aws_region = data.aws_region.current.name
artifact_bucket = aws_s3_bucket.artifacts.bucket
# The role that the pipeline will assume into each account
deploy_role = var.deployment_role
# The accounts to deploy to
deploy_accounts = jsonencode(var.deployment_accounts)
# Info about where we're running the task
execution_role_arn = aws_iam_role.execution_role.arn
task_role_arn = aws_iam_role.deployment_task.arn
ecs_cluster = aws_ecs_cluster.cluster.arn
task_family = "delivery-pipeline" # TODO: Don't hardcode
subnets = jsonencode(var.subnets)
security_groups = jsonencode([aws_security_group.deployment_task.id])
log_group = aws_cloudwatch_log_group.ecs.name
step_function_role_arn = aws_iam_role.sfn.arn
}
}
resource "aws_ssm_parameter" "account_information" {
for_each = local.account_information
name = "/${local.ssm_base_path}/${each.key}"
type = "String"
value = each.value
}
data "aws_iam_policy_document" "allow_read_account_information" {
statement {
resources = [
"arn:aws:ssm:eu-west-1:${data.aws_caller_identity.current.account_id}:parameter/${local.ssm_base_path}",
"arn:aws:ssm:eu-west-1:${data.aws_caller_identity.current.account_id}:parameter/${local.ssm_base_path}/*",
]
actions = [
"ssm:GetParameter",
"ssm:GetParameters",
"ssm:GetParametersByPath",
]
}
}
resource "aws_iam_role_policy" "allow_read_account_information" {
role = aws_iam_role.central_account.id
name = "allow-read-account-information"
policy = data.aws_iam_policy_document.allow_read_account_information.json
}