diff --git a/main.tf b/main.tf index 3a36b1b..0c218ec 100644 --- a/main.tf +++ b/main.tf @@ -295,6 +295,8 @@ resource "aws_lambda_function" "this" { ################################################ resource "aws_cloudwatch_event_rule" "this" { + count = var.cloudwatch_schedule_expression != "none" ? 1 : 0 + name = "trigger-lambda-scheduler-${var.name}" description = "Trigger lambda scheduler" schedule_expression = var.cloudwatch_schedule_expression @@ -302,16 +304,20 @@ resource "aws_cloudwatch_event_rule" "this" { } resource "aws_cloudwatch_event_target" "this" { - arn = aws_lambda_function.this.arn - rule = aws_cloudwatch_event_rule.this.name + count = var.cloudwatch_schedule_expression != "none" ? 1 : 0 + + arn = aws_lambda_function.this.arn + rule = aws_cloudwatch_event_rule.this[0].name } resource "aws_lambda_permission" "this" { + count = var.cloudwatch_schedule_expression != "none" ? 1 : 0 + statement_id = "AllowExecutionFromCloudWatch" action = "lambda:InvokeFunction" principal = "events.amazonaws.com" function_name = aws_lambda_function.this.function_name - source_arn = aws_cloudwatch_event_rule.this.arn + source_arn = aws_cloudwatch_event_rule.this[0].arn } ################################################ @@ -324,3 +330,14 @@ resource "aws_cloudwatch_log_group" "this" { retention_in_days = 14 tags = var.tags } + +################################################ +# +# HTTP ENDPOINT TO TRIGGER LAMBDA +# +################################################ +resource "aws_lambda_function_url" "http_trigger" { + count = var.http_trigger ? 1 : 0 + function_name = aws_lambda_function.this.function_name + authorization_type = var.http_trigger_authorization_type +} diff --git a/outputs.tf b/outputs.tf index f77de87..efa0921 100644 --- a/outputs.tf +++ b/outputs.tf @@ -42,3 +42,8 @@ output "scheduler_log_group_arn" { description = "The Amazon Resource Name (ARN) specifying the log group" value = aws_cloudwatch_log_group.this.arn } + +output "http_trigger" { + description = "The http trigger if set" + value = var.http_trigger ? aws_lambda_function_url.http_trigger[0].function_url : "none" +} diff --git a/variables.tf b/variables.tf index 2b7063c..77e88dd 100644 --- a/variables.tf +++ b/variables.tf @@ -4,7 +4,7 @@ # trigger lambda functuon every night at 22h00 from Monday to Friday # cf doc : https://docs.aws.amazon.com/lambda/latest/dg/tutorial-scheduled-events-schedule-expressions.html variable "cloudwatch_schedule_expression" { - description = "Define the aws cloudwatch event rule schedule expression" + description = "Define the aws cloudwatch event rule schedule expression ('none' for no schedule)" type = string default = "cron(0 22 ? * MON-FRI *)" } @@ -108,3 +108,15 @@ variable "tags" { type = map(any) default = null } + +variable "http_trigger" { + description = "Create an http endpoint to trigger the lambda" + type = bool + default = false +} + +variable "http_trigger_authorization_type" { + description = "Authorization type for the http endpoint to trigger the lambda" + type = string + default = "NONE" +}