resource "aws_cloudwatch_log_group" "function" { name = "/aws/lambda/${aws_lambda_function.portal_data_sync.function_name}" retention_in_days = 14 tags = merge(var.standard_tags, var.tags) } ### ### Trigger Portal Sync Lambda with Rules and Targets ### ### Time-based rules for portal sync: resource "aws_cloudwatch_event_rule" "portal_event_quarter_hourly_rule" { name = "aws-portal-lambda-data-sync-quarter-hourly" description = "Rule for portal data sync lambda function - every 15 minutes" schedule_expression = "rate(15 minutes)" is_enabled = var.environment == "test" ? false : true tags = merge(var.standard_tags, var.tags) } resource "aws_cloudwatch_event_rule" "portal_event_half_hourly_rule" { name = "aws-portal-lambda-data-sync-half-hourly" description = "Rule for portal data sync lambda function - every 30 minutes" schedule_expression = "rate(30 minutes)" is_enabled = var.environment == "test" ? false : true tags = merge(var.standard_tags, var.tags) } resource "aws_cloudwatch_event_rule" "portal_event_hourly_rule" { name = "aws-portal-lambda-data-sync-hourly" description = "Rule for portal data sync lambda function - every hour" schedule_expression = "rate(1 hour)" is_enabled = var.environment == "test" ? false : true tags = merge(var.standard_tags, var.tags) } resource "aws_cloudwatch_event_rule" "portal_event_four_hourly_rule" { name = "aws-portal-lambda-data-sync-four-hourly" description = "Rule for portal data sync lambda function - every 4 hours" schedule_expression = "rate(4 hours)" is_enabled = var.environment == "test" ? false : true tags = merge(var.standard_tags, var.tags) } resource "aws_cloudwatch_event_rule" "portal_event_daily_rule" { name = "aws-portal-lambda-data-sync-daily" description = "Rule for portal data sync lambda function - every day" schedule_expression = "cron(5 5 * * ? *)" is_enabled = var.environment == "test" ? false : true tags = merge(var.standard_tags, var.tags) } resource "aws_cloudwatch_event_rule" "portal_event_weekly_rule" { name = "aws-portal-lambda-data-sync-weekly" description = "Rule for portal data sync lambda function - every week" schedule_expression = "rate(7 days)" is_enabled = var.environment == "test" ? false : true tags = merge(var.standard_tags, var.tags) } resource "aws_cloudwatch_event_rule" "portal_event_monthly_rule" { name = "aws-portal-lambda-data-sync-monthly" description = "Rule for portal data sync lambda function - every month" schedule_expression = "rate(30 days)" is_enabled = var.environment == "test" ? false : true tags = merge(var.standard_tags, var.tags) } ### Time-based targets for portal sync: resource "aws_cloudwatch_event_target" "portal_lambda_cloudwatch_target_quarter_hourly" { target_id = "PortalSyncQuarterHourly" rule = aws_cloudwatch_event_rule.portal_event_quarter_hourly_rule.name input = "{\"frequency_identifier\":\"quarter-hourly\"}" arn = aws_lambda_function.portal_data_sync.arn } resource "aws_cloudwatch_event_target" "portal_lambda_cloudwatch_target_half_hourly" { target_id = "PortalSyncHalfHourly" rule = aws_cloudwatch_event_rule.portal_event_half_hourly_rule.name input = "{\"frequency_identifier\":\"half-hourly\"}" arn = aws_lambda_function.portal_data_sync.arn } resource "aws_cloudwatch_event_target" "portal_lambda_cloudwatch_target_hourly" { target_id = "PortalSyncHourly" rule = aws_cloudwatch_event_rule.portal_event_hourly_rule.name input = "{\"frequency_identifier\":\"hourly\"}" arn = aws_lambda_function.portal_data_sync.arn } resource "aws_cloudwatch_event_target" "portal_lambda_cloudwatch_target_four_hourly" { target_id = "PortalSyncFourHourly" rule = aws_cloudwatch_event_rule.portal_event_four_hourly_rule.name input = "{\"frequency_identifier\":\"four-hourly\"}" arn = aws_lambda_function.portal_data_sync.arn } resource "aws_cloudwatch_event_target" "portal_lambda_cloudwatch_target_daily" { target_id = "PortalSyncDaily" rule = aws_cloudwatch_event_rule.portal_event_daily_rule.name input = "{\"frequency_identifier\":\"daily\"}" arn = aws_lambda_function.portal_data_sync.arn } resource "aws_cloudwatch_event_target" "portal_lambda_cloudwatch_target_weekly" { target_id = "PortalSyncWeekly" rule = aws_cloudwatch_event_rule.portal_event_weekly_rule.name input = "{\"frequency_identifier\":\"weekly\"}" arn = aws_lambda_function.portal_data_sync.arn } resource "aws_cloudwatch_event_target" "portal_lambda_cloudwatch_target_monthly" { target_id = "PortalSyncMonthly" rule = aws_cloudwatch_event_rule.portal_event_monthly_rule.name input = "{\"frequency_identifier\":\"monthly\"}" arn = aws_lambda_function.portal_data_sync.arn } ### Invoke permissions for Time-based rules for portal sync: resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_lambda_quarter_hourly" { statement_id = "AllowExecutionFromCloudWatchQuarterHourly" action = "lambda:InvokeFunction" function_name = aws_lambda_function.portal_data_sync.function_name principal = "events.amazonaws.com" source_arn = aws_cloudwatch_event_rule.portal_event_quarter_hourly_rule.arn } resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_lambda_half_hourly" { statement_id = "AllowExecutionFromCloudWatchHalfHourly" action = "lambda:InvokeFunction" function_name = aws_lambda_function.portal_data_sync.function_name principal = "events.amazonaws.com" source_arn = aws_cloudwatch_event_rule.portal_event_half_hourly_rule.arn } resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_lambda_hourly" { statement_id = "AllowExecutionFromCloudWatchHourly" action = "lambda:InvokeFunction" function_name = aws_lambda_function.portal_data_sync.function_name principal = "events.amazonaws.com" source_arn = aws_cloudwatch_event_rule.portal_event_hourly_rule.arn } resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_lambda_four_hourly" { statement_id = "AllowExecutionFromCloudWatchFourHourly" action = "lambda:InvokeFunction" function_name = aws_lambda_function.portal_data_sync.function_name principal = "events.amazonaws.com" source_arn = aws_cloudwatch_event_rule.portal_event_four_hourly_rule.arn } resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_lambda_daily" { statement_id = "AllowExecutionFromCloudWatchDaily" action = "lambda:InvokeFunction" function_name = aws_lambda_function.portal_data_sync.function_name principal = "events.amazonaws.com" source_arn = aws_cloudwatch_event_rule.portal_event_daily_rule.arn } resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_lambda_weekly" { statement_id = "AllowExecutionFromCloudWatchWeekly" action = "lambda:InvokeFunction" function_name = aws_lambda_function.portal_data_sync.function_name principal = "events.amazonaws.com" source_arn = aws_cloudwatch_event_rule.portal_event_weekly_rule.arn } resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_lambda_monthly" { statement_id = "AllowExecutionFromCloudWatchMonthly" action = "lambda:InvokeFunction" function_name = aws_lambda_function.portal_data_sync.function_name principal = "events.amazonaws.com" source_arn = aws_cloudwatch_event_rule.portal_event_monthly_rule.arn }