-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
332 lines (282 loc) · 9.3 KB
/
main.tf
File metadata and controls
332 lines (282 loc) · 9.3 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
region = var.region
access_key = var.access_key
secret_key = var.secret_key
}
# Create VPC
resource "aws_vpc" "my_vpc" {
cidr_block = "10.0.0.0/16"
}
# Internet Gateway
resource "aws_internet_gateway" "my_internet_gateway" {
vpc_id = aws_vpc.my_vpc.id
}
# Public Subnet
resource "aws_subnet" "public_subnet" {
vpc_id = aws_vpc.my_vpc.id
cidr_block = "10.0.1.0/24"
map_public_ip_on_launch = true
}
# Route Table for Public Subnet
resource "aws_route_table" "public_route_table" {
vpc_id = aws_vpc.my_vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.my_internet_gateway.id
}
}
# Associate Route Table with Subnet
resource "aws_route_table_association" "public_route_table_assoc" {
subnet_id = aws_subnet.public_subnet.id
route_table_id = aws_route_table.public_route_table.id
}
# Security Group for Lambda (allowing HTTPS traffic)
resource "aws_security_group" "lambda_sg" {
vpc_id = aws_vpc.my_vpc.id
egress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
# IAM Role
resource "aws_iam_role" "lambda_role" {
name = "lambda_role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
"Action" = "sts:AssumeRole"
"Effect" = "Allow"
"Principal" = {
"Service" = "lambda.amazonaws.com"
}
}
]
})
}
# IAM Policy for logging
resource "aws_iam_policy" "lambda_logging" {
name = "LambdaLoggingPolicy"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
]
Resource = "arn:aws:logs:*:*:*"
}
]
})
}
# Attach IAM Policy to Role
resource "aws_iam_role_policy_attachment" "lambda_logging_attach" {
role = aws_iam_role.lambda_role.name
policy_arn = aws_iam_policy.lambda_logging.arn
}
# Archive file for Lambda function
data "archive_file" "lambda_arch" {
type = "zip"
source_dir = "C:\\Users\\riosm\\Desktop\\Terraform Project\\Project(Lambda function)(3)\\App"
output_path = "C:\\Users\\riosm\\Desktop\\Terraform Project\\Project(Lambda function)(3)\\App\\lambdaFunc.zip"
}
# Lambda Function
resource "aws_lambda_function" "lambda_func" {
function_name = "lambda_func"
filename = data.archive_file.lambda_arch.output_path
role = aws_iam_role.lambda_role.arn
handler = "todolist.lambda_handler"
runtime = "python3.8"
source_code_hash = data.archive_file.lambda_arch.output_base64sha256
timeout = 20
depends_on = [aws_iam_role_policy_attachment.lambda_logging_attach]
}
#Api Gateway
resource "aws_api_gateway_rest_api" "RestAPI" {
name = "ToDoListAPI"
description = "API for managing tasks"
}
#Root Menu Method (δεν χρειάζεται resource λόγω της χρήσης του root_resource)
resource "aws_api_gateway_method" "RootGetMethod" {
rest_api_id = aws_api_gateway_rest_api.RestAPI.id
resource_id = aws_api_gateway_rest_api.RestAPI.root_resource_id
http_method = "GET"
authorization = "NONE"
}
#Root Menu Integration
resource "aws_api_gateway_integration" "RootGetIntegration" {
rest_api_id = aws_api_gateway_rest_api.RestAPI.id
resource_id = aws_api_gateway_rest_api.RestAPI.root_resource_id
http_method = aws_api_gateway_method.RootGetMethod.http_method
integration_http_method = "POST"
type = "AWS_PROXY"
uri = aws_lambda_function.lambda_func.invoke_arn
}
#Resource for /task(Add/Delete Task)
resource "aws_api_gateway_resource" "AddDelRes" {
rest_api_id = aws_api_gateway_rest_api.RestAPI.id
parent_id = aws_api_gateway_rest_api.RestAPI.root_resource_id
path_part = "tasks"
}
#Method for /task(Add)
resource "aws_api_gateway_method" "AddMethod" {
rest_api_id = aws_api_gateway_rest_api.RestAPI.id
resource_id = aws_api_gateway_resource.AddDelRes.id
http_method = "POST"
authorization = "NONE"
}
#Integration for /task(Add)
resource "aws_api_gateway_integration" "AddInteg" {
rest_api_id = aws_api_gateway_rest_api.RestAPI.id
resource_id = aws_api_gateway_resource.AddDelRes.id
http_method = aws_api_gateway_method.AddMethod.http_method
integration_http_method = "POST"
type = "AWS_PROXY"
uri = aws_lambda_function.lambda_func.invoke_arn
}
#Method for /task(Delete)
resource "aws_api_gateway_method" "DelMethod" {
rest_api_id = aws_api_gateway_rest_api.RestAPI.id
resource_id = aws_api_gateway_resource.AddDelRes.id
http_method = "DELETE"
authorization = "NONE"
}
#Integration for task(Delete)
resource "aws_api_gateway_integration" "DelInteg" {
rest_api_id = aws_api_gateway_rest_api.RestAPI.id
resource_id = aws_api_gateway_resource.AddDelRes.id
http_method = aws_api_gateway_method.DelMethod.http_method
integration_http_method = "POST"
type = "AWS_PROXY"
uri = aws_lambda_function.lambda_func.invoke_arn
}
# Resource for /tasks (List all tasks)
resource "aws_api_gateway_resource" "ListRes" {
rest_api_id = aws_api_gateway_rest_api.RestAPI.id
parent_id = aws_api_gateway_rest_api.RestAPI.root_resource_id
path_part = "tasks"
}
#Method for /tasks (List all tasks)
resource "aws_api_gateway_method" "ListMethod" {
rest_api_id = aws_api_gateway_rest_api.RestAPI.id
resource_id = aws_api_gateway_resource.ListRes.id
http_method = "GET"
authorization = "NONE"
}
#Integration for /tasks (List all tasks)
resource "aws_api_gateway_integration" "ListInteg" {
rest_api_id = aws_api_gateway_rest_api.RestAPI.id
resource_id = aws_api_gateway_resource.ListRes.id
http_method = aws_api_gateway_method.ListMethod.http_method
integration_http_method = "POST"
type = "AWS_PROXY"
uri = aws_lambda_function.lambda_func.invoke_arn
}
# Resource for /help (Display API usage information)
resource "aws_api_gateway_resource" "helpRes" {
rest_api_id = aws_api_gateway_rest_api.RestAPI.id
parent_id = aws_api_gateway_rest_api.RestAPI.root_resource_id
path_part = "help"
}
#Method for /help (Display API usage information)
resource "aws_api_gateway_method" "help_get_method" {
rest_api_id = aws_api_gateway_rest_api.RestAPI.id
resource_id = aws_api_gateway_resource.helpRes.id
http_method = "GET"
authorization = "NONE"
}
#Integration for /help(Display API usage information)
resource "aws_api_gateway_integration" "helpInteg" {
rest_api_id = aws_api_gateway_rest_api.RestAPI.id
resource_id = aws_api_gateway_resource.helpRes.id
http_method = aws_api_gateway_method.help_get_method.http_method
integration_http_method = "POST"
type = "AWS_PROXY"
uri = aws_lambda_function.lambda_func.invoke_arn
}
# Deployment for the API
resource "aws_api_gateway_deployment" "api_deployment" {
rest_api_id = aws_api_gateway_rest_api.RestAPI.id
depends_on = [
aws_api_gateway_integration.RootGetIntegration,
aws_api_gateway_integration.AddInteg,
aws_api_gateway_integration.DelInteg,
aws_api_gateway_integration.ListInteg,
aws_api_gateway_integration.helpInteg
]
}
#Stage for the API
resource "aws_api_gateway_stage" "api_stage" {
deployment_id = aws_api_gateway_deployment.api_deployment.id
rest_api_id = aws_api_gateway_rest_api.RestAPI.id
stage_name = "v1"
}
# Lambda permission for API Gateway to invoke
resource "aws_lambda_permission" "lambda_api_gateway_permission" {
statement_id = "AllowAPIGatewayInvoke"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.lambda_func.function_name
principal = "apigateway.amazonaws.com"
source_arn = "${aws_api_gateway_rest_api.RestAPI.execution_arn}/*/*"
}
#IAM Policy
resource "aws_iam_policy" "api_access_policy" {
name = "APIGatewayPublicAccessPolicy"
description = "Policy to allow public access to specific API Gateway resources"
policy = jsonencode({
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "execute-api:Invoke",
"Resource": [
"${aws_api_gateway_rest_api.RestAPI.execution_arn}/*"
]
}
]
})
}
#IAM Role
resource "aws_iam_role" "api_access_role" {
name = "APIExecutionRole"
assume_role_policy = jsonencode({
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "apigateway.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
})
}
# Resource Policy for API Gateway to allow public access
resource "aws_api_gateway_rest_api_policy" "api_gateway_policy" {
rest_api_id = aws_api_gateway_rest_api.RestAPI.id
policy = jsonencode({
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": "*",
"Action": "execute-api:Invoke",
"Resource": "${aws_api_gateway_rest_api.RestAPI.execution_arn}/*"
}
]
})
}