-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
217 lines (186 loc) · 6.02 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
terraform {
backend "remote" {
organization = "chxnedu-crc"
workspaces {
name = "Prod-Env-Backend"
}
}
}
provider "aws" {
region = "us-east-1"
}
resource "aws_dynamodb_table" "VisitorCount" {
name = "VisitorCounterDB"
hash_key = "Version"
attribute {
name = "Version"
type = "N"
}
billing_mode = "PROVISIONED"
read_capacity = 1
write_capacity = 1
}
resource "aws_appautoscaling_target" "dynamodb_table_read_target" {
max_capacity = 10
min_capacity = 1
resource_id = "table/${aws_dynamodb_table.VisitorCount.name}"
scalable_dimension = "dynamodb:table:ReadCapacityUnits"
service_namespace = "dynamodb"
}
resource "aws_appautoscaling_policy" "dynamodb_table_read_policy" {
name = "DynamoDBReadCapacityUtilization:${aws_appautoscaling_target.dynamodb_table_read_target.resource_id}"
policy_type = "TargetTrackingScaling"
resource_id = aws_appautoscaling_target.dynamodb_table_read_target.resource_id
scalable_dimension = aws_appautoscaling_target.dynamodb_table_read_target.scalable_dimension
service_namespace = aws_appautoscaling_target.dynamodb_table_read_target.service_namespace
target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "DynamoDBReadCapacityUtilization"
}
target_value = 70
}
}
resource "aws_appautoscaling_target" "dynamodb_table_write_target" {
max_capacity = 10
min_capacity = 1
resource_id = "table/${aws_dynamodb_table.VisitorCount.name}"
scalable_dimension = "dynamodb:table:WriteCapacityUnits"
service_namespace = "dynamodb"
}
resource "aws_appautoscaling_policy" "dynamodb_table_write_policy" {
name = "DynamoDBWriteCapacityUtilization:${aws_appautoscaling_target.dynamodb_table_write_target.resource_id}"
policy_type = "TargetTrackingScaling"
resource_id = aws_appautoscaling_target.dynamodb_table_write_target.resource_id
scalable_dimension = aws_appautoscaling_target.dynamodb_table_write_target.scalable_dimension
service_namespace = aws_appautoscaling_target.dynamodb_table_write_target.service_namespace
target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "DynamoDBWriteCapacityUtilization"
}
target_value = 70
}
}
resource "aws_dynamodb_table_item" "Count" {
table_name = aws_dynamodb_table.VisitorCount.name
hash_key = aws_dynamodb_table.VisitorCount.hash_key
item = <<-EOS
{
"Version": { "N": "1" },
"TotalCount": { "N": "0" }
}
EOS
lifecycle {
ignore_changes = [
item,
]
}
}
resource "aws_iam_role" "lambda_role" {
name = "Visitor_Count_Lambda_Role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
data "aws_iam_policy" "BasicExecutionRole" {
arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
data "aws_iam_policy" "DynamodbFullAccess" {
arn = "arn:aws:iam::aws:policy/AmazonDynamoDBFullAccess"
}
resource "aws_iam_role_policy_attachment" "attach_role_to_policy" {
role = aws_iam_role.lambda_role.name
policy_arn = data.aws_iam_policy.BasicExecutionRole.arn
}
resource "aws_iam_role_policy_attachment" "attach_dynamo_role_to_policy" {
role = aws_iam_role.lambda_role.name
policy_arn = data.aws_iam_policy.DynamodbFullAccess.arn
}
data "archive_file" "zip_the_code" {
type = "zip"
source_dir = "${path.module}/python/"
output_path = "${path.module}/python/lambda_python.zip"
}
resource "aws_lambda_function" "UpdateVisitorCount" {
function_name = "UpdateCount"
role = aws_iam_role.lambda_role.arn
architectures = ["x86_64"]
filename = "${path.module}/python/lambda_python.zip"
handler = "lambda_code.update"
runtime = "python3.9"
depends_on = [
aws_dynamodb_table_item.Count,
aws_iam_role_policy_attachment.attach_role_to_policy,
aws_iam_role_policy_attachment.attach_dynamo_role_to_policy
]
}
resource "aws_lambda_permission" "lambda-permission" {
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.UpdateVisitorCount.function_name
principal = "apigateway.amazonaws.com"
source_arn = "${aws_apigatewayv2_api.crc-api.execution_arn}/*/*/update_count"
}
resource "aws_apigatewayv2_api" "crc-api" {
name = "CRC-API"
protocol_type = "HTTP"
cors_configuration {
allow_origins = ["https://resume.chxnedu.online", "https://chxnedu.online"]
}
depends_on = [
aws_lambda_function.UpdateVisitorCount
]
}
resource "aws_apigatewayv2_integration" "lambda-integration" {
api_id = aws_apigatewayv2_api.crc-api.id
integration_type = "AWS_PROXY"
connection_type = "INTERNET"
description = "Integrating my lambda function to API"
integration_method = "POST"
integration_uri = aws_lambda_function.UpdateVisitorCount.invoke_arn
payload_format_version = "2.0"
}
resource "aws_apigatewayv2_route" "lambda-route" {
api_id = aws_apigatewayv2_api.crc-api.id
route_key = "ANY /update_count"
target = "integrations/${aws_apigatewayv2_integration.lambda-integration.id}"
}
resource "aws_apigatewayv2_stage" "lambda-stage" {
api_id = aws_apigatewayv2_api.crc-api.id
name = "$default"
auto_deploy = true
}
output "api_endpoint" {
value = aws_apigatewayv2_api.crc-api.api_endpoint
}
resource "aws_sns_topic" "lambda_alert" {
name = "LAMBDA_ALERT"
}
resource "aws_sns_topic_subscription" "email_alert" {
topic_arn = aws_sns_topic.lambda_alert.arn
protocol = "email"
endpoint = "ojichinedu4@gmail.com"
}
resource "aws_cloudwatch_metric_alarm" "lambda_error" {
alarm_name = "LAMBDA_ERROR"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "5"
metric_name = "Errors"
namespace = "AWS/Lambda"
period = "300"
statistic = "Average"
threshold = "1"
actions_enabled = true
alarm_actions = [ aws_sns_topic.lambda_alert.arn ]
alarm_description = "Alerts me when Lambda has an error"
}