-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
155 lines (127 loc) · 4.29 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
resource "aws_dynamodb_table" "minimal_backend_table" {
name = "minimal-backend-table"
billing_mode = "PROVISIONED"
read_capacity = 5
write_capacity = 5
hash_key = "id"
attribute {
name = "id"
type = "N"
}
}
data "archive_file" "backend_function_package" {
type = "zip"
source_file = "./backend_lambda_function.py"
output_path = "./backend_lambda_function.zip"
}
resource "aws_lambda_function" "minimal_backend_function" {
function_name = "minimal-backend-function"
filename = data.archive_file.backend_function_package.output_path
handler = "backend_lambda_function.lambda_handler"
runtime = "python3.8"
role = aws_iam_role.function_assume_role.arn
}
data "aws_iam_policy_document" "function_assume_role_policy" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
resource "aws_iam_role" "function_assume_role" {
name = "lambda-dynamodb-role"
assume_role_policy = data.aws_iam_policy_document.function_assume_role_policy.json
}
data "aws_iam_policy_document" "function_permissions" {
statement {
sid = "LambdaDynamodb"
actions = [
"dynamodb:Scan",
"dynamodb:PutItem",
"dynamodb:DeleteItem",
"dynamodb:UpdateItem"
]
resources = [aws_dynamodb_table.minimal_backend_table.arn]
}
}
resource "aws_iam_role_policy" "function_permissions" {
name = "lambda-dynamodb-policy"
role = aws_iam_role.function_assume_role.id
policy = data.aws_iam_policy_document.function_permissions.json
}
resource "aws_api_gateway_rest_api" "minimal_api" {
name = "minimal-api"
}
resource "aws_api_gateway_method" "proxy_root" {
rest_api_id = aws_api_gateway_rest_api.minimal_api.id
resource_id = aws_api_gateway_rest_api.minimal_api.root_resource_id
http_method = "ANY"
authorization = "NONE"
}
resource "aws_api_gateway_integration" "lambda_root" {
rest_api_id = aws_api_gateway_rest_api.minimal_api.id
resource_id = aws_api_gateway_method.proxy_root.resource_id
http_method = aws_api_gateway_method.proxy_root.http_method
integration_http_method = "POST"
type = "AWS_PROXY"
uri = aws_lambda_function.minimal_backend_function.invoke_arn
}
resource "aws_api_gateway_resource" "proxy" {
rest_api_id = aws_api_gateway_rest_api.minimal_api.id
parent_id = aws_api_gateway_rest_api.minimal_api.root_resource_id
path_part = "{proxy+}"
}
resource "aws_api_gateway_method" "proxy" {
rest_api_id = aws_api_gateway_rest_api.minimal_api.id
resource_id = aws_api_gateway_resource.proxy.id
http_method = "ANY"
authorization = "NONE"
}
resource "aws_api_gateway_integration" "lambda" {
rest_api_id = aws_api_gateway_rest_api.minimal_api.id
resource_id = aws_api_gateway_method.proxy.resource_id
http_method = aws_api_gateway_method.proxy.http_method
integration_http_method = "POST"
type = "AWS_PROXY"
uri = aws_lambda_function.minimal_backend_function.invoke_arn
}
resource "aws_api_gateway_deployment" "minimal" {
depends_on = [
aws_api_gateway_integration.lambda,
aws_api_gateway_integration.lambda_root,
]
rest_api_id = aws_api_gateway_rest_api.minimal_api.id
stage_name = "minimal"
}
resource "aws_lambda_permission" "api_invoke_lambda" {
statement_id = "AllowAPIGatewayInvoke"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.minimal_backend_function.function_name
principal = "apigateway.amazonaws.com"
# The "/*/*" portion grants access from any method on any resource
# within the API Gateway REST API.
source_arn = "${aws_api_gateway_rest_api.minimal_api.execution_arn}/*/*"
}
resource "aws_s3_bucket" "minimal_frontend_bucket" {
website {
index_document = "index.html"
}
}
resource "aws_s3_bucket_object" "minimal_index" {
bucket = aws_s3_bucket.minimal_frontend_bucket.id
key = "index.html"
source = "./index.html"
acl = "public-read"
content_type = "text/html"
}
resource "aws_s3_bucket_object" "minimal_script" {
bucket = aws_s3_bucket.minimal_frontend_bucket.id
key = "index.js"
content = templatefile(
"./index.js",
{ api_url = aws_api_gateway_deployment.minimal.invoke_url }
)
acl = "public-read"
}