From b81c9270c376b6393759aa8c77ffd0bc3e5be32b Mon Sep 17 00:00:00 2001 From: thanhz Date: Tue, 12 Sep 2023 15:38:01 +0100 Subject: [PATCH 1/2] create api gateway resource --- terraform/root_main.tf | 15 +++++ .../api_gateway/reference_generator.json.tpl | 56 +++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 terraform/templates/api_gateway/reference_generator.json.tpl diff --git a/terraform/root_main.tf b/terraform/root_main.tf index f902cf0..feea38a 100644 --- a/terraform/root_main.tf +++ b/terraform/root_main.tf @@ -48,4 +48,19 @@ module "reference_generator_lambda" { timeout_seconds = 60 memory_size = 1024 tags = local.hosting_common_tags + lambda_invoke_permissions = { + "apigateway.amazonaws.com" = "${module.reference_generator_api_gateway.api_execution_arn}/*/GET/counter" + } +} + +module "reference_generator_api_gateway" { + source = "./da-terraform-modules/apigateway" + api_definition = templatefile("./templates/api_gateway/reference_generator.json.tpl", { + environment = local.hosting_environment + title = "${upper(var.project)}ReferenceGenerator" + lambda_arn = module.reference_generator_lambda.lambda_arn, + }) + api_name = "${upper(var.project)}ReferenceGenerator" + environment = local.hosting_environment + common_tags = local.hosting_common_tags } diff --git a/terraform/templates/api_gateway/reference_generator.json.tpl b/terraform/templates/api_gateway/reference_generator.json.tpl new file mode 100644 index 0000000..666c7d8 --- /dev/null +++ b/terraform/templates/api_gateway/reference_generator.json.tpl @@ -0,0 +1,56 @@ +{ + "swagger" : "2.0", + "info" : { + "description" : "API Gateway for Reference Generator", + "version" : "2023-09-11T11:10:51Z", + "title" : "${title}" + }, + "basePath" : "/${environment}", + "schemes" : [ "https" ], + "paths" : { + "/counter" : { + "get" : { + "produces" : [ "application/json" ], + "parameters" : [ { + "name" : "numberofrefs", + "in" : "query", + "required" : true, + "type" : "string" + } ], + "responses" : { + "200" : { + "description" : "200 response", + "schema" : { + "$ref" : "#/definitions/Empty" + } + } + }, + "x-amazon-apigateway-request-validator" : "Validate query string parameters and headers", + "x-amazon-apigateway-integration" : { + "type" : "aws_proxy", + "httpMethod" : "POST", + "uri" : "arn:aws:apigateway:eu-west-2:lambda:path/2015-03-31/functions/${lambda_arn}/invocations", + "responses" : { + "default" : { + "statusCode" : "200" + } + }, + "passthroughBehavior" : "when_no_match", + "contentHandling" : "CONVERT_TO_TEXT" + } + } + } + }, + "definitions" : { + "Empty" : { + "type" : "object", + "title" : "Empty Schema" + } + }, + "x-amazon-apigateway-request-validators" : { + "Validate query string parameters and headers" : { + "validateRequestParameters" : true, + "validateRequestBody" : false + } + } +} From 977de8ed4a8c1b491154fdc40593735dd61af9f6 Mon Sep 17 00:00:00 2001 From: thanhz Date: Wed, 13 Sep 2023 10:27:57 +0100 Subject: [PATCH 2/2] pr comments --- terraform/root_locals.tf | 7 ++++--- terraform/root_main.tf | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/terraform/root_locals.tf b/terraform/root_locals.tf index de32d90..896f834 100644 --- a/terraform/root_locals.tf +++ b/terraform/root_locals.tf @@ -11,7 +11,8 @@ locals { "Role" = "prvt" } ) - dynamodb_hash_key = "v1" - reference_generator_function_name = "${var.project}-reference-generator-${local.hosting_environment}" - reference_counter_table_name = "${var.project}-reference-counter" + dynamodb_hash_key = "v1" + reference_generator_function_name = "${var.project}-reference-generator-${local.hosting_environment}" + reference_generator_api_gateway_name = "${upper(var.project)}ReferenceGenerator${local.hosting_environment}" + reference_counter_table_name = "${var.project}-reference-counter" } diff --git a/terraform/root_main.tf b/terraform/root_main.tf index feea38a..41760c9 100644 --- a/terraform/root_main.tf +++ b/terraform/root_main.tf @@ -57,10 +57,10 @@ module "reference_generator_api_gateway" { source = "./da-terraform-modules/apigateway" api_definition = templatefile("./templates/api_gateway/reference_generator.json.tpl", { environment = local.hosting_environment - title = "${upper(var.project)}ReferenceGenerator" + title = local.reference_generator_api_gateway_name lambda_arn = module.reference_generator_lambda.lambda_arn, }) - api_name = "${upper(var.project)}ReferenceGenerator" + api_name = local.reference_generator_api_gateway_name environment = local.hosting_environment common_tags = local.hosting_common_tags }