-
Notifications
You must be signed in to change notification settings - Fork 1
/
cloudwatch_events.tf
38 lines (35 loc) · 1.41 KB
/
cloudwatch_events.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
resource "aws_cloudwatch_event_rule" "this" {
count = length(local.event_schedule)
name = substr("${var.name}-${var.environment}-cloudwatch-event-${count.index}", 0, 64)
description = "Cloudwatch Event ${count.index} to Trigger Lambda ${var.name}-${var.environment}"
schedule_expression = length(regexall("^[0-9]+$", local.event_schedule[count.index])) == 0 ? local.event_schedule[count.index] : "rate(${local.event_schedule[count.index]} minutes)"
tags = merge(
local.tags,
var.global_tags,
{
"Environment" = var.environment,
"Name" = "${var.name}-${var.environment}"
}
)
}
resource "aws_cloudwatch_event_target" "this" {
count = length(local.event_schedule)
rule = aws_cloudwatch_event_rule.this[count.index].name
target_id = "${var.name}-${var.environment}-${count.index}"
arn = aws_lambda_function.this.arn
input = jsonencode(merge(
{
S3_BUCKET = var.s3_bucket
SSM_CONFIG_PATH = var.ssm_config_path
},
local.lambda_input[count.index]
))
}
resource "aws_lambda_permission" "this" {
count = length(local.event_schedule)
statement_id = "AllowExecutionFromCloudWatch-${count.index}"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.this.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.this[count.index].arn
}