-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmain.tf
187 lines (159 loc) · 4.89 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
module "subscription_label" {
source = "cloudposse/label/null"
version = "0.25.0"
attributes = ["pipeline", "updates"]
context = module.this.context
}
resource "aws_sns_topic" "pipeline_updates" {
# tfsec:ignore:AWS016
name = module.subscription_label.id
tags = module.this.tags
}
resource "aws_sns_topic_subscription" "pipeline_updates" {
topic_arn = aws_sns_topic.pipeline_updates.arn
protocol = "lambda"
endpoint = aws_lambda_function.pipeline_notification.arn
}
resource "aws_cloudwatch_event_rule" "pipeline_updates" {
name = "${module.subscription_label.id}-pipeline"
tags = module.this.tags
event_pattern = jsonencode({
source = ["aws.codepipeline"]
detail-type = [
"CodePipeline Pipeline Execution State Change",
]
detail = {
pipeline = var.codepipelines.*.name
state = [for id in var.pipeline_event_type_ids : upper(id)]
}
})
}
resource "aws_cloudwatch_event_target" "pipeline_updates" {
rule = aws_cloudwatch_event_rule.pipeline_updates.name
arn = aws_sns_topic.pipeline_updates.arn
target_id = "${module.subscription_label.id}-pipeline"
}
resource "aws_cloudwatch_event_rule" "approval_updates" {
name = "${module.subscription_label.id}-approval"
tags = module.this.tags
event_pattern = jsonencode({
source = ["aws.codepipeline"]
detail-type = [
"CodePipeline Action Execution State Change",
]
detail = {
pipeline = var.codepipelines.*.name
state = [for id in var.approval_event_type_ids : upper(id)]
type = {
category = ["Approval"]
}
}
})
}
resource "aws_cloudwatch_event_target" "approval_updates" {
rule = aws_cloudwatch_event_rule.approval_updates.name
arn = aws_sns_topic.pipeline_updates.arn
}
resource "aws_sns_topic_policy" "pipeline_updates" {
arn = aws_sns_topic.pipeline_updates.arn
policy = data.aws_iam_policy_document.pipeline_updates_policy.json
}
data "aws_iam_policy_document" "pipeline_updates_policy" {
statement {
sid = "sns-publish"
effect = "Allow"
actions = ["SNS:Publish"]
resources = [aws_sns_topic.pipeline_updates.arn]
principals {
type = "Service"
identifiers = ["events.amazonaws.com"]
}
}
}
data "archive_file" "notifier_package" {
type = "zip"
source_file = "${path.module}/lambdas/notifier/notifier.py"
output_file_mode = "0666"
output_path = "${path.module}/lambdas/notifier.zip"
}
resource "aws_lambda_function" "pipeline_notification" {
filename = data.archive_file.notifier_package.output_path
function_name = module.this.id
role = aws_iam_role.pipeline_notification.arn
runtime = var.lambda_runtime
source_code_hash = data.archive_file.notifier_package.output_base64sha256
handler = "notifier.handler"
timeout = 10
environment {
variables = {
SLACK_WEBHOOK_URL = var.slack_url
SLACK_CHANNEL = var.slack_channel
SLACK_USERNAME = var.slack_username
SLACK_EMOJI = var.slack_emoji
ENVIRONMENT = var.stage
}
}
tags = module.this.tags
depends_on = [
aws_iam_role_policy_attachment.pipeline_notification,
data.archive_file.notifier_package,
]
}
resource "aws_lambda_permission" "pipeline_notification" {
statement_id = "AllowExecutionFromSNS"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.pipeline_notification.function_name
principal = "sns.amazonaws.com"
source_arn = aws_sns_topic.pipeline_updates.arn
}
data "aws_iam_policy_document" "pipeline_notification_role" {
statement {
actions = ["sts:AssumeRole"]
principals {
identifiers = ["lambda.amazonaws.com"]
type = "Service"
}
}
}
resource "aws_iam_role" "pipeline_notification" {
name = "${module.this.id}-pipeline-notification"
assume_role_policy = data.aws_iam_policy_document.pipeline_notification_role.json
tags = module.this.tags
}
data "aws_iam_policy_document" "pipeline_notification" {
statement {
actions = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
]
resources = [
"arn:aws:logs:*:*:*"
]
}
statement {
actions = [
"codepipeline:GetPipelineExecution"
]
resources = var.codepipelines.*.arn
}
statement {
actions = [
"iam:PassRole"
]
resources = [
aws_iam_role.pipeline_notification.arn
]
}
}
resource "aws_iam_policy" "pipeline_notification" {
name = "${module.this.id}-pipeline-notification"
path = "/"
description = "IAM policy for the Slack notification lambda"
policy = data.aws_iam_policy_document.pipeline_notification.json
tags = module.this.tags
}
resource "aws_iam_role_policy_attachment" "pipeline_notification" {
role = aws_iam_role.pipeline_notification.name
policy_arn = aws_iam_policy.pipeline_notification.arn
}