Skip to content

Commit 9474e0d

Browse files
Added lambda module
1 parent 0d6d9b6 commit 9474e0d

4 files changed

Lines changed: 88 additions & 0 deletions

File tree

terraform/main.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,16 @@ module "api_gateway" {
4949
environment = var.environment
5050
lambda_invoke_arn = module.lambda.function_invoke_arn
5151
common_tags = local.common_tags
52+
}
53+
54+
# Lambda Module
55+
module "lambda" {
56+
source = "./modules/lambda"
57+
environment = var.environment
58+
lambda_role_arn = module.iam.lambda_role_arn
59+
dynamodb_table_name = module.dynamodb.table_name
60+
api_gateway_execution_arn = module.api_gateway.execution_arn
61+
lambda_funtion_dir = var.lambda_funtion_dir
62+
common_tags = local.common_tags
63+
depends_on = [module.api_gateway]
5264
}

terraform/modules/lambda/main.tf

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
data "archive_file" "lambda_zip" {
2+
type = "zip"
3+
source_dir = var.lambda_funtion_dir
4+
output_path = "${path.module}/${var.environment}_lambda_function.zip"
5+
}
6+
7+
resource "aws_lambda_function" "health_check" {
8+
function_name = "${var.environment}-health-check-function"
9+
role = var.lambda_role_arn
10+
handler = "lambda_function.lambda_handler"
11+
source_code_hash = data.archive_file.lambda_zip.output_base64sha256
12+
runtime = "python3.11"
13+
filename = data.archive_file.lambda_zip.output_path
14+
15+
environment {
16+
variables = {
17+
DYNAMODB_TABLE_NAME = var.dynamodb_table_name
18+
ENVIRONMENT = var.environment
19+
}
20+
}
21+
22+
tags = var.common_tags
23+
24+
depends_on = [data.archive_file.lambda_zip]
25+
}
26+
27+
resource "aws_lambda_permission" "api_gateway" {
28+
statement_id = "AllowAPIGatewayInvoke"
29+
action = "lambda:InvokeFunction"
30+
function_name = aws_lambda_function.health_check.function_name
31+
principal = "apigateway.amazonaws.com"
32+
source_arn = "${var.api_gateway_execution_arn}/*/*"
33+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
output "function_arn" {
2+
description = "ARN of the Lambda function"
3+
value = aws_lambda_function.health_check.arn
4+
}
5+
6+
output "function_name" {
7+
description = "Name of the Lambda function"
8+
value = aws_lambda_function.health_check.function_name
9+
}
10+
11+
output "function_invoke_arn" {
12+
description = "Invoke ARN of the Lambda function"
13+
value = aws_lambda_function.health_check.invoke_arn
14+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
variable "environment" {
2+
description = "Environment name"
3+
type = string
4+
}
5+
6+
variable "lambda_role_arn" {
7+
description = "ARN of the IAM role for Lambda"
8+
type = string
9+
}
10+
11+
variable "dynamodb_table_name" {
12+
description = "Name of the DynamoDB table"
13+
type = string
14+
}
15+
16+
variable "api_gateway_execution_arn" {
17+
description = "Execution ARN of the API Gateway"
18+
type = string
19+
}
20+
21+
variable "lambda_funtion_dir" {
22+
description = "Path to the Lambda function source code directory"
23+
type = string
24+
}
25+
26+
variable "common_tags" {
27+
description = "Common tags for all resources"
28+
type = map(string)
29+
}

0 commit comments

Comments
 (0)