Skip to content

Commit

Permalink
deploying lambda out of module
Browse files Browse the repository at this point in the history
  • Loading branch information
tajewole-moj committed Oct 24, 2023
1 parent 478a144 commit 0c280db
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 16 deletions.
40 changes: 40 additions & 0 deletions terraform/environments/apex/event_triggers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
resource "aws_cloudwatch_event_rule" "snapshotDBFunctionmon_sun" {
name = "laa-createSnapshotRule-LWN8E1LNHFJR"
description = "Fires every five minutes"
schedule_expression = "cron(38 09 ? * MON-SUN *)"
}

resource "aws_cloudwatch_event_target" "snapshotDBFunctioncheck_mon_sun" {
rule = aws_cloudwatch_event_rule.snapshotDBFunctionmon_sun.name
arn = aws_lambda_function.snapshotDBFunction.arn
input =jsonencode({"appname": "apex Database Server"})
}

resource "aws_lambda_permission" "allow_cloudwatch_to_call_check_mon_sun" {
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.snapshotDBFunction.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.snapshotDBFunctionmon_sun.arn
}


resource "aws_cloudwatch_event_rule" "deletesnapshotFunction_mon_fri" {
name = "laa-deletesnapshotRule-LWN8E1LNHFJR"
description = "Fires every five minutes"
schedule_expression = "cron(38 09 ? * MON-SUN *)"
}

resource "aws_cloudwatch_event_target" "deletesnapshotFunctioncheck_mon_fri" {
rule = aws_cloudwatch_event_rule.deletesnapshotFunction_mon_fri.name
arn = aws_lambda_function.deletesnapshotFunction.arn
input =jsonencode({"appname": "apex Database Server"})
}

resource "aws_lambda_permission" "allow_cloudwatch_to_call_check_mon_fri" {
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.deletesnapshotFunction.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.deletesnapshotFunction_mon_fri.arn
}
115 changes: 99 additions & 16 deletions terraform/environments/apex/lambda.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,112 @@ module "s3_bucket_lambda" {

}

module "lambda_backup" {
source = "./modules/lambda"
vpc_id = data.aws_vpc.shared.id
security_grp_name = "${local.application_name}-${local.environment}-lambdaSecurityGroup"
backup_policy_name = "${local.application_name}-lambda-instance-policy"
source_file = local.dbsourcefiles
output_path = local.zipfiles
# filename = local.zipfiles
function_name = local.functions
handler = local.handlers
role = module.iambackup.backuprole
runtime = local.runtime
subnet_ids = [data.aws_subnet.private_subnets_a.id]
lamdbabucketname = "laa-${local.application_name}-${local.environment}-mp"
key = "dbsnapshot1.zip"
resource "aws_security_group" "lambdasg" {
name = "${local.application_name}-lambda-security-group"
description = "APEX Lambda Security Group"
vpc_id = data.aws_vpc.shared.id

egress {
description = "outbound access"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}

data "archive_file" "dbsnapshot_file" {
type = "zip"
source_file = local.dbsnapshot_source_file
output_path = local.dbsnapshot_output_path
}

data "archive_file" "deletesnapshot_file" {
type = "zip"
source_file = local.deletesnapshot_source_file
output_path = local.deletesnapshot_output_path
}

data "archive_file" "dbconnect_file" {
type = "zip"
source_file = local.dbconnect_source_file
output_path = local.dbconnect_output_path
}

tags = merge(

resource "aws_lambda_function" "snapshotDBFunction" {
function_name = local.snapshotDBFunctionname
role = module.iambackup.backuprole
handler = local.snapshotDBFunctionhandler
source_code_hash = data.archive_file.dbsnapshot_file.output_base64sha256
runtime = local.snapshotDBFunctionruntime
s3_bucket = module.s3_bucket_lambda.lambdabucketname
s3_key = local.snapshotDBFunctionfilename

environment {
variables = {
LD_LIBRARY_PATH = "/opt/nodejs/node_modules/lib"

}
}
vpc_config {
security_group_ids = [aws_security_group.lambdasg.id]
subnet_ids = [data.aws_subnet.private_subnets_a.id]
}
tags = merge(
local.tags,
{ Name = "laa-${local.application_name}-${local.environment}-mp" }
)
}

resource "aws_lambda_function" "deletesnapshotFunction" {
function_name = local.deletesnapshotFunctionname
role = module.iambackup.backuprole
handler = local.deletesnapshotFunctionhandler
source_code_hash = data.archive_file.delesnapshot_file.output_base64sha256
runtime = local.deletesnapshotFunctionruntime
s3_bucket = module.s3_bucket_lambda.lambdabucketname
s3_key = local.deletesnapshotFunctionfilename

environment {
variables = {
LD_LIBRARY_PATH = "/opt/nodejs/node_modules/lib"

}
}
vpc_config {
security_group_ids = [aws_security_group.lambdasg.id]
subnet_ids = [data.aws_subnet.private_subnets_a.id]
}
tags = merge(
local.tags,
{ Name = "laa-${local.application_name}-${local.environment}-mp" }
)
}


resource "aws_lambda_function" "connectDBFunction" {
function_name = local.connectDBFunctionname
role = module.iambackup.backuprole
handler = local.connectDBFunctionhandler
source_code_hash = data.archive_file.dbconnect_file.output_base64sha256
runtime = local.connectDBFunctionruntime
s3_bucket = module.s3_bucket_lambda.lambdabucketname
s3_key = local.connectDBFunctionfilename

environment {
variables = {
LD_LIBRARY_PATH = "/opt/nodejs/node_modules/lib"

}
}
vpc_config {
security_group_ids = [aws_security_group.lambdasg.id]
subnet_ids = [data.aws_subnet.private_subnets_a.id]
}
tags = merge(
local.tags,
{ Name = "laa-${local.application_name}-${local.environment}-mp" }
)
}

3 changes: 3 additions & 0 deletions terraform/environments/apex/modules/s3/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "lambdabucketname" {
value = aws_s3_bucket.laa-lambda-backup.bucket
}

0 comments on commit 0c280db

Please sign in to comment.