-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
183 lines (153 loc) · 5.03 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
locals {
s3_bucket_name = "${var.name_prefix}-bucket-${random_string.random.id}"
aws_dynamodb_table_name = "${var.name_prefix}-serverlessddb-${random_string.random.id}"
aws_lambda_function_name = "${var.name_prefix}-serverlesslambda-${random_string.random.id}"
aws_iam_role_name = "${var.name_prefix}-LambdaDdbPostRole-${random_string.random.id}"
aws_iam_policy_name = "${var.name_prefix}-LambdaDdbPostPolicy-${random_string.random.id}"
aws_apigatewayv2_api_name = "${var.name_prefix}-http-api-${random_string.random.id}"
}
resource "random_string" "random" {
length = 4
special = false
lower = true
upper = false
}
resource "aws_dynamodb_table" "table" {
name = local.aws_dynamodb_table_name
billing_mode = "PAY_PER_REQUEST"
hash_key = "year"
range_key = "title"
attribute {
name = "year"
type = "N"
}
attribute {
name = "title"
type = "S"
}
}
#========================================================================
// lambda setup
#========================================================================
resource "aws_s3_bucket" "lambda_bucket" {
bucket = local.s3_bucket_name
force_destroy = true
}
data "archive_file" "lambda_zip" {
type = "zip"
source_dir = "${path.module}/src"
output_path = "${path.module}/src.zip"
}
//Define lambda function
resource "aws_lambda_function" "http_api_lambda" {
filename = data.archive_file.lambda_zip.output_path
function_name = local.aws_lambda_function_name
description = "Lambda function to write to dynamodb"
runtime = "python3.8"
handler = "app.lambda_handler"
source_code_hash = data.archive_file.lambda_zip.output_base64sha256
role = aws_iam_role.lambda_exec.arn
environment {
variables = {
DDB_TABLE = aws_dynamodb_table.table.name
}
}
}
resource "aws_iam_role" "lambda_exec" {
name = local.aws_iam_role_name
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Action = "sts:AssumeRole"
Effect = "Allow"
Sid = ""
Principal = {
Service = "lambda.amazonaws.com"
}
}
]
})
}
resource "aws_iam_policy" "lambda_exec_role" {
name = local.aws_iam_policy_name
policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"dynamodb:GetItem",
"dynamodb:PutItem",
"dynamodb:UpdateItem"
],
"Resource": "arn:aws:dynamodb:*:*:table/${aws_dynamodb_table.table.name}"
},
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "*"
}
]
}
POLICY
}
resource "aws_iam_role_policy_attachment" "lambda_policy" {
role = aws_iam_role.lambda_exec.name
policy_arn = aws_iam_policy.lambda_exec_role.arn
}
#========================================================================
// API Gateway section
#========================================================================
resource "aws_apigatewayv2_api" "http_api" {
name = local.aws_apigatewayv2_api_name
protocol_type = "HTTP"
}
resource "aws_apigatewayv2_stage" "default" {
api_id = aws_apigatewayv2_api.http_api.id
name = "$default"
auto_deploy = true
access_log_settings {
destination_arn = aws_cloudwatch_log_group.api_access_logs.arn
format = jsonencode({
requestId = "$context.requestId"
sourceIp = "$context.identity.sourceIp"
requestTime = "$context.requestTime"
protocol = "$context.protocol"
httpMethod = "$context.httpMethod"
resourcePath = "$context.resourcePath"
routeKey = "$context.routeKey"
status = "$context.status"
responseLength = "$context.responseLength"
integrationErrorMessage = "$context.integrationErrorMessage"
}
)
}
depends_on = [aws_cloudwatch_log_group.api_access_logs]
}
resource "aws_apigatewayv2_integration" "apigw_lambda" {
api_id = aws_apigatewayv2_api.http_api.id
integration_uri = aws_lambda_function.http_api_lambda.invoke_arn
integration_type = "AWS_PROXY"
integration_method = "POST"
}
resource "aws_apigatewayv2_route" "post" {
api_id = aws_apigatewayv2_api.http_api.id
route_key = "POST /movies"
target = "integrations/${aws_apigatewayv2_integration.apigw_lambda.id}"
}
resource "aws_cloudwatch_log_group" "api_access_logs" {
name = "/aws/api_gw/${aws_apigatewayv2_api.http_api.name}"
retention_in_days = 7
}
resource "aws_lambda_permission" "api_gw" {
statement_id = "AllowExecutionFromAPIGateway"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.http_api_lambda.function_name
principal = "apigateway.amazonaws.com"
source_arn = "${aws_apigatewayv2_api.http_api.execution_arn}/*/*"
}