-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
149 lines (131 loc) · 4.07 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
resource "aws_sns_topic" "this" {
name = "${var.name}-slack-notify"
display_name = "Autoscaling Notifications to Slack"
}
resource "aws_sns_topic_policy" "sns_topic_policy" {
count = var.aux_sns_senders_arn_list == null ? 0 : 1
arn = aws_sns_topic.this.arn
policy = data.aws_iam_policy_document.sns_topic_policy[count.index].json
}
# Access from a domestic account won't affected by the policy
data "aws_iam_policy_document" "sns_topic_policy" {
// Keep default policy on cloud untouched. Remove it after Automation will be migrated to another account on all environments
count = var.aux_sns_senders_arn_list == null ? 0 : 1
// TODO: make this statement dependent on var.aux_sns_senders_arn_list after Automation will be migrated to another account on all environments
statement {
sid = "AllowPublishFromAnotherAccounts"
principals {
type = "AWS"
identifiers = ["*"]
}
effect = "Allow"
actions = ["SNS:Publish"]
resources = [aws_sns_topic.this.arn]
condition {
test = "ArnLike"
variable = "aws:SourceArn"
values = var.aux_sns_senders_arn_list
}
}
// TODO: make this statement mandatory after Automation will be migrated to another account on all environments
// When you create a CW Events rule with Amazon SNS as the target, CW Events adds the permission to your Amazon SNS
// topic on your behalf, so we should do it explicitly here
statement {
sid = "AllowPublishFromCloudWatchEventsRules"
principals {
type = "Service"
identifiers = [
"events.amazonaws.com",
"cloudwatch.amazonaws.com",
]
}
effect = "Allow"
actions = ["SNS:Publish"]
resources = [aws_sns_topic.this.arn]
}
}
resource "aws_iam_role" "this" {
name = "${var.name}-slack-notify"
assume_role_policy = <<EOT
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOT
}
resource "aws_iam_role_policy" "this" {
name = "${var.name}-slack-notify"
role = aws_iam_role.this.id
policy = <<EOT
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
"ecs:DescribeServices"
],
"Effect": "Allow",
"Resource": "*"
}
]
}
EOT
}
resource "aws_cloudwatch_log_group" "this" {
name = "/aws/lambda/${local.lambda_slack_notify_name}"
retention_in_days = 7
}
data "archive_file" "this" {
source_file = "${path.module}/source/lambda_function/main.py"
output_path = "${local.lambda_slack_notify_name}.zip"
output_file_mode = "0666"
type = "zip"
}
resource "aws_lambda_function" "this" {
count = var.enable_lambda ? 1 : 0
function_name = local.lambda_slack_notify_name
role = aws_iam_role.this.arn
handler = "main.lambda_handler"
runtime = var.runtime
timeout = var.timeout
filename = data.archive_file.this.output_path
source_code_hash = data.archive_file.this.output_base64sha256
layers = var.layers
environment {
variables = {
SLACK_CHANNEL = var.slack_channel
SLACK_USERNAME = var.slack_username
SLACK_WEBHOOK = var.slack_webhook
STACK_NAME = var.name
}
}
depends_on = [
aws_cloudwatch_log_group.this
]
}
resource "aws_lambda_permission" "this" {
count = var.enable_lambda ? 1 : 0
statement_id = "AllowExecutionFromSNS"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.this[0].function_name
principal = "sns.amazonaws.com"
source_arn = aws_sns_topic.this.arn
}
resource "aws_sns_topic_subscription" "lambda-sns" {
count = var.enable_lambda ? 1 : 0
topic_arn = aws_sns_topic.this.arn
protocol = "lambda"
endpoint = aws_lambda_function.this[0].arn
}