-
Notifications
You must be signed in to change notification settings - Fork 1
/
state_machine.tf
83 lines (69 loc) · 2.49 KB
/
state_machine.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
# STATE MACHINE DEFINITON
# --------------------------------------------------------------------------------
resource "aws_sfn_state_machine" "state_machine" {
name = "${local.resource_prefix}SFN-StateMachine"
role_arn = aws_iam_role.step_functions_role.arn
definition = templatefile("${path.module}/state_machine.json", {
get_organizations_lambda = aws_lambda_function.get_organizations.arn,
prepare_organizations_lambda = aws_lambda_function.prepare_organizations.arn,
get_workspaces_lambda = aws_lambda_function.get_workspaces.arn,
prepare_workspaces_lambda = aws_lambda_function.prepare_workspaces.arn,
create_workspace_state_backup_lambda = aws_lambda_function.create_workspace_state_backup.arn,
send_report_lambda = aws_lambda_function.send_report.arn,
handle_error_lambda = aws_lambda_function.handle_error.arn,
clean_up_lambda = aws_lambda_function.clean_up.arn
})
}
# IAM
# --------------------------------------------------------------------------------
# Role
resource "aws_iam_role" "step_functions_role" {
name = "${local.resource_prefix}SFN-Role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "states.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}
# Policy document
data "aws_iam_policy_document" "step_functions_policy" {
statement {
effect = "Allow"
actions = ["lambda:InvokeFunction"]
resources = [
aws_lambda_function.get_organizations.arn,
aws_lambda_function.prepare_organizations.arn,
aws_lambda_function.get_workspaces.arn,
aws_lambda_function.prepare_workspaces.arn,
aws_lambda_function.create_workspace_state_backup.arn,
aws_lambda_function.send_report.arn,
aws_lambda_function.handle_error.arn,
aws_lambda_function.clean_up.arn
]
}
statement {
effect = "Allow"
actions = ["sns:Publish"]
resources = [aws_sns_topic.report.arn]
}
}
# Policy
resource "aws_iam_policy" "step_function_policy" {
name = "${local.resource_prefix}SFN-Policy"
policy = data.aws_iam_policy_document.step_functions_policy.json
}
# Polic attachment
resource "aws_iam_role_policy_attachment" "step_functions_role_attachment" {
role = aws_iam_role.step_functions_role.name
policy_arn = aws_iam_policy.step_function_policy.arn
}