This repository was archived by the owner on Apr 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.tf
158 lines (146 loc) · 4.35 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
########################
#### IAM for Lambda ####
########################
data "aws_iam_policy_document" "AWSLambdaTrustPolicy" {
statement {
actions = ["sts:AssumeRole"]
effect = "Allow"
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
resource "aws_iam_role" "lambda_role" {
name = var.lambda_role_name
assume_role_policy = data.aws_iam_policy_document.AWSLambdaTrustPolicy.json
description = "role which is required for nlb to alb connector lambda"
}
resource "aws_iam_policy" "lambda_policy" {
name = var.lambda_policy_name
description = "policy which is required for nlb to alb connector lambda"
// to check of createbucket nodig is?
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
"Sid" : "LambdaLogging",
"Effect" : "Allow",
"Action" : [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource" : [
"arn:aws:logs:*:*:*"
]
},
{
"Sid" : "S3Objects",
"Action" : [
"s3:Get*",
"s3:PutObject"
],
"Effect" : "Allow",
"Resource" : "${aws_s3_bucket.bucket.arn}/*"
},
{
"Sid" : "S3List",
"Action" : [
"s3:ListBucket"
],
"Effect" : "Allow",
"Resource" : aws_s3_bucket.bucket.arn
},
{
"Sid" : "S3ListAll",
"Action" : [
"s3:ListAllMyBuckets"
],
"Effect" : "Allow",
"Resource" : "*"
},
{
"Sid" : "ELB",
"Action" : [
"elasticloadbalancing:Describe*",
],
"Effect" : "Allow",
"Resource" : "*"
},
{
"Sid" : "ELBTG",
"Action" : [
"elasticloadbalancing:RegisterTargets",
"elasticloadbalancing:DeregisterTargets"
],
"Effect" : "Allow",
"Resource" : var.nlb_target_arn
},
{
"Sid" : "CW",
"Action" : [
"cloudwatch:putMetricData"
],
"Effect" : "Allow",
"Resource" : "*"
}
]
})
}
resource "aws_iam_role_policy_attachment" "lambda_role_policy_attachment" {
role = aws_iam_role.lambda_role.name
policy_arn = aws_iam_policy.lambda_policy.arn
}
################
#### Lambda ####
################
resource "aws_lambda_function" "lambda" {
filename = "${path.module}/nlb_alb_connector_lambda.zip"
function_name = var.lambda_function_name
role = aws_iam_role.lambda_role.arn
handler = "nlb_alb_connector_lambda.lambda_handler"
source_code_hash = filebase64sha256("${path.module}/nlb_alb_connector_lambda.zip")
#source_code_hash = filebase64sha256("./nlb_alb_connector_lambda.zip")
runtime = "python3.8"
timeout = 300
environment {
variables = {
ALB_DNS_NAME = var.alb_dns_name
ALB_LISTENER = var.alb_listener
INVOCATIONS_BEFORE_DEREGISTRATION = 3
MAX_LOOKUP_PER_INVOCATION = 50
NLB_TG_ARN = var.nlb_target_arn
S3_BUCKET = aws_s3_bucket.bucket.id
CW_METRIC_FLAG_IP_COUNT = true
}
}
}
###################
#### S3 Bucket ####
###################
resource "aws_s3_bucket" "bucket" {
bucket = var.bucket_name
acl = "private"
force_destroy = var.bucket_destroy
}
#############################################
#### CloudWatch Event to trigger Lambda ####
#############################################
resource "aws_cloudwatch_event_rule" "every_minute" {
name = var.cloudwatch_event_rule_name == "" ? "every-minute" : var.cloudwatch_event_rule_name
description = "Fires every minute"
schedule_expression = "rate(1 minute)"
}
resource "aws_lambda_permission" "allow_cloudwatch_to_call_lambda" {
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.lambda.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.every_minute.arn
}
resource "aws_cloudwatch_event_target" "check_connection_every_minute" {
rule = aws_cloudwatch_event_rule.every_minute.name
target_id = var.lambda_function_name
arn = aws_lambda_function.lambda.arn
}