From 9ea036db6fe737cd80056c31ecd4cadd4e16601a Mon Sep 17 00:00:00 2001 From: Santiago Lo Coco Date: Sun, 4 Dec 2022 11:47:43 -0300 Subject: [PATCH] Fix lots of bugs --- terraform/modules/apigw/main.tf | 8 ++- terraform/modules/apigw/variables.tf | 4 +- terraform/modules/lambda/README.md | 1 + terraform/modules/lambda/main.tf | 8 +++ terraform/modules/lambda/variables.tf | 11 ++++ terraform/organization/apigw.tf | 75 ++++++++++++++++++++++++-- terraform/organization/datasources.tf | 3 +- terraform/organization/html/index.html | 16 ++++-- terraform/organization/lambda.tf | 4 ++ terraform/organization/locals.tf | 19 +++++++ 10 files changed, 136 insertions(+), 13 deletions(-) diff --git a/terraform/modules/apigw/main.tf b/terraform/modules/apigw/main.tf index d54b733..89b4fc2 100644 --- a/terraform/modules/apigw/main.tf +++ b/terraform/modules/apigw/main.tf @@ -79,9 +79,11 @@ resource "aws_api_gateway_deployment" "this" { aws_api_gateway_resource.this.id, aws_api_gateway_method.this["get"].id, aws_api_gateway_method.this["post"].id, + aws_api_gateway_method.this["put"].id, aws_api_gateway_method.this["options"].id, aws_api_gateway_integration.this["get"].id, aws_api_gateway_integration.this["post"].id, + aws_api_gateway_integration.this["put"].id, aws_api_gateway_integration.this["options"].id, ])) } @@ -99,9 +101,11 @@ resource "aws_api_gateway_deployment" "this" { } resource "aws_lambda_permission" "this" { + for_each = var.lambda + statement_id = "AllowExecutionFromAPIGateway" action = "lambda:InvokeFunction" - function_name = var.lambda[0].function_name + function_name = each.value.function_name principal = "apigateway.amazonaws.com" - source_arn = "${var.lambda[0].source_arn}:${aws_api_gateway_rest_api.this.id}/*/${aws_api_gateway_method.this["get"].http_method}${aws_api_gateway_resource.this.path}" + source_arn = "${each.value.source_arn}:${aws_api_gateway_rest_api.this.id}/*/${each.value.http_method}${aws_api_gateway_resource.this.path}" } diff --git a/terraform/modules/apigw/variables.tf b/terraform/modules/apigw/variables.tf index f408b24..f09f578 100644 --- a/terraform/modules/apigw/variables.tf +++ b/terraform/modules/apigw/variables.tf @@ -24,8 +24,8 @@ variable "role_arn" { } variable "lambda" { - description = "List of lambdas the API will execute." - type = list(any) + description = "Map of lambdas the API will execute." + type = map(any) } variable "method" { diff --git a/terraform/modules/lambda/README.md b/terraform/modules/lambda/README.md index 03ccd9a..7edf878 100644 --- a/terraform/modules/lambda/README.md +++ b/terraform/modules/lambda/README.md @@ -41,5 +41,6 @@ No modules. | Name | Description | |------|-------------| | [function\_arn](#output\_function\_arn) | The ARN of the Lambda Function | +| [function\_invoke\_arn](#output\_function\_invoke\_arn) | The invoke ARN of the Lambda Function | | [function\_name](#output\_function\_name) | The name of the Lambda Function | \ No newline at end of file diff --git a/terraform/modules/lambda/main.tf b/terraform/modules/lambda/main.tf index df561fb..3c1eb07 100644 --- a/terraform/modules/lambda/main.tf +++ b/terraform/modules/lambda/main.tf @@ -10,6 +10,7 @@ resource "aws_lambda_function" "this" { runtime = var.runtime tags = var.tags timeout = 30 + source_code_hash = var.source_code_hash dynamic "vpc_config" { for_each = var.vpc_subnet_ids != null && var.vpc_security_group_ids != null ? [true] : [] @@ -18,4 +19,11 @@ resource "aws_lambda_function" "this" { subnet_ids = var.vpc_subnet_ids } } + + dynamic "environment" { + for_each = length(keys(var.environment_variables)) == 0 ? [] : [true] + content { + variables = var.environment_variables + } + } } diff --git a/terraform/modules/lambda/variables.tf b/terraform/modules/lambda/variables.tf index 5275966..753a2ec 100644 --- a/terraform/modules/lambda/variables.tf +++ b/terraform/modules/lambda/variables.tf @@ -51,3 +51,14 @@ variable "vpc_security_group_ids" { type = list(string) default = null } + +variable "environment_variables" { + description = "A map that defines environment variables for the Lambda Function." + type = map(string) + default = {} +} + +variable "source_code_hash" { + description = "The zip hash." + type = string +} \ No newline at end of file diff --git a/terraform/organization/apigw.tf b/terraform/organization/apigw.tf index 4df3c86..6990ad6 100644 --- a/terraform/organization/apigw.tf +++ b/terraform/organization/apigw.tf @@ -12,13 +12,26 @@ module "apigw" { name = "AWSAPIGateway-g3" - lambda = [ - { + lambda = { + lambdaDB = { function_arn = module.lambda["lambdaDB"].function_invoke_arn function_name = module.lambda["lambdaDB"].function_name source_arn = "arn:aws:execute-api:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}" + http_method = "GET" + }, + lambdaUpdateAPI = { + function_arn = module.lambda["lambdaUpdateAPI"].function_invoke_arn + function_name = module.lambda["lambdaUpdateAPI"].function_name + source_arn = "arn:aws:execute-api:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}" + http_method = "PUT" } - ] + lambdaRemove = { + function_arn = module.lambda["lambdaRemove"].function_invoke_arn + function_name = module.lambda["lambdaRemove"].function_name + source_arn = "arn:aws:execute-api:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}" + http_method = "DELETE" + } + } role_arn = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/LabRole" sqs_arn = "arn:aws:apigateway:${data.aws_region.current.name}:sqs:path/${module.sqs.name}" @@ -30,6 +43,12 @@ module "apigw" { post = { http_method = "POST" }, + put = { + http_method = "PUT" + }, + delete = { + http_method = "DELETE" + }, options = { http_method = "OPTIONS" }, @@ -56,6 +75,30 @@ module "apigw" { "application/json" = file("template.json") } }, + put = { + integration_http_method = "POST", + type = "AWS_PROXY", + credentials = null, + uri = module.lambda["lambdaUpdateAPI"].function_invoke_arn, + request_parameters = { + "integration.request.header.Content-Type" = "'application/x-www-form-urlencoded'" + }, + request_templates = { + "application/json" = file("template.json") + } + }, + delete = { + integration_http_method = "POST", + type = "AWS_PROXY", + credentials = null, + uri = module.lambda["lambdaRemove"].function_invoke_arn, + request_parameters = { + "integration.request.header.Content-Type" = "'application/x-www-form-urlencoded'" + }, + request_templates = { + "application/json" = file("template.json") + } + }, options = { integration_http_method = null, type = "MOCK", @@ -85,6 +128,18 @@ module "apigw" { "method.response.header.Access-Control-Allow-Origin" = "true" } }, + put = { + response_models = {} + response_parameters = { + "method.response.header.Access-Control-Allow-Origin" = "true" + } + }, + delete = { + response_models = {} + response_parameters = { + "method.response.header.Access-Control-Allow-Origin" = "true" + } + }, options = { response_models = { "application/json" = "Empty" @@ -110,11 +165,23 @@ module "apigw" { "method.response.header.Access-Control-Allow-Origin" = "'*'" } }, + put = { + selection_pattern = "^2[0-9][0-9]" + response_parameters = { + "method.response.header.Access-Control-Allow-Origin" = "'*'" + } + }, + delete = { + selection_pattern = "^2[0-9][0-9]" + response_parameters = { + "method.response.header.Access-Control-Allow-Origin" = "'*'" + } + }, options = { selection_pattern = null response_parameters = { "method.response.header.Access-Control-Allow-Headers" = "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'", - "method.response.header.Access-Control-Allow-Methods" = "'GET,OPTIONS,POST'", + "method.response.header.Access-Control-Allow-Methods" = "'GET,OPTIONS,POST,PUT,DELETE'", "method.response.header.Access-Control-Allow-Origin" = "'*'" } } diff --git a/terraform/organization/datasources.tf b/terraform/organization/datasources.tf index 35aa384..3379531 100644 --- a/terraform/organization/datasources.tf +++ b/terraform/organization/datasources.tf @@ -24,7 +24,8 @@ data "aws_iam_policy_document" "dynamodb" { "dynamodb:PutItem", "dynamodb:Scan", "dynamodb:GetItem", - "dynamodb:UpdateItem" + "dynamodb:UpdateItem", + "dynamodb:DeleteItem" ] principals { type = "AWS" diff --git a/terraform/organization/html/index.html b/terraform/organization/html/index.html index 3e27ca2..5634c1e 100644 --- a/terraform/organization/html/index.html +++ b/terraform/organization/html/index.html @@ -24,7 +24,8 @@ Enter the new stock number.
  • - + +
  • @@ -41,17 +42,24 @@ const thisForm = document.getElementById('myForm'); thisForm.addEventListener('submit', async function (e) { e.preventDefault(); + var api_method + if (e.submitter.value == "Save") { + api_method = "PUT" + } else { + api_method = "DELETE" + } const formData = new FormData(thisForm).entries() const str = JSON.stringify(Object.fromEntries(formData)) const response = await fetch("${ENDPOINT}/products", { - method: 'POST', + method: api_method, headers: { 'Content-Type': 'application/json' }, - body: str.replace(/\"/g, '') + body: str.replace(/"([\d\.]+)"/g, "$1") }); - const result = await response.json(); + // const result = await response.json(); + const result = await response; }); function adjust_textarea(h) { diff --git a/terraform/organization/lambda.tf b/terraform/organization/lambda.tf index 8cd3d5d..6054634 100644 --- a/terraform/organization/lambda.tf +++ b/terraform/organization/lambda.tf @@ -18,4 +18,8 @@ module "lambda" { vpc_subnet_ids = module.vpc.private_subnets vpc_security_group_ids = [each.value.security_group_ids] + + source_code_hash = filebase64sha256(each.value.package) + + # environment_variables = each.value.environment_variables } diff --git a/terraform/organization/locals.tf b/terraform/organization/locals.tf index b55560c..fc2b5d9 100644 --- a/terraform/organization/locals.tf +++ b/terraform/organization/locals.tf @@ -82,6 +82,9 @@ locals { handler = "lambda_handler.main" runtime = "python3.7", security_group_ids = aws_security_group.sns_sg.id + # environment_variables = { + # apigw = "${module.apigw.endpoint}" + # } } lambdaUpdate = { package = "${local.path}/lambda/lambdaUpdate.zip" @@ -99,6 +102,22 @@ locals { runtime = "python3.9", security_group_ids = aws_security_group.sns_sg.id } + lambdaUpdateAPI = { + package = "${local.path}/lambda/lambdaUpdateAPI.zip" + function_name = "AWSLambdaHandlerUpdateAPIg3" + role = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/LabRole" + handler = "lambda_handler.main" + runtime = "python3.9", + security_group_ids = aws_security_group.dynamodb_sg.id + } + lambdaRemove = { + package = "${local.path}/lambda/lambdaRemove.zip" + function_name = "AWSLambdaHandlerRemoveDBg3" + role = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/LabRole" + handler = "lambda_handler.main" + runtime = "python3.9", + security_group_ids = aws_security_group.dynamodb_sg.id + } } private_inbound = [