cloudwatch.tf 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. resource "aws_cloudwatch_log_group" "function_scheduler" {
  2. name = "/aws/lambda/${aws_lambda_function.portal_scheduler.function_name}"
  3. retention_in_days = 14
  4. tags = merge(var.standard_tags, var.tags)
  5. }
  6. resource "aws_cloudwatch_log_group" "function_customer_sync" {
  7. name = "/aws/lambda/${aws_lambda_function.portal_customer_sync.function_name}"
  8. retention_in_days = 14
  9. tags = merge(var.standard_tags, var.tags)
  10. }
  11. ###
  12. ### Trigger Portal Scheduler Lambda with Rules and Targets
  13. ###
  14. ### Time-based rules for portal sync:
  15. resource "aws_cloudwatch_event_rule" "portal_scheduler_quarter_hourly_rule" {
  16. name = "aws-portal-lambda-scheduler-quarter-hourly"
  17. description = "Rule for portal scheduler lambda function - every 15 minutes"
  18. schedule_expression = "rate(15 minutes)"
  19. is_enabled = var.environment == "test" ? false : true
  20. tags = merge(var.standard_tags, var.tags)
  21. }
  22. resource "aws_cloudwatch_event_rule" "portal_scheduler_half_hourly_rule" {
  23. name = "aws-portal-lambda-scheduler-half-hourly"
  24. description = "Rule for portal scheduler lambda function - every 30 minutes"
  25. schedule_expression = "rate(30 minutes)"
  26. is_enabled = var.environment == "test" ? false : true
  27. tags = merge(var.standard_tags, var.tags)
  28. }
  29. resource "aws_cloudwatch_event_rule" "portal_scheduler_hourly_rule" {
  30. name = "aws-portal-lambda-scheduler-hourly"
  31. description = "Rule for portal scheduler lambda function - every hour"
  32. schedule_expression = "rate(1 hour)"
  33. is_enabled = var.environment == "test" ? false : true
  34. tags = merge(var.standard_tags, var.tags)
  35. }
  36. resource "aws_cloudwatch_event_rule" "portal_scheduler_four_hourly_rule" {
  37. name = "aws-portal-lambda-scheduler-four-hourly"
  38. description = "Rule for portal scheduler lambda function - every 4 hours"
  39. schedule_expression = "rate(4 hours)"
  40. is_enabled = var.environment == "test" ? false : true
  41. tags = merge(var.standard_tags, var.tags)
  42. }
  43. resource "aws_cloudwatch_event_rule" "portal_scheduler_daily_rule" {
  44. name = "aws-portal-lambda-scheduler-daily"
  45. description = "Rule for portal scheduler lambda function - every day"
  46. schedule_expression = "cron(5 5 * * ? *)"
  47. is_enabled = var.environment == "test" ? false : true
  48. tags = merge(var.standard_tags, var.tags)
  49. }
  50. resource "aws_cloudwatch_event_rule" "portal_scheduler_weekly_rule" {
  51. name = "aws-portal-lambda-scheduler-weekly"
  52. description = "Rule for portal scheduler lambda function - every week"
  53. schedule_expression = "rate(7 days)"
  54. is_enabled = var.environment == "test" ? false : true
  55. tags = merge(var.standard_tags, var.tags)
  56. }
  57. resource "aws_cloudwatch_event_rule" "portal_scheduler_monthly_rule" {
  58. name = "aws-portal-lambda-scheduler-monthly"
  59. description = "Rule for portal scheduler lambda function - every month"
  60. schedule_expression = "rate(30 days)"
  61. is_enabled = var.environment == "test" ? false : true
  62. tags = merge(var.standard_tags, var.tags)
  63. }
  64. ### Time-based targets for portal scheduler:
  65. resource "aws_cloudwatch_event_target" "portal_scheduler_cloudwatch_target_quarter_hourly" {
  66. target_id = "PortalSchedulerQuarterHourly"
  67. rule = aws_cloudwatch_event_rule.portal_scheduler_quarter_hourly_rule.name
  68. input = "{\"frequency_identifier\":\"quarter-hourly\"}"
  69. arn = aws_lambda_function.portal_scheduler.arn
  70. }
  71. resource "aws_cloudwatch_event_target" "portal_scheduler_cloudwatch_target_half_hourly" {
  72. target_id = "PortalSchedulerHalfHourly"
  73. rule = aws_cloudwatch_event_rule.portal_scheduler_half_hourly_rule.name
  74. input = "{\"frequency_identifier\":\"half-hourly\"}"
  75. arn = aws_lambda_function.portal_scheduler.arn
  76. }
  77. resource "aws_cloudwatch_event_target" "portal_scheduler_cloudwatch_target_hourly" {
  78. target_id = "PortalSchedulerHourly"
  79. rule = aws_cloudwatch_event_rule.portal_scheduler_hourly_rule.name
  80. input = "{\"frequency_identifier\":\"hourly\"}"
  81. arn = aws_lambda_function.portal_scheduler.arn
  82. }
  83. resource "aws_cloudwatch_event_target" "portal_scheduler_cloudwatch_target_four_hourly" {
  84. target_id = "PortalSchedulerFourHourly"
  85. rule = aws_cloudwatch_event_rule.portal_scheduler_four_hourly_rule.name
  86. input = "{\"frequency_identifier\":\"four-hourly\"}"
  87. arn = aws_lambda_function.portal_scheduler.arn
  88. }
  89. resource "aws_cloudwatch_event_target" "portal_scheduler_cloudwatch_target_daily" {
  90. target_id = "PortalSchedulerDaily"
  91. rule = aws_cloudwatch_event_rule.portal_scheduler_daily_rule.name
  92. input = "{\"frequency_identifier\":\"daily\"}"
  93. arn = aws_lambda_function.portal_scheduler.arn
  94. }
  95. resource "aws_cloudwatch_event_target" "portal_scheduler_cloudwatch_target_weekly" {
  96. target_id = "PortalSchedulerWeekly"
  97. rule = aws_cloudwatch_event_rule.portal_scheduler_weekly_rule.name
  98. input = "{\"frequency_identifier\":\"weekly\"}"
  99. arn = aws_lambda_function.portal_scheduler.arn
  100. }
  101. resource "aws_cloudwatch_event_target" "portal_scheduler_cloudwatch_target_monthly" {
  102. target_id = "PortalSchedulerMonthly"
  103. rule = aws_cloudwatch_event_rule.portal_scheduler_monthly_rule.name
  104. input = "{\"frequency_identifier\":\"monthly\"}"
  105. arn = aws_lambda_function.portal_scheduler.arn
  106. }
  107. ### Invoke permissions for Time-based rules for portal sync:
  108. resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_scheduler_quarter_hourly" {
  109. statement_id = "AllowExecutionFromCloudWatchQuarterHourly"
  110. action = "lambda:InvokeFunction"
  111. function_name = aws_lambda_function.portal_scheduler.function_name
  112. principal = "events.amazonaws.com"
  113. source_arn = aws_cloudwatch_event_rule.portal_scheduler_quarter_hourly_rule.arn
  114. }
  115. resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_scheduler_half_hourly" {
  116. statement_id = "AllowExecutionFromCloudWatchHalfHourly"
  117. action = "lambda:InvokeFunction"
  118. function_name = aws_lambda_function.portal_scheduler.function_name
  119. principal = "events.amazonaws.com"
  120. source_arn = aws_cloudwatch_event_rule.portal_scheduler_half_hourly_rule.arn
  121. }
  122. resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_scheduler_hourly" {
  123. statement_id = "AllowExecutionFromCloudWatchHourly"
  124. action = "lambda:InvokeFunction"
  125. function_name = aws_lambda_function.portal_scheduler.function_name
  126. principal = "events.amazonaws.com"
  127. source_arn = aws_cloudwatch_event_rule.portal_scheduler_hourly_rule.arn
  128. }
  129. resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_scheduler_four_hourly" {
  130. statement_id = "AllowExecutionFromCloudWatchFourHourly"
  131. action = "lambda:InvokeFunction"
  132. function_name = aws_lambda_function.portal_scheduler.function_name
  133. principal = "events.amazonaws.com"
  134. source_arn = aws_cloudwatch_event_rule.portal_scheduler_four_hourly_rule.arn
  135. }
  136. resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_scheduler_daily" {
  137. statement_id = "AllowExecutionFromCloudWatchDaily"
  138. action = "lambda:InvokeFunction"
  139. function_name = aws_lambda_function.portal_scheduler.function_name
  140. principal = "events.amazonaws.com"
  141. source_arn = aws_cloudwatch_event_rule.portal_scheduler_daily_rule.arn
  142. }
  143. resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_scheduler_weekly" {
  144. statement_id = "AllowExecutionFromCloudWatchWeekly"
  145. action = "lambda:InvokeFunction"
  146. function_name = aws_lambda_function.portal_scheduler.function_name
  147. principal = "events.amazonaws.com"
  148. source_arn = aws_cloudwatch_event_rule.portal_scheduler_weekly_rule.arn
  149. }
  150. resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_scheduler_monthly" {
  151. statement_id = "AllowExecutionFromCloudWatchMonthly"
  152. action = "lambda:InvokeFunction"
  153. function_name = aws_lambda_function.portal_scheduler.function_name
  154. principal = "events.amazonaws.com"
  155. source_arn = aws_cloudwatch_event_rule.portal_scheduler_monthly_rule.arn
  156. }