-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
219 lines (177 loc) · 6.61 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
# -----------------------------------------------------------------
# CREATE AWS SNS TO LAMBDA
# -----------------------------------------------------------------
terraform {
required_version = ">= 0.12"
}
provider "aws" {
region = var.aws_region
version = ">= 2.12"
}
# create if specified
resource "aws_sns_topic" "sns_log_topic" {
count = var.create_sns_topic ? 1 : 0
name = var.sns_topic_name
tags = local.common_tags
}
# retrieve topic if not created, arn referenced
data "aws_sns_topic" "sns_log_topic" {
count = var.create_sns_topic ? 0 : 1
name = var.sns_topic_name
}
# -----------------------------------------------------------------
# CREATE LAMBDA FUNCTION USING ZIP FILE
# -----------------------------------------------------------------
# make zip
data "archive_file" "lambda_function" {
type = "zip"
source_file = "${path.module}/function/lambda_function.py"
output_path = "${path.module}/lambda.zip"
}
# create lambda using function
resource "aws_lambda_function" "sns_lambda" {
function_name = "${var.lambda_func_name}"
description = "lambda function triggered from sns"
filename = "${path.module}/lambda.zip"
source_code_hash = data.archive_file.lambda_function.output_base64sha256
publish = var.lambda_publish_func ? true : false
role = aws_iam_role.iam_for_lambda_with_sns.arn
runtime = var.lambda_runtime
handler = "lambda_function.lambda_handler"
timeout = var.lambda_timeout
memory_size = var.lambda_mem_size
tags = local.common_tags
}
# -----------------------------------------------------------------
# SUBSCRIBE LAMBDA FUNCTION TO SNS TOPIC
# -----------------------------------------------------------------
resource "aws_sns_topic_subscription" "lambda" {
topic_arn = var.create_sns_topic ? aws_sns_topic.sns_log_topic[0].arn : data.aws_sns_topic.sns_log_topic[0].arn
protocol = "lambda"
endpoint = var.lambda_publish_func ? aws_lambda_function.sns_lambda.qualified_arn : aws_lambda_function.sns_lambda.arn
}
# -----------------------------------------------------------------
# ENABLE SNS TOPIC AS LAMBDA FUNCTION TRIGGER
# -----------------------------------------------------------------
resource "aws_lambda_permission" "lambda_with_sns" {
statement_id = "AllowExecutionFromSNS"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.sns_lambda.function_name
principal = "sns.amazonaws.com"
source_arn = var.create_sns_topic ? aws_sns_topic.sns_log_topic[0].arn : data.aws_sns_topic.sns_log_topic[0].arn
qualifier = var.lambda_publish_func ? aws_lambda_function.sns_lambda.version : null
}
# -------------------------------------------------------------------------------------
# CREATE IAM ROLE AND POLICIES FOR LAMBDA FUNCTION
# -------------------------------------------------------------------------------------
# Create IAM role
resource "aws_iam_role" "iam_for_lambda_with_sns" {
name = "lambda-${lower(var.lambda_func_name)}-${var.sns_topic_name}"
assume_role_policy = data.aws_iam_policy_document.iam_for_lambda_with_sns.json
tags = local.common_tags
}
# Add base Lambda Execution policy
resource "aws_iam_role_policy" "lambda_cloudwatch_logs_polcy" {
name = "lambda-${lower(var.lambda_func_name)}-policy-${var.sns_topic_name}"
role = aws_iam_role.iam_for_lambda_with_sns.id
policy = data.aws_iam_policy_document.lambda_cloudwatch_logs_policy.json
}
# JSON POLICY - assume role
data "aws_iam_policy_document" "iam_for_lambda_with_sns" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
# JSON POLICY - base Lambda Execution policy
data "aws_iam_policy_document" "lambda_cloudwatch_logs_policy" {
statement {
actions = [
"logs:CreateLogStream",
"logs:CreateLogGroup",
"logs:PutLogEvents",
"logs:DeleteLogStream",
"logs:DeleteLogGroup"
]
resources = ["*"]
}
}
# Cloudwatch event rule to capture console sign in
resource "aws_cloudwatch_event_rule" "console-access" {
name = "capture-aws-sign-in"
tags = local.common_tags
description = "Capture each AWS Console Sign In"
event_pattern = <<PATTERN
{
"detail-type": [
"AWS Console Sign In via CloudTrail"
]
}
PATTERN
}
# Cloudwatch target for console sign in
resource "aws_cloudwatch_event_target" "console-access" {
rule = "${aws_cloudwatch_event_rule.console-access.name}"
target_id = "SendToSNS"
arn = var.create_sns_topic ? aws_sns_topic.sns_log_topic[0].arn : data.aws_sns_topic.sns_log_topic[0].arn
}
# Cloudwatch event rule to capture ec2-running status
resource "aws_cloudwatch_event_rule" "ec2-running-status" {
name = "capture-ec2-running-status"
tags = local.common_tags
description = "Capture each ec2 running status in the region"
event_pattern = <<PATTERN
{
"source": [ "aws.ec2" ],
"detail-type": [ "EC2 Instance State-change Notification" ],
"detail": {
"state": [ "running" ]
}
}
PATTERN
}
# Cloudwatch target for ec2-running status
resource "aws_cloudwatch_event_target" "ec2-running-status" {
rule = "${aws_cloudwatch_event_rule.ec2-running-status.name}"
target_id = "SendToSNS"
arn = var.create_sns_topic ? aws_sns_topic.sns_log_topic[0].arn : data.aws_sns_topic.sns_log_topic[0].arn
}
# Cloudwatch event rule to capture ec2-terminated status
resource "aws_cloudwatch_event_rule" "ec2-terminated-status" {
name = "capture-ec2-terminated-status"
tags = local.common_tags
description = "Capture each ec2 terminated status in the region"
event_pattern = <<PATTERN
{
"source": [ "aws.ec2" ],
"detail-type": [ "EC2 Instance State-change Notification" ],
"detail": {
"state": [ "terminated" ]
}
}
PATTERN
}
# Cloudwatch target for ec2-terminated status
resource "aws_cloudwatch_event_target" "ec2-terminated-status" {
rule = "${aws_cloudwatch_event_rule.ec2-terminated-status.name}"
target_id = "SendToSNS"
arn = var.create_sns_topic ? aws_sns_topic.sns_log_topic[0].arn : data.aws_sns_topic.sns_log_topic[0].arn
}
resource "aws_sns_topic_policy" "sns_topic_policy" {
arn = var.create_sns_topic ? aws_sns_topic.sns_log_topic[0].arn : data.aws_sns_topic.sns_log_topic[0].arn
policy = "${data.aws_iam_policy_document.sns_topic_policy.json}"
}
data "aws_iam_policy_document" "sns_topic_policy" {
statement {
effect = "Allow"
actions = ["SNS:Publish"]
principals {
type = "Service"
identifiers = ["events.amazonaws.com"]
}
resources = [var.create_sns_topic ? aws_sns_topic.sns_log_topic[0].arn : data.aws_sns_topic.sns_log_topic[0].arn]
}
}