Fix lots of bugs
This commit is contained in:
parent
adad6f5386
commit
9ea036db6f
|
@ -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}"
|
||||
}
|
||||
|
|
|
@ -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" {
|
||||
|
|
|
@ -41,5 +41,6 @@ No modules.
|
|||
| Name | Description |
|
||||
|------|-------------|
|
||||
| <a name="output_function_arn"></a> [function\_arn](#output\_function\_arn) | The ARN of the Lambda Function |
|
||||
| <a name="output_function_invoke_arn"></a> [function\_invoke\_arn](#output\_function\_invoke\_arn) | The invoke ARN of the Lambda Function |
|
||||
| <a name="output_function_name"></a> [function\_name](#output\_function\_name) | The name of the Lambda Function |
|
||||
<!-- END_TF_DOCS -->
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -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" = "'*'"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -24,7 +24,8 @@
|
|||
<span>Enter the new stock number.</span>
|
||||
</li>
|
||||
<li>
|
||||
<input type="submit" value="Upload"/>
|
||||
<input type="submit" value="Save"/>
|
||||
<input type="submit" value="Delete"/>
|
||||
</li>
|
||||
</ul>
|
||||
</form>
|
||||
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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 = [
|
||||
|
|
Loading…
Reference in New Issue