-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
180 lines (141 loc) · 5.34 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
################################################################################
# REST API
################################################################################
resource "aws_api_gateway_rest_api" "this" {
name = "serverless-swagger-ui"
description = "This is a test API Gateway to demonstrate the use of Swagger UI"
body = templatefile("${path.module}/api-gateway-definition.yaml",
{
orders_handler_arn = aws_lambda_function.orders_handler.arn
swagger_ui_handler_arn = aws_lambda_function.swagger_ui_handler.arn
}
)
endpoint_configuration {
types = ["REGIONAL"]
}
}
################################################################################
# Orders Handler
################################################################################
resource "aws_lambda_function" "orders_handler" {
function_name = "orders-handler"
role = aws_iam_role.orders_handler.arn
filename = data.archive_file.orders_handler.output_path
source_code_hash = data.archive_file.orders_handler.output_base64sha256
handler = "orders.handler"
runtime = "nodejs18.x"
}
resource "aws_lambda_permission" "orders_handler" {
function_name = aws_lambda_function.orders_handler.function_name
action = "lambda:InvokeFunction"
principal = "apigateway.amazonaws.com"
source_arn = "${aws_api_gateway_rest_api.this.execution_arn}/*"
}
data "archive_file" "orders_handler" {
type = "zip"
source_file = "${path.module}/src/orders/orders.js"
output_path = "${path.module}/src/orders/orders.zip"
}
resource "aws_iam_role" "orders_handler" {
name = "orders-handler"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "lambda.amazonaws.com"
}
},
]
})
}
resource "aws_iam_role_policy_attachment" "orders_handler" {
role = aws_iam_role.orders_handler.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
################################################################################
# Swagger UI Handler
################################################################################
resource "aws_lambda_function" "swagger_ui_handler" {
function_name = "swagger-ui-handler"
role = aws_iam_role.swagger_ui_handler.arn
filename = data.archive_file.swagger_ui_handler.output_path
source_code_hash = data.archive_file.swagger_ui_handler.output_base64sha256
handler = "app.handler"
layers = [aws_lambda_layer_version.swagger_ui_handler.arn]
runtime = "nodejs14.x"
}
resource "aws_lambda_permission" "swagger_ui_handler" {
function_name = aws_lambda_function.swagger_ui_handler.function_name
action = "lambda:InvokeFunction"
principal = "apigateway.amazonaws.com"
source_arn = "${aws_api_gateway_rest_api.this.execution_arn}/*"
}
data "archive_file" "swagger_ui_handler" {
type = "zip"
source_file = "${path.module}/src/swagger-ui/app.js"
output_path = "${path.module}/src/swagger-ui/app.zip"
}
resource "aws_lambda_layer_version" "swagger_ui_handler" {
layer_name = "swagger-ui-commonLibs"
filename = "${path.module}/src/swagger-ui/build/commonLibs.zip"
compatible_runtimes = ["nodejs14.x"]
# depends_on = [
# data.archive_file.commonLibs
# ]
}
resource "aws_iam_role" "swagger_ui_handler" {
name = "swagger-ui-handler"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "lambda.amazonaws.com"
}
},
]
})
}
resource "aws_iam_role_policy_attachment" "swagger_ui_handler_cloudwatch_access" {
role = aws_iam_role.swagger_ui_handler.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
resource "aws_iam_role_policy_attachment" "swagger_ui_handler_api_gateway_access" {
role = aws_iam_role.swagger_ui_handler.name
policy_arn = "arn:aws:iam::aws:policy/AmazonAPIGatewayAdministrator"
}
################################################################################
# OPTIONAL: Perform npm install of dependencies and create zip file
################################################################################
# resource "null_resource" "lambda_swagger_ui_nodejs_layer" {
# provisioner "local-exec" {
# working_dir = "${path.module}/src/swagger-ui/layers/commonLibs/nodejs"
# command = "npm install"
# }
# }
#data "archive_file" "commonLibs" {
# type = "zip"
#
# source_dir = "${path.module}/src/swagger-ui/layers/commonLibs"
# output_path = "${path.module}/src/swagger-ui/build/commonLibs.zip"
#
#depends_on = [null_resource.lambda_swagger_ui_nodejs_layer]
################################################################################
# Create Deployment and API Gateway Stage
################################################################################
resource "aws_api_gateway_deployment" "this" {
rest_api_id = aws_api_gateway_rest_api.this.id
lifecycle {
create_before_destroy = true
}
}
resource "aws_api_gateway_stage" "this" {
deployment_id = aws_api_gateway_deployment.this.id
rest_api_id = aws_api_gateway_rest_api.this.id
stage_name = "v1"
}