-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
136 lines (117 loc) · 3.37 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
provider "aws" {
region = var.region
}
resource "aws_iam_role" "lambda_exec_role" {
name = "lambda_exec_role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow"
}
]
}
EOF
}
resource "aws_iam_role_policy" "lambda_policy" {
name = "lambda_dynamodb_policy"
role = aws_iam_role.lambda_exec_role.id
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = [
"dynamodb:PutItem",
"dynamodb:GetItem",
"dynamodb:UpdateItem",
"dynamodb:DeleteItem"
]
Effect = "Allow"
Resource = aws_dynamodb_table.users_table.arn
},
{
Action = "logs:CreateLogGroup"
Effect = "Allow"
Resource = "arn:aws:logs:*:*:*"
},
{
Action = [
"logs:CreateLogStream",
"logs:PutLogEvents"
]
Effect = "Allow"
Resource = "arn:aws:logs:*:*:log-group:/aws/lambda/*:*"
}
]
})
}
resource "aws_lambda_function" "users_lambda" {
function_name = "users_lambda"
role = aws_iam_role.lambda_exec_role.arn
handler = "lambda_function.lambda_handler"
runtime = "python3.8"
filename = "${path.module}/lambda.zip" # Pointing to the local file
environment {
variables = {
table_name = aws_dynamodb_table.users_table.name
}
}
}
resource "aws_dynamodb_table" "users_table" {
name = "users_table"
billing_mode = "PAY_PER_REQUEST"
hash_key = "user_id"
attribute {
name = "user_id"
type = "S"
}
}
# API Gateway setup
resource "aws_api_gateway_rest_api" "users_api" {
name = "users_api"
}
resource "aws_api_gateway_resource" "users_resource" {
rest_api_id = aws_api_gateway_rest_api.users_api.id
parent_id = aws_api_gateway_rest_api.users_api.root_resource_id
path_part = "users"
}
resource "aws_api_gateway_method" "users_method" {
rest_api_id = aws_api_gateway_rest_api.users_api.id
resource_id = aws_api_gateway_resource.users_resource.id
http_method = "POST"
authorization = "NONE"
}
resource "aws_api_gateway_integration" "users_integration" {
rest_api_id = aws_api_gateway_rest_api.users_api.id
resource_id = aws_api_gateway_resource.users_resource.id
http_method = aws_api_gateway_method.users_method.http_method
integration_http_method = "POST"
type = "AWS_PROXY"
uri = aws_lambda_function.users_lambda.invoke_arn
}
resource "aws_lambda_permission" "allow_api_gateway" {
statement_id = "AllowExecutionFromAPIGateway"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.users_lambda.arn
principal = "apigateway.amazonaws.com"
source_arn = "${aws_api_gateway_rest_api.users_api.execution_arn}/*/*"
}
resource "aws_api_gateway_deployment" "users_deployment" {
depends_on = [aws_api_gateway_integration.users_integration]
rest_api_id = aws_api_gateway_rest_api.users_api.id
stage_name = "prod"
}
output "api_gateway_url" {
value = "${aws_api_gateway_deployment.users_deployment.invoke_url}/users"
}
output "lambda_function_arn" {
value = aws_lambda_function.users_lambda.arn
}
output "dynamodb_table_name" {
value = aws_dynamodb_table.users_table.name
}