-
Notifications
You must be signed in to change notification settings - Fork 156
/
Copy pathmain.tf
250 lines (213 loc) · 5.46 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
provider "aws" {
region = "ap-southeast-1"
# Make it faster by skipping something
skip_get_ec2_platforms = true
skip_metadata_api_check = true
skip_region_validation = true
skip_credentials_validation = true
skip_requesting_account_id = true
}
module "eventbridge" {
source = "../../"
bus_name = "${random_pet.this.id}-bus"
attach_tracing_policy = true
attach_kinesis_policy = true
kinesis_target_arns = [aws_kinesis_stream.this.arn]
attach_sfn_policy = true
sfn_target_arns = [module.step_function.this_state_machine_arn]
attach_sqs_policy = true
sqs_target_arns = [
aws_sqs_queue.queue.arn,
aws_sqs_queue.fifo.arn,
aws_sqs_queue.dlq.arn
]
attach_cloudwatch_policy = true
cloudwatch_target_arns = [aws_cloudwatch_log_group.this.arn]
rules = {
orders = {
description = "Capture all order data"
event_pattern = jsonencode({ "source" : ["myapp.orders"] })
enabled = false
}
emails = {
description = "Capture all emails data"
event_pattern = jsonencode({ "source" : ["myapp.emails"] })
enabled = true
}
}
targets = {
orders = [
{
name = "send-orders-to-sqs"
arn = aws_sqs_queue.queue.arn
input_transformer = local.order_input_transformer
},
{
name = "send-orders-to-sqs-wth-dead-letter"
arn = aws_sqs_queue.queue.arn
dead_letter_arn = aws_sqs_queue.dlq.arn
},
{
name = "send-orders-to-sqs-with-retry-policy"
arn = aws_sqs_queue.queue.arn
dead_letter_arn = aws_sqs_queue.dlq.arn
retry_policy = {
maximum_retry_attempts = 10
maximum_event_age_in_seconds = 300
}
},
{
name = "send-orders-to-fifo-sqs"
arn = aws_sqs_queue.fifo.arn
dead_letter_arn = aws_sqs_queue.dlq.arn
message_group_id = "send-orders-to-fifo-sqs"
},
{
name = "log-orders-to-cloudwatch"
arn = aws_cloudwatch_log_group.this.arn
}
]
emails = [
{
name = "process-email-with-sfn"
arn = module.step_function.this_state_machine_arn
attach_role_arn = true
},
{
name = "send-orders-to-kinesis"
arn = aws_kinesis_stream.this.arn
dead_letter_arn = aws_sqs_queue.dlq.arn
input_transformer = local.order_input_transformer
attach_role_arn = true
}
]
}
######################
# Additional policies
######################
attach_policy_json = true
policy_json = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"xray:GetSamplingStatisticSummaries"
],
"Resource": ["*"]
}
]
}
EOF
attach_policy_jsons = true
policy_jsons = [<<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"xray:*"
],
"Resource": ["*"]
}
]
}
EOF
]
number_of_policy_jsons = 1
attach_policy = true
policy = "arn:aws:iam::aws:policy/AWSXRayDaemonWriteAccess"
attach_policies = true
policies = ["arn:aws:iam::aws:policy/AWSXrayReadOnlyAccess"]
number_of_policies = 1
attach_policy_statements = true
policy_statements = {
dynamodb = {
effect = "Allow",
actions = ["dynamodb:BatchWriteItem"],
resources = ["arn:aws:dynamodb:eu-west-1:052212379155:table/Test"]
},
s3_read = {
effect = "Deny",
actions = ["s3:HeadObject", "s3:GetObject"],
resources = ["arn:aws:s3:::my-bucket/*"]
}
}
###########################
# END: Additional policies
###########################
}
locals {
order_input_transformer = {
input_paths = {
order_id = "$.detail.order_id"
}
input_template = <<EOF
{
"id": <order_id>
}
EOF
}
}
##################
# Extra resources
##################
resource "random_pet" "this" {
length = 2
}
resource "aws_kinesis_stream" "this" {
name = random_pet.this.id
shard_count = 1
}
resource "aws_sqs_queue" "queue" {
name = "${random_pet.this.id}-queue"
}
resource "aws_sqs_queue" "fifo" {
name = "${random_pet.this.id}.fifo"
fifo_queue = true
content_based_deduplication = true
}
resource "aws_sqs_queue" "dlq" {
name = "${random_pet.this.id}-dlq"
}
resource "aws_sqs_queue_policy" "queue" {
queue_url = aws_sqs_queue.queue.id
policy = data.aws_iam_policy_document.queue.json
}
data "aws_iam_policy_document" "queue" {
statement {
sid = "events-policy"
actions = ["sqs:SendMessage"]
principals {
type = "Service"
identifiers = ["events.amazonaws.com"]
}
resources = [
aws_sqs_queue.queue.arn,
aws_sqs_queue.fifo.arn
]
}
}
resource "aws_cloudwatch_log_group" "this" {
name = "/aws/events/${random_pet.this.id}"
tags = {
Name = "${random_pet.this.id}-log-group"
}
}
################
# Step Function
################
module "step_function" {
source = "terraform-aws-modules/step-functions/aws"
version = "~> 1.0"
name = random_pet.this.id
definition = jsonencode(yamldecode(templatefile("sfn.asl.yaml", {})))
trusted_entities = ["events.amazonaws.com"]
service_integrations = {
stepfunction = {
stepfunction = ["*"]
}
}
}