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 f902cf0..41760c9 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 = local.reference_generator_api_gateway_name + lambda_arn = module.reference_generator_lambda.lambda_arn, + }) + api_name = local.reference_generator_api_gateway_name + 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 + } + } +}