-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
224 lines (193 loc) · 6.99 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
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
resource "aws_dynamodb_table" "Dev_Dynamo" {
name = var.table_name
billing_mode = "PAY_PER_REQUEST"
hash_key = "PK"
range_key = "SK"
attribute {
name = "PK"
type = "S"
}
attribute {
name = "SK"
type = "S"
}
}
# Create S3 bucket to store our application source code.
resource "aws_s3_bucket" "Dev_s3" {
bucket = var.code_bucket_name
force_destroy = true
}
resource "aws_s3_bucket_public_access_block" "lambda_bucket_public_access" {
bucket = aws_s3_bucket.Dev_s3.id
block_public_acls = false
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
# Initialize module containing IAM policies.
module "iam_policies" {
source = "./tf-modules/iam-policies"
table_name = aws_dynamodb_table.Dev_Dynamo.name
}
## Update Product Lambda
module "update_product_lambda" {
source = "./tf-modules/lambda-function"
lambda_bucket_id = aws_s3_bucket.Dev_s3.id
publish_dir = "${path.module}/../build/updateproduct"
zip_file = "updateproduct.zip"
function_name = "updateproduct"
lambda_handler = "updateproduct"
environment_variables = {
"product_TABLE_NAME" = aws_dynamodb_table.Dev_Dynamo.name
}
}
module "update_product_lambda_api" {
source = "./tf-modules/api-gateway-lambda-integration"
api_id = module.product_api_gateway.api_id
api_arn = module.product_api_gateway.api_arn
function_arn = module.update_product_lambda.function_arn
function_name = module.update_product_lambda.function_name
http_method = "PUT"
route = "/"
}
resource "aws_iam_role_policy_attachment" "update_product_lambda_dynamo_db_write" {
role = module.update_product_lambda.function_role_name
policy_arn = module.iam_policies.dynamo_db_write
}
resource "aws_iam_role_policy_attachment" "update_product_lambda_dynamo_db_read" {
role = module.update_product_lambda.function_role_name
policy_arn = module.iam_policies.dynamo_db_read
}
resource "aws_iam_role_policy_attachment" "update_product_lambda_cw_metrics" {
role = module.update_product_lambda.function_role_name
policy_arn = module.iam_policies.cloud_watch_put_metrics
}
# Create Product Lambda
module "create_product_lambda" {
source = "./tf-modules/lambda-function"
lambda_bucket_id = aws_s3_bucket.Dev_s3.id
publish_dir = "${path.module}/../build/createproduct"
zip_file = "createproduct.zip"
function_name = "createproduct"
lambda_handler = "createproduct"
environment_variables = {
"product_TABLE_NAME" = aws_dynamodb_table.Dev_Dynamo.name
}
}
module "create_product_lambda_api" {
source = "./tf-modules/api-gateway-lambda-integration"
api_id = module.product_api_gateway.api_id
api_arn = module.product_api_gateway.api_arn
function_arn = module.create_product_lambda.function_arn
function_name = module.create_product_lambda.function_name
http_method = "POST"
route = "/"
}
resource "aws_iam_role_policy_attachment" "create_product_lambda_dynamo_db_write" {
role = module.create_product_lambda.function_role_name
policy_arn = module.iam_policies.dynamo_db_write
}
resource "aws_iam_role_policy_attachment" "create_product_lambda_cw_metrics" {
role = module.create_product_lambda.function_role_name
policy_arn = module.iam_policies.cloud_watch_put_metrics
}
# Get Product Lambda
module "get_product_lambda" {
source = "./tf-modules/lambda-function"
lambda_bucket_id = aws_s3_bucket.Dev_s3.id
publish_dir = "${path.module}/../build/getproduct"
zip_file = "getproduct.zip"
function_name = "getproduct"
lambda_handler = "getproduct"
environment_variables = {
"PROJECT_TABLE_NAME" = aws_dynamodb_table.Dev_Dynamo.name
}
}
module "get_product_lambda_api" {
source = "./tf-modules/api-gateway-lambda-integration"
api_id = module.product_api_gateway.api_id
api_arn = module.product_api_gateway.api_arn
function_arn = module.get_product_lambda.function_arn
function_name = module.get_product_lambda.function_name
http_method = "GET"
route = "/{partitionKey}"
}
resource "aws_iam_role_policy_attachment" "get_product_lambda_dynamo_db_read" {
role = module.get_product_lambda.function_role_name
policy_arn = module.iam_policies.dynamo_db_read
}
resource "aws_iam_role_policy_attachment" "get_product_lambda_cw_metrics" {
role = module.get_product_lambda.function_role_name
policy_arn = module.iam_policies.cloud_watch_put_metrics
}
# Get all products Lambda
module "get_products_lambda" {
source = "./tf-modules/lambda-function"
lambda_bucket_id = aws_s3_bucket.Dev_s3.id
publish_dir = "${path.module}/../build/getproducts"
zip_file = "getproducts.zip"
function_name = "getproducts"
lambda_handler = "getproducts"
environment_variables = {
"product_TABLE_NAME" = aws_dynamodb_table.Dev_Dynamo.name
}
}
module "get_products_lambda_api" {
source = "./tf-modules/api-gateway-lambda-integration"
api_id = module.product_api_gateway.api_id
api_arn = module.product_api_gateway.api_arn
function_arn = module.get_products_lambda.function_arn
function_name = module.get_products_lambda.function_name
http_method = "GET"
route = "/"
}
resource "aws_iam_role_policy_attachment" "get_products_lambda_dynamo_db_read" {
role = module.get_products_lambda.function_role_name
policy_arn = module.iam_policies.dynamo_db_read
}
resource "aws_iam_role_policy_attachment" "get_products_lambda_cw_metrics" {
role = module.get_products_lambda.function_role_name
policy_arn = module.iam_policies.cloud_watch_put_metrics
}
## Delete Project lambda
module "delete_product_lambda" {
source = "./tf-modules/lambda-function"
lambda_bucket_id = aws_s3_bucket.Dev_s3.id
publish_dir = "${path.module}/../build/deleteproduct"
zip_file = "deleteproduct.zip"
function_name = "deleteproduct"
lambda_handler = "deleteproduct"
environment_variables = {
"product_TABLE_NAME" = aws_dynamodb_table.Dev_Dynamo.name
}
}
module "delete_product_lambda_api" {
source = "./tf-modules/api-gateway-lambda-integration"
api_id = module.product_api_gateway.api_id
api_arn = module.product_api_gateway.api_arn
function_arn = module.delete_product_lambda.function_arn
function_name = module.delete_product_lambda.function_name
http_method = "DELETE"
route = "/{partitionKey}"
}
resource "aws_iam_role_policy_attachment" "delete_product_lambda_dynamo_db_delete" {
role = module.delete_product_lambda.function_role_name
policy_arn = module.iam_policies.dynamo_db_delete
}
resource "aws_iam_role_policy_attachment" "delete_product_lambda_dynamo_db_read" {
role = module.delete_product_lambda.function_role_name
policy_arn = module.iam_policies.dynamo_db_read
}
resource "aws_iam_role_policy_attachment" "delete_product_lambda_cw_metrics" {
role = module.delete_product_lambda.function_role_name
policy_arn = module.iam_policies.cloud_watch_put_metrics
}
module "product_api_gateway" {
source = "./tf-modules/api-gateway"
api_name = "implant-wise-product-api"
stage_name = "dev"
stage_auto_deploy = true
}
module "cognito" {
source = "./tf-modules/cognito"
}