-
Notifications
You must be signed in to change notification settings - Fork 0
/
tf_deploy_main.tf
227 lines (170 loc) · 7.57 KB
/
tf_deploy_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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
########## COMMON DECLARATIONS ##########
## CONFIGURE AWS PROVIDER ##
provider "aws" {
shared_credentials_files = [var.aws_creds_path]
region = var.aws_region
}
## 1 Deploy the script to ECR using the deploy script. The deploy script takes in account_id, region and repository_name as variables, so set them accordingly.
# 1.1 In the AWS ECR, create a repository with repository_name
# 1.2 Upload the docker image to the ECR.
# Run `predeploy` bash script
## 2. Create a new lambda function (using docker), and select the deployed repository_name
# 2.1 Create ROLE
resource "aws_iam_role" "histogram_tool_preprocessing_role" {
name = "fcx-histogram_tool_preprocessing_role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Sid = ""
Principal = {
Service = "lambda.amazonaws.com"
}
}
]
})
}
# policy defination
data "aws_iam_policy_document" "histogram_tool_aws_access" {
statement {
effect = "Allow"
actions = ["s3:GetObject", "s3-object-lambda:*"]
resources = ["*"]
}
}
resource "aws_iam_policy" "histogram_tool_aws_access" {
name = "fcx_histogram_tool_aws_access"
path = "/"
description = "IAM policy that allows a lambda to get s3 object"
policy = data.aws_iam_policy_document.histogram_tool_aws_access.json
}
# attach policies to role
resource "aws_iam_role_policy_attachment" "lambda_policy_basic" {
role = aws_iam_role.histogram_tool_preprocessing_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
resource "aws_iam_role_policy_attachment" "histogram_tool_aws_access" {
role = aws_iam_role.histogram_tool_preprocessing_role.name
policy_arn = aws_iam_policy.histogram_tool_aws_access.arn
}
# 2.2 Create lambda function
resource "aws_lambda_function" "histogram_tool_preprocessing" {
package_type = "Image"
function_name = "fcx-histogram-preprocessing"
image_uri = "${var.accountId}.dkr.ecr.${var.aws_region}.amazonaws.com/${var.ecr_name}:latest"
role = aws_iam_role.histogram_tool_preprocessing_role.arn
memory_size = var.lambda_execution_memory
timeout = var.lambda_execution_timeout
ephemeral_storage {
size = var.lambda_execution_ephimeral_storage
}
}
## 2.3. CREATE CLOUDWATCH LOG GROUP ##
# log lambda
resource "aws_cloudwatch_log_group" "histogram_tool_preprocessing" {
name = "/aws/lambda/${aws_lambda_function.histogram_tool_preprocessing.function_name}"
retention_in_days = 5
}
## 3. Configure REST API gateway for the lambda.
## REST API GATEWAY
# API Gateway name
resource "aws_api_gateway_rest_api" "histogram_tool_preprocessing" {
name = "fcx-histogram-preprocessing-api"
}
## create resource
resource "aws_api_gateway_resource" "histogram_tool_preprocessing" {
path_part = aws_lambda_function.histogram_tool_preprocessing.function_name
parent_id = aws_api_gateway_rest_api.histogram_tool_preprocessing.root_resource_id
rest_api_id = aws_api_gateway_rest_api.histogram_tool_preprocessing.id
}
## create method
resource "aws_api_gateway_method" "histogram_tool_preprocessing" {
rest_api_id = aws_api_gateway_rest_api.histogram_tool_preprocessing.id
resource_id = aws_api_gateway_resource.histogram_tool_preprocessing.id
http_method = "POST"
authorization = "NONE"
api_key_required = true
}
## allow CORS on preflight
module "subset_trigger_api_cors" {
source = "squidfunk/api-gateway-enable-cors/aws"
version = "0.3.3"
api_id = aws_api_gateway_rest_api.histogram_tool_preprocessing.id
api_resource_id = aws_api_gateway_resource.histogram_tool_preprocessing.id
depends_on = [ aws_api_gateway_rest_api.histogram_tool_preprocessing, aws_api_gateway_resource.histogram_tool_preprocessing ]
}
## INTEGRATION OF GATEWAY AND LAMBDA TRIGGER
resource "aws_api_gateway_integration" "histogram_tool_preprocessing" {
rest_api_id = aws_api_gateway_rest_api.histogram_tool_preprocessing.id
resource_id = aws_api_gateway_resource.histogram_tool_preprocessing.id
http_method = aws_api_gateway_method.histogram_tool_preprocessing.http_method
integration_http_method = aws_api_gateway_method.histogram_tool_preprocessing.http_method
type = "AWS_PROXY"
uri = aws_lambda_function.histogram_tool_preprocessing.invoke_arn
}
## PERMISSIONS to trigger lamba from api gateway
resource "aws_lambda_permission" "histogram_tool_preprocessing" {
statement_id = "AllowExecutionFromAPIGateway"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.histogram_tool_preprocessing.function_name
principal = "apigateway.amazonaws.com"
# More: http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-control-access-using-iam-policies-to-invoke-api.html
source_arn = "arn:aws:execute-api:${var.aws_region}:${var.accountId}:${aws_api_gateway_rest_api.histogram_tool_preprocessing.id}/*/${aws_api_gateway_method.histogram_tool_preprocessing.http_method}${aws_api_gateway_resource.histogram_tool_preprocessing.path}"
}
## SET RESPONSE HANDLERS FOR API-GATEWAY (NOT NEEDED FOR AWS PROXY INTEGRATION) HANDLE CORS HEADERS FROM LAMBDA RESPONSE ITSELF
## Create deployment for histogram_tool_preprocessing
resource "aws_api_gateway_deployment" "histogram_tool_preprocessing" {
rest_api_id = aws_api_gateway_rest_api.histogram_tool_preprocessing.id
triggers = {
redeployment = sha1(jsonencode([
aws_api_gateway_resource.histogram_tool_preprocessing.id,
aws_api_gateway_method.histogram_tool_preprocessing.id,
aws_api_gateway_integration.histogram_tool_preprocessing.id,
]))
}
lifecycle {
create_before_destroy = true
}
}
## create stage for the histogram_tool_preprocessing
resource "aws_api_gateway_stage" "histogram_tool_preprocessing" {
deployment_id = aws_api_gateway_deployment.histogram_tool_preprocessing.id
rest_api_id = aws_api_gateway_rest_api.histogram_tool_preprocessing.id
stage_name = var.stage_name
depends_on = [aws_cloudwatch_log_group.histogram_tool_preprocessing_api]
}
### to enable api key and its usage plan for histogram_tool_preprocessing
# create api key
resource "aws_api_gateway_api_key" "histogram_tool_preprocessing_api_key" {
name = "histogram_preprocessing_api-key"
}
# create usage plans
resource "aws_api_gateway_usage_plan" "histogram_tool_preprocessing_api_usagePlan" {
name = "histogram_preprocessing_api-usagePlan"
description = "Usage plan for the histogram_preprocessing_api key"
api_stages {
api_id = aws_api_gateway_rest_api.histogram_tool_preprocessing.id
stage = aws_api_gateway_stage.histogram_tool_preprocessing.stage_name
}
}
resource "aws_api_gateway_usage_plan_key" "histogram_usagePlan_with_key" {
key_id = aws_api_gateway_api_key.histogram_tool_preprocessing_api_key.id
key_type = "API_KEY"
usage_plan_id = aws_api_gateway_usage_plan.histogram_tool_preprocessing_api_usagePlan.id
}
## add and enable cloudwatch logs for subset_trigger_api
resource "aws_cloudwatch_log_group" "histogram_tool_preprocessing_api" {
name = "API-Gateway-Execution-Logs_${aws_api_gateway_rest_api.histogram_tool_preprocessing.id}/${var.stage_name}"
retention_in_days = 3
}
resource "aws_api_gateway_method_settings" "histogram_tool_preprocessing_api_method" {
rest_api_id = aws_api_gateway_rest_api.histogram_tool_preprocessing.id
stage_name = aws_api_gateway_stage.histogram_tool_preprocessing.stage_name
method_path = "*/*"
settings {
metrics_enabled = true
logging_level = "INFO"
}
}