-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
183 lines (161 loc) · 4.78 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
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
provider "aws" {
region = "us-east-1"
}
####################################
### Lambda IAM Roles
####################################
# IAM role for the Lambdas
resource "aws_iam_role" "lambda_exec_role" {
name = "lambda_execution_role"
assume_role_policy = jsonencode({
Version = "2012-10-17",
Statement = [{
Action = "sts:AssumeRole",
Principal = {
Service = "lambda.amazonaws.com"
},
Effect = "Allow",
Sid = "",
}],
})
}
# Give the Lambdas permission to write logs
resource "aws_iam_role_policy_attachment" "lambda_policy" {
role = aws_iam_role.lambda_exec_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
# Give the Lambdas permission to manage Step Functions
resource "aws_iam_role_policy_attachment" "lambda_sfn_policy_attachment" {
role = aws_iam_role.lambda_exec_role.name
policy_arn = "arn:aws:iam::aws:policy/AWSStepFunctionsFullAccess"
}
####################################
### Step Functions IAM Roles
####################################
# IAM role for the Step Functions
resource "aws_iam_role" "sfn_exec_role" {
name = "sfn_execution_role"
assume_role_policy = jsonencode({
Version = "2012-10-17",
Statement = [{
Action = "sts:AssumeRole",
Principal = {
Service = "states.amazonaws.com"
},
Effect = "Allow",
Sid = "",
}],
})
}
# Give the Step Functions permission to invoke Lambdas
resource "aws_iam_role_policy" "sfn_exec_policy" {
role = aws_iam_role.sfn_exec_role.id
policy = jsonencode({
Version = "2012-10-17",
Statement = [
{
Action = [
"lambda:InvokeFunction"
],
Effect = "Allow",
Resource = [
aws_lambda_function.lambda_transaction.arn
]
}
]
})
}
####################################
### Lambda
####################################
# A Lambda function that performs a database transaction
resource "aws_lambda_function" "lambda_transaction" {
function_name = "LambdaTransaction"
handler = "index.handler"
runtime = "nodejs20.x"
filename = "lambda/lambda_transaction.zip"
role = aws_iam_role.lambda_exec_role.arn
memory_size = 512
}
# A Lambda function that executes the standard workflow and returns its execution duration
resource "aws_lambda_function" "sfn_executor" {
function_name = "SfnExecutor"
handler = "sfn_executor.handler"
runtime = "nodejs20.x"
filename = "lambda/sfn_executor.zip"
role = aws_iam_role.lambda_exec_role.arn
timeout = 15
}
####################################
### Step Functions
####################################
# A Step Functions standard workflow that calls LambdaTransaction steps times
resource "aws_sfn_state_machine" "lambda_transaction_standard_workflow" {
name = "BenchmarkStandardWorkflow"
role_arn = aws_iam_role.sfn_exec_role.arn
definition = jsonencode({
Comment : "A Step Functions state machine that calls Lambda steps times",
StartAt : "Initialize",
States : {
Initialize : {
Type : "Pass",
Parameters : {
"currentIndex" : 0,
"totalSteps.$" : "$.steps"
},
ResultPath : "$.iterator",
Next : "Loop"
},
Loop : {
Type : "Choice",
Choices : [
{
Variable : "$.iterator.currentIndex",
NumericLessThanPath : "$.iterator.totalSteps",
Next : "CallLambda"
}
],
Default : "Done"
},
CallLambda : {
Type : "Task",
Resource : aws_lambda_function.lambda_transaction.arn,
Parameters : {
"hostname.$" : "$.hostname",
"username.$" : "$.username",
"password.$" : "$.password"
},
ResultPath : "$.lambdaResult",
Next : "IncrementIndex"
},
IncrementIndex : {
Type : "Pass",
Parameters : {
"currentIndex.$" : "States.MathAdd($.iterator.currentIndex, 1)",
"totalSteps.$" : "$.iterator.totalSteps"
},
ResultPath : "$.iterator",
Next : "Loop"
},
Done : {
Type : "Succeed"
}
}
})
}
# A Step Functions express workflow that calls LambdaTransaction steps times
resource "aws_sfn_state_machine" "lambda_transaction_express_workflow" {
name = "BenchmarkExpressWorkflow"
role_arn = aws_iam_role.sfn_exec_role.arn
definition = aws_sfn_state_machine.lambda_transaction_standard_workflow.definition
type = "EXPRESS"
}
####################################
### Outputs
####################################
output "express_workflow_arn" {
value = aws_sfn_state_machine.lambda_transaction_express_workflow.arn
}
output "sfn_executor_arn" {
value = aws_lambda_function.sfn_executor.arn
}