-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
117 lines (92 loc) · 2.8 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
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.16"
}
}
required_version = ">= 1.2.0"
}
provider "aws" {
region = "eu-central-1"
}
data "aws_iam_policy_document" "assume_role" {
statement {
effect = "Allow"
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
actions = ["sts:AssumeRole"]
}
}
data "aws_iam_policy_document" "lambda_policy_document" {
version = "2012-10-17"
statement {
actions = ["dynamodb:PutItem", "dynamodb:UpdateItem", "dynamodb:BatchWriteItem"]
effect = "Allow"
resources = [aws_dynamodb_table.ip_table.arn]
}
}
resource "aws_iam_role" "iam_for_lambda" {
name = "iam_for_lambda"
assume_role_policy = data.aws_iam_policy_document.assume_role.json
}
resource "aws_iam_policy" "lambda_policy" {
name = "lambda-dynamodb-policy"
policy = data.aws_iam_policy_document.lambda_policy_document.json
}
resource "aws_iam_role_policy_attachment" "lambda_attachment" {
policy_arn = aws_iam_policy.lambda_policy.arn
role = aws_iam_role.iam_for_lambda.name
}
data "archive_file" "lambda_zip_file" {
type = "zip"
source_dir = "${path.module}/dist"
output_path = "${path.module}/lambda.zip"
}
resource "aws_lambda_function" "ip_lambda" {
# If the file is not in the current working directory you will need to include a
# path.module in the filename.
filename = data.archive_file.lambda_zip_file.output_path
function_name = "ip_recorder"
role = aws_iam_role.iam_for_lambda.arn
handler = "index.handler"
source_code_hash = data.archive_file.lambda_zip_file.output_base64sha256
runtime = "nodejs18.x"
}
resource "aws_apigatewayv2_api" "main" {
name = "main"
protocol_type = "HTTP"
}
resource "aws_apigatewayv2_stage" "default" {
api_id = aws_apigatewayv2_api.main.id
name = "$default"
auto_deploy = true
}
resource "aws_apigatewayv2_integration" "lambda_handler" {
api_id = aws_apigatewayv2_api.main.id
integration_type = "AWS_PROXY"
integration_uri = aws_lambda_function.ip_lambda.invoke_arn
}
resource "aws_apigatewayv2_route" "get_handler" {
api_id = aws_apigatewayv2_api.main.id
route_key = "GET /"
target = "integrations/${aws_apigatewayv2_integration.lambda_handler.id}"
}
resource "aws_lambda_permission" "api_gw" {
statement_id = "AllowExecutionFromAPIGateway"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.ip_lambda.function_name
principal = "apigateway.amazonaws.com"
source_arn = "${aws_apigatewayv2_api.main.execution_arn}/*/*"
}
resource "aws_dynamodb_table" "ip_table" {
name = "ip-table"
billing_mode = "PAY_PER_REQUEST"
hash_key = "id"
attribute {
name = "id"
type = "S"
}
}