-
Notifications
You must be signed in to change notification settings - Fork 1
/
2_order_processing.tf
129 lines (121 loc) · 2.84 KB
/
2_order_processing.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
resource "aws_iam_role" "order_step_function_role" {
name = "order_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" "order_step_function_policy" {
policy = <<END
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"lambda:*",
"dynamodb:*",
"sqs:*",
"sns:*"
],
"Resource": "*"
}
]
}
END
role = aws_iam_role.order_step_function_role.id
}
resource "aws_sns_topic" "order_publish" {
name = "order-publish"
}
resource "aws_sqs_queue" "order_queue" {
name = "order-queue"
delay_seconds = 5
max_message_size = 2048
message_retention_seconds = 86400
receive_wait_time_seconds = 10
redrive_policy = jsonencode({
deadLetterTargetArn = aws_sqs_queue.order_queue_dlq.arn
maxReceiveCount = 4
})
}
resource "aws_sqs_queue" "order_queue_dlq" {
name = "order-queue-dlq"
delay_seconds = 90
max_message_size = 2048
message_retention_seconds = 86400
receive_wait_time_seconds = 10
}
resource "aws_sfn_state_machine" "order_state_machine" {
name = "order-step-function"
role_arn = aws_iam_role.order_step_function_role.arn
definition = <<EOF
{
"StartAt": "InsertOrder",
"States": {
"InsertOrder": {
"Type": "Task",
"Resource": "arn:aws:states:::dynamodb:putItem",
"Parameters": {
"TableName": "order",
"Item": {
"order_id": {
"S.$": "$.order_id"
}
}
},
"ResultPath": null,
"Next": "Process Order"
},
"Process Order": {
"Type": "Task",
"TimeoutSeconds": 60,
"Resource": "arn:aws:states:::sqs:sendMessage.waitForTaskToken",
"Parameters": {
"QueueUrl": "${aws_sqs_queue.order_queue.url}",
"MessageBody": {
"MessageTitle.$": "$.order_id",
"TaskToken.$": "$$.Task.Token"
}
},
"ResultPath": "$.result",
"Next": "SNS Publish",
"Retry": [
{
"ErrorEquals": [
"States.ALL"
],
"BackoffRate": 2,
"MaxAttempts": 3,
"IntervalSeconds": 5,
"Comment": "retry",
"JitterStrategy": "FULL"
}
]
},
"SNS Publish": {
"Type": "Task",
"Resource": "arn:aws:states:::sns:publish",
"Parameters": {
"Message.$": "$",
"TopicArn": "${aws_sns_topic.order_publish.arn}"
},
"End": true
}
}
}
EOF
}