-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstep_function.tf
More file actions
180 lines (162 loc) · 4.32 KB
/
Copy pathstep_function.tf
File metadata and controls
180 lines (162 loc) · 4.32 KB
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
locals {
## Base states for the Step Function state machine
base_states = {
NormalizePipeBatchInput = {
Type = "Choice"
Choices = [
{
Variable = "$[0]"
IsPresent = true
Next = "UnwrapPipeBatch"
}
]
Default = "DetermineExecutionMode"
}
UnwrapPipeBatch = {
Type = "Pass"
InputPath = "$[0]"
Next = "DetermineExecutionMode"
}
DetermineExecutionMode = {
Type = "Choice"
Choices = [
{
Variable = "$.source"
StringEquals = "eventbridge.account_creation"
Next = "InvokeLambdaSingleAccount"
}
]
Default = "InvokeLambdaAllAccounts"
}
InvokeLambdaSingleAccount = {
Type = "Task"
Resource = module.lambda.lambda_function_arn
Parameters = {
"account_id.$" = "$.detail.account_id"
"source" = "account_creation"
}
Next = "EvaluateResponse"
Retry = [
{
ErrorEquals = ["States.ALL"]
IntervalSeconds = 2
MaxAttempts = 3
BackoffRate = 2.0
}
]
}
InvokeLambdaAllAccounts = {
Type = "Task"
Resource = module.lambda.lambda_function_arn
Parameters = {
"source" = "cron_schedule"
}
Next = "EvaluateResponse"
Retry = [
{
ErrorEquals = ["States.ALL"]
IntervalSeconds = 2
MaxAttempts = 3
BackoffRate = 2.0
}
]
}
EvaluateResponse = {
Type = "Choice"
Choices = [
{
Variable = "$.errors"
IsNull = false
Next = var.sns_topic_arn != null ? "SendNotification" : "Failure"
}
]
Default = "Success"
}
Success = {
Type = "Succeed"
}
Failure = {
Type = "Fail"
}
}
## Optional SendNotification state (only included if SNS topic is provided)
optional_states = var.sns_topic_arn != null ? {
SendNotification = {
Type = "Task"
Resource = "arn:aws:states:::sns:publish"
Parameters = {
TopicArn = var.sns_topic_arn
Message = {
"error_details.$" = "$"
}
}
Next = "Failure"
}
} : {}
## Merge all states
all_states = merge(local.base_states, local.optional_states)
## The definition of the Step Function state machine
step_function_definition = jsonencode({
Comment = "Used to orchestrate the SSO group assignment workflow"
StartAt = "NormalizePipeBatchInput"
States = local.all_states
})
}
## Craft assume role policy for the Step Function
data "aws_iam_policy_document" "step_function_assume_role" {
statement {
sid = "AllowStepFunctionToAssumeRole"
effect = "Allow"
principals {
type = "Service"
identifiers = ["states.amazonaws.com"]
}
actions = [
"sts:AssumeRole"
]
}
}
## Craft a IAM policy document for the Step Function
data "aws_iam_policy_document" "step_function_lambda" {
statement {
sid = "AllowStepFunctionToInvokeLambda"
effect = "Allow"
actions = [
"lambda:InvokeFunction"
]
resources = [module.lambda.lambda_function_arn]
}
dynamic "statement" {
for_each = var.sns_topic_arn != null ? [1] : []
content {
sid = "AllowStepFunctionToPublishToSNS"
effect = "Allow"
actions = [
"sns:Publish"
]
resources = [var.sns_topic_arn]
}
}
}
## Provision a IAM role for the Step Function
resource "aws_iam_role" "step_function" {
name = format("%s-step-function", var.name)
tags = local.tags
assume_role_policy = data.aws_iam_policy_document.step_function_assume_role.json
}
## Provide the Step Function role the ability to invoke the Lambda function
resource "aws_iam_role_policy" "step_function_lambda" {
name = format("%s-step-function-lambda", var.name)
role = aws_iam_role.step_function.id
policy = data.aws_iam_policy_document.step_function_lambda.json
}
## Provision a Step Function state machine
resource "aws_sfn_state_machine" "main" {
name = var.name
role_arn = aws_iam_role.step_function.arn
definition = local.step_function_definition
tags = local.tags
depends_on = [
aws_iam_role_policy.step_function_lambda,
]
}