-
Notifications
You must be signed in to change notification settings - Fork 1
/
1_simple_state_machine.tf
126 lines (113 loc) · 2.88 KB
/
1_simple_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
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
resource "aws_iam_role" "simple_step_function_role" {
name = "simple_step_function_role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Sid = ""
Principal = {
Service = "states.amazonaws.com"
}
},
]
})
tags = {
tag-key = "tag-value"
}
}
resource "aws_iam_role_policy" "simple_step_function_policy" {
policy = <<END
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "lambda:*",
"Resource": "*"
}
]
}
END
role = aws_iam_role.simple_step_function_role.id
}
resource "aws_sfn_state_machine" "sfn_state_machine" {
name = "simple-step-function"
role_arn = aws_iam_role.simple_step_function_role.arn
definition = <<EOF
{
"Comment": "A Hello World example of the Amazon States Language using an AWS Lambda Function",
"StartAt": "Step1",
"States": {
"Step1": {
"Type": "Task",
"Resource": "${aws_lambda_function.simple_step_function_lambda_1.arn}",
"Next": "Step2"
},
"Step2": {
"Type": "Task",
"Resource": "${aws_lambda_function.simple_step_function_lambda_2.arn}",
"End": true
}
}
}
EOF
}
data "aws_iam_policy_document" "assume_role" {
statement {
effect = "Allow"
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
actions = ["sts:AssumeRole"]
}
}
resource "aws_iam_role" "iam_for_lambda" {
name = "iam_for_lambda"
assume_role_policy = data.aws_iam_policy_document.assume_role.json
}
data "archive_file" "lambda_zip_inline_1" {
type = "zip"
output_path = "/tmp/lambda_zip_inline1.zip"
source {
content = <<EOF
def test(event, context):
print("Test called")
event["apples"] = "1"
return event
EOF
filename = "main.py"
}
}
data "archive_file" "lambda_zip_inline_2" {
type = "zip"
output_path = "/tmp/lambda_zip_inline2.zip"
source {
content = <<EOF
def test(event, context):
print("Test called")
event["oranges"] = "2"
return event
EOF
filename = "main.py"
}
}
resource "aws_lambda_function" "simple_step_function_lambda_1" {
function_name = "simple-step-function-step1"
role = aws_iam_role.iam_for_lambda.arn
handler = "main.test"
runtime = "python3.9"
filename = data.archive_file.lambda_zip_inline_1.output_path
source_code_hash = data.archive_file.lambda_zip_inline_1.output_base64sha256
}
resource "aws_lambda_function" "simple_step_function_lambda_2" {
function_name = "simple-step-function-step2"
role = aws_iam_role.iam_for_lambda.arn
handler = "main.test"
runtime = "python3.9"
filename = data.archive_file.lambda_zip_inline_2.output_path
source_code_hash = data.archive_file.lambda_zip_inline_2.output_base64sha256
}