From 2e7268944b8d60ef23d7befece9fda3372b20d62 Mon Sep 17 00:00:00 2001 From: chenga Date: Wed, 1 May 2024 08:20:04 +0300 Subject: [PATCH] initial commit --- locals.tf | 6 ++++-- modules/lambda/lambda.tf | 4 ++-- modules/lambda/variables.tf | 5 +++++ multiple-lambdas-integration.tf | 6 ++++-- shared.tf | 10 ++++++++-- single-lambda-integration.tf | 1 + variables.tf | 20 ++++++++++++++++++++ 7 files changed, 44 insertions(+), 8 deletions(-) diff --git a/locals.tf b/locals.tf index 613b9cb..08f2bf7 100644 --- a/locals.tf +++ b/locals.tf @@ -1,6 +1,8 @@ locals { - resource_name_pattern = "spectral-${var.integration_type}-integration-${var.environment}" + resource_name_pattern = "${coalesce(var.resource_name_common_part, "spectral-${var.integration_type}-integration-${var.environment}")}-${random_string.random_resource_name_suffix.id}" single_lambda_integration = contains(["jira", "terraform"], var.integration_type) ? true : false - multiple_lambda_integration = contains(["gitlab"], var.integration_type) ? true : false + multiple_lambda_integration = contains(["gitlab", "github"], var.integration_type) ? true : false api_triggered_function_arn = local.single_lambda_integration ? module.lambda_function[0].lambda_function_arn : module.frontend_lambda_function[0].lambda_function_arn + frontend_lambda_handler = contains(["github"], var.integration_type) ? "index.handler" : "frontend.app" + backend_lambda_handler = contains(["github"], var.integration_type) ? "index.handler" : "backend.app" } \ No newline at end of file diff --git a/modules/lambda/lambda.tf b/modules/lambda/lambda.tf index 3608858..9d06039 100644 --- a/modules/lambda/lambda.tf +++ b/modules/lambda/lambda.tf @@ -1,6 +1,6 @@ locals { - runtime = "nodejs14.x" - lambda_source_code_zip_path = "${path.module}/source_code/${var.integration_type}/${var.lambda_source_code_filename}" + runtime = "nodejs20.x" + lambda_source_code_zip_path = "${coalesce(var.lambda_source_code_path, "${path.module}/source_code/${var.integration_type}")}/${var.lambda_source_code_filename}" } resource "aws_lambda_function" "spectral_scanner_lambda" { diff --git a/modules/lambda/variables.tf b/modules/lambda/variables.tf index 88956b8..766e710 100644 --- a/modules/lambda/variables.tf +++ b/modules/lambda/variables.tf @@ -81,6 +81,11 @@ variable "lambda_source_code_filename" { description = "The lambda source code filename" } +variable "lambda_source_code_path" { + type = string + description = "The lambda source code path" +} + variable "role_arn" { type = string description = "The lambda source code filename" diff --git a/multiple-lambdas-integration.tf b/multiple-lambdas-integration.tf index fe7555a..afc554a 100644 --- a/multiple-lambdas-integration.tf +++ b/multiple-lambdas-integration.tf @@ -9,13 +9,14 @@ module "frontend_lambda_function" { env_vars = var.env_vars logs_retention_in_days = var.lambda_logs_retention_in_days should_write_logs = var.lambda_enable_logs - lambda_handler = "frontend.app" + lambda_handler = local.frontend_lambda_handler timeout = var.lambda_function_timeout memory_size = var.lambda_function_memory_size publish = var.lambda_publish secrets_arns = var.store_secret_in_secrets_manager ? module.secrets_manager[0].secrets_arns : [] store_secret_in_secrets_manager = var.store_secret_in_secrets_manager lambda_source_code_filename = "frontend.zip" + lambda_source_code_path = var.frontend_lambda_source_code_path role_arn = module.lambda_role.lambda_role_arn } @@ -30,13 +31,14 @@ module "backend_lambda_function" { env_vars = var.env_vars logs_retention_in_days = var.lambda_logs_retention_in_days should_write_logs = var.lambda_enable_logs - lambda_handler = "backend.app" + lambda_handler = local.backend_lambda_handler timeout = var.lambda_function_timeout memory_size = var.lambda_function_memory_size publish = var.lambda_publish secrets_arns = var.store_secret_in_secrets_manager ? module.secrets_manager[0].secrets_arns : [] store_secret_in_secrets_manager = var.store_secret_in_secrets_manager lambda_source_code_filename = "backend.zip" + lambda_source_code_path = var.backend_lambda_source_code_path role_arn = module.lambda_role.lambda_role_arn } diff --git a/shared.tf b/shared.tf index 383ade2..91ca8de 100644 --- a/shared.tf +++ b/shared.tf @@ -1,10 +1,16 @@ +resource "random_string" "random_resource_name_suffix" { + length = 10 + special = false + upper = false +} + module "api_gateway" { source = "./modules/api_gateway" global_tags = var.global_tags tags = var.tags environment = var.environment integration_type = var.integration_type - resource_name_pattern = local.single_lambda_integration ? local.resource_name_pattern : "${local.resource_name_pattern}-frontend" + resource_name_pattern = local.single_lambda_integration ? local.resource_name_pattern : "${local.resource_name_pattern}-frontend-${random_string.random_resource_name_suffix.id}" lambda_function_arn = local.api_triggered_function_arn } @@ -16,7 +22,7 @@ module "secrets_manager" { module "lambda_role" { source = "./modules/role" - resource_name_pattern = local.single_lambda_integration ? local.resource_name_pattern : "${local.resource_name_pattern}-frontend" + resource_name_pattern = local.single_lambda_integration ? local.resource_name_pattern : "${local.resource_name_pattern}-frontend-${random_string.random_resource_name_suffix.id}" store_secret_in_secrets_manager = var.store_secret_in_secrets_manager secrets_arns = var.store_secret_in_secrets_manager ? module.secrets_manager[0].secrets_arns : [] tags = var.tags diff --git a/single-lambda-integration.tf b/single-lambda-integration.tf index c6b4da8..2cf9299 100644 --- a/single-lambda-integration.tf +++ b/single-lambda-integration.tf @@ -15,5 +15,6 @@ module "lambda_function" { secrets_arns = var.store_secret_in_secrets_manager ? module.secrets_manager[0].secrets_arns : [] store_secret_in_secrets_manager = var.store_secret_in_secrets_manager lambda_source_code_filename = "app.zip" + lambda_source_code_path = var.lambda_source_code_path role_arn = module.lambda_role.lambda_role_arn } \ No newline at end of file diff --git a/variables.tf b/variables.tf index 5ae6987..cffdfa1 100644 --- a/variables.tf +++ b/variables.tf @@ -10,6 +10,21 @@ variable "integration_type" { } } +variable "lambda_source_code_path" { + type = string + description = "Path to the lambda source code zip file" +} + +variable "frontend_lambda_source_code_path" { + type = string + description = "Path to the lambda source code zip file of the frontend lambda" +} + +variable "backend_lambda_source_code_path" { + type = string + description = "Path to the lambda source code zip file of the backend lambda" +} + variable "environment" { type = string description = "The target environment name for deployment." @@ -79,4 +94,9 @@ variable "store_secret_in_secrets_manager" { type = bool description = "Whether to store your secrets in secrets manager, default is false" default = false +} + +variable "resource_name_common_part" { + type = string + description = "A common part for all resources created under the stack" } \ No newline at end of file