-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
129 lines (110 loc) · 2.63 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
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "4.58.0"
}
}
}
provider "aws" {
region = var.aws_region
}
locals {
tag = var.event_source
}
module "log_groups" {
for_each = toset(["dev", "prod"])
source = "./infra/log-group"
environment = each.key
event_source = var.event_source
log_retention_in_days = var.log_retention_in_days
tag = local.tag
aws_region = var.aws_region
}
module "eventbridge" {
source = "terraform-aws-modules/eventbridge/aws"
bus_name = var.event_source
attach_cloudwatch_policy = true
cloudwatch_target_arns = [module.log_groups["dev"].event_log_group_arn, module.log_groups["prod"].event_log_group_arn]
attach_sqs_policy = true
sqs_target_arns = [
aws_sqs_queue.dlq.arn
]
tags = {
Name = local.tag
}
rules = {
dev = {
description = "dev logs"
event_pattern = jsonencode({ "source" : [{
"prefix" : module.log_groups["dev"].event_source_name
}] })
}
prod = {
description = "prod logs"
event_pattern = jsonencode({ "source" : [{
"prefix" : module.log_groups["prod"].event_source_name
}] })
}
}
targets = {
dev = [
{
name = "send-logs-to-cloudwatch-dev"
arn = module.log_groups["dev"].event_log_group_arn,
dead_letter_arn = aws_sqs_queue.dlq.arn
}
]
prod = [
{
name = "send-logs-to-cloudwatch-prod"
arn = module.log_groups["prod"].event_log_group_arn,
dead_letter_arn = aws_sqs_queue.dlq.arn
}
]
}
}
data "aws_iam_policy_document" "eventbridge" {
statement {
effect = "Allow"
actions = [
"logs:CreateLogStream"
]
resources = [
"${module.log_groups["dev"].event_log_group_arn}:*",
"${module.log_groups["prod"].event_log_group_arn}:*"
]
principals {
type = "Service"
identifiers = [
"events.amazonaws.com"
]
}
}
statement {
effect = "Allow"
actions = [
"logs:PutLogEvents"
]
resources = [
"${module.log_groups["dev"].event_log_group_arn}:*:*",
"${module.log_groups["prod"].event_log_group_arn}:*:*"
]
principals {
type = "Service"
identifiers = [
"events.amazonaws.com"
]
}
}
}
resource "aws_cloudwatch_log_resource_policy" "eventbridge" {
policy_document = data.aws_iam_policy_document.eventbridge.json
policy_name = "${var.event_source}-resource-policy"
}
resource "aws_sqs_queue" "dlq" {
name = "${var.event_source}-dlq"
tags = {
Name = local.tag
}
}