-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
130 lines (106 loc) · 3.39 KB
/
main.tf
File metadata and controls
130 lines (106 loc) · 3.39 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
# Where the zipped packages are stored for retrieval by lambda
resource "aws_s3_bucket" "packages" {
bucket = var.packages_bucket
force_destroy = true
}
# The zip hash of the API package for lambda to unpack and run.
data "aws_s3_bucket_object" "api_zip_hash" {
bucket = aws_s3_bucket.packages.id
key = "${var.package_key}.base64sha256"
}
resource "aws_cloudwatch_log_group" "api_logs" {
name = "/aws/lambda/${var.api_name}"
retention_in_days = 90
}
resource "aws_lambda_function" "api" {
s3_bucket = aws_s3_bucket.packages.id
s3_key = var.package_key
function_name = var.api_name
description = "Lambda for ${var.api_name}"
role = var.api_iam_role_arn
runtime = var.runtime
source_code_hash = "${data.aws_s3_bucket_object.api_zip_hash.body}"
handler = "index.handler"
timeout = var.timeout
memory_size = 512
depends_on = [
aws_cloudwatch_log_group.api_logs,
]
environment {
variables = var.environment_variables
}
}
resource "aws_lambda_permission" "api_gateway_api" {
statement_id = "AllowExecutionFromAPIGateway"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.api.arn
principal = "apigateway.amazonaws.com"
source_arn = "${aws_api_gateway_rest_api.api.execution_arn}/*"
}
# API Gateway
resource "aws_api_gateway_rest_api" "api" {
name = var.api_name
depends_on = [
aws_lambda_function.api,
]
}
resource "aws_api_gateway_resource" "api" {
parent_id = aws_api_gateway_rest_api.api.root_resource_id
rest_api_id = aws_api_gateway_rest_api.api.id
path_part = "{proxy+}"
}
resource "aws_api_gateway_method" "api" {
rest_api_id = aws_api_gateway_rest_api.api.id
resource_id = aws_api_gateway_resource.api.id
http_method = "POST"
authorization = "NONE"
}
resource "aws_api_gateway_integration" "api" {
rest_api_id = aws_api_gateway_rest_api.api.id
resource_id = aws_api_gateway_resource.api.id
http_method = aws_api_gateway_method.api.http_method
integration_http_method = "POST"
type = "AWS_PROXY"
uri = aws_lambda_function.api.invoke_arn
}
resource "aws_api_gateway_method_response" "api" {
rest_api_id = aws_api_gateway_rest_api.api.id
resource_id = aws_api_gateway_resource.api.id
http_method = aws_api_gateway_method.api.http_method
status_code = 200
response_models = {
"application/json" = "Empty"
}
}
resource "aws_api_gateway_integration_response" "api" {
rest_api_id = aws_api_gateway_rest_api.api.id
resource_id = aws_api_gateway_resource.api.id
http_method = aws_api_gateway_method.api.http_method
status_code = aws_api_gateway_method_response.api.status_code
response_templates = {
"application/json" = ""
}
}
module "api-gateway-enable-cors" {
source = "squidfunk/api-gateway-enable-cors/aws"
version = "0.3.3"
api_id = aws_api_gateway_rest_api.api.id
api_resource_id = aws_api_gateway_resource.api.id
allow_methods = [
"OPTIONS",
"POST"
]
}
resource "aws_api_gateway_deployment" "api" {
depends_on = [
aws_api_gateway_method.api,
]
rest_api_id = aws_api_gateway_rest_api.api.id
stage_name = "prod"
triggers = {
redeployment = md5("main.tf")
}
lifecycle {
create_before_destroy = true
}
}