cloudwatch.tf 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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(local.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(local.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(local.standard_tags, var.tags)
  21. }
  22. resource "aws_cloudwatch_event_rule" "portal_scheduler_third_hourly_rule" {
  23. name = "aws-portal-lambda-scheduler-third-hourly"
  24. description = "Rule for portal scheduler lambda function - every 20th minute"
  25. schedule_expression = "cron(0/20 * * * ? *)"
  26. is_enabled = var.environment == "test" ? false : true
  27. tags = merge(local.standard_tags, var.tags)
  28. }
  29. resource "aws_cloudwatch_event_rule" "portal_scheduler_half_hourly_rule" {
  30. name = "aws-portal-lambda-scheduler-half-hourly"
  31. description = "Rule for portal scheduler lambda function - every 30 minutes"
  32. schedule_expression = "rate(30 minutes)"
  33. is_enabled = var.environment == "test" ? false : true
  34. tags = merge(local.standard_tags, var.tags)
  35. }
  36. resource "aws_cloudwatch_event_rule" "portal_scheduler_hourly_rule" {
  37. name = "aws-portal-lambda-scheduler-hourly"
  38. description = "Rule for portal scheduler lambda function - every hour"
  39. schedule_expression = "rate(1 hour)"
  40. is_enabled = var.environment == "test" ? false : true
  41. tags = merge(local.standard_tags, var.tags)
  42. }
  43. resource "aws_cloudwatch_event_rule" "portal_scheduler_four_hourly_rule" {
  44. name = "aws-portal-lambda-scheduler-four-hourly"
  45. description = "Rule for portal scheduler lambda function - every 4 hours"
  46. schedule_expression = "rate(4 hours)"
  47. is_enabled = var.environment == "test" ? false : true
  48. tags = merge(local.standard_tags, var.tags)
  49. }
  50. resource "aws_cloudwatch_event_rule" "portal_scheduler_daily_rule" {
  51. name = "aws-portal-lambda-scheduler-daily"
  52. description = "Rule for portal scheduler lambda function - every day"
  53. schedule_expression = "cron(5 5 * * ? *)"
  54. is_enabled = var.environment == "test" ? false : true
  55. tags = merge(local.standard_tags, var.tags)
  56. }
  57. resource "aws_cloudwatch_event_rule" "portal_scheduler_weekly_rule" {
  58. name = "aws-portal-lambda-scheduler-weekly"
  59. description = "Rule for portal scheduler lambda function - every week"
  60. schedule_expression = "rate(7 days)"
  61. is_enabled = var.environment == "test" ? false : true
  62. tags = merge(local.standard_tags, var.tags)
  63. }
  64. resource "aws_cloudwatch_event_rule" "portal_scheduler_monthly_rule" {
  65. name = "aws-portal-lambda-scheduler-monthly"
  66. description = "Rule for portal scheduler lambda function - every month"
  67. schedule_expression = "cron(0 17 1 * ? *)"
  68. is_enabled = var.environment == "test" ? false : true
  69. tags = merge(local.standard_tags, var.tags)
  70. }
  71. ### Time-based targets for portal scheduler:
  72. resource "aws_cloudwatch_event_target" "portal_scheduler_cloudwatch_target_quarter_hourly" {
  73. target_id = "PortalSchedulerQuarterHourly"
  74. rule = aws_cloudwatch_event_rule.portal_scheduler_quarter_hourly_rule.name
  75. input = "{\"frequency_identifier\":\"quarter-hourly\"}"
  76. arn = aws_lambda_function.portal_scheduler.arn
  77. }
  78. resource "aws_cloudwatch_event_target" "portal_scheduler_cloudwatch_target_third_hourly" {
  79. target_id = "PortalSchedulerThirdHourly"
  80. rule = aws_cloudwatch_event_rule.portal_scheduler_third_hourly_rule.name
  81. input = "{\"frequency_identifier\":\"threat-q-twenty-minute\"}"
  82. arn = aws_lambda_function.portal_scheduler.arn
  83. }
  84. resource "aws_cloudwatch_event_target" "portal_scheduler_cloudwatch_target_half_hourly" {
  85. target_id = "PortalSchedulerHalfHourly"
  86. rule = aws_cloudwatch_event_rule.portal_scheduler_half_hourly_rule.name
  87. input = "{\"frequency_identifier\":\"half-hourly\"}"
  88. arn = aws_lambda_function.portal_scheduler.arn
  89. }
  90. resource "aws_cloudwatch_event_target" "portal_scheduler_cloudwatch_target_hourly" {
  91. target_id = "PortalSchedulerHourly"
  92. rule = aws_cloudwatch_event_rule.portal_scheduler_hourly_rule.name
  93. input = "{\"frequency_identifier\":\"hourly\"}"
  94. arn = aws_lambda_function.portal_scheduler.arn
  95. }
  96. resource "aws_cloudwatch_event_target" "portal_scheduler_cloudwatch_target_four_hourly" {
  97. target_id = "PortalSchedulerFourHourly"
  98. rule = aws_cloudwatch_event_rule.portal_scheduler_four_hourly_rule.name
  99. input = "{\"frequency_identifier\":\"four-hourly\"}"
  100. arn = aws_lambda_function.portal_scheduler.arn
  101. }
  102. resource "aws_cloudwatch_event_target" "portal_scheduler_cloudwatch_target_daily" {
  103. target_id = "PortalSchedulerDaily"
  104. rule = aws_cloudwatch_event_rule.portal_scheduler_daily_rule.name
  105. input = "{\"frequency_identifier\":\"daily\"}"
  106. arn = aws_lambda_function.portal_scheduler.arn
  107. }
  108. resource "aws_cloudwatch_event_target" "portal_scheduler_cloudwatch_target_weekly" {
  109. target_id = "PortalSchedulerWeekly"
  110. rule = aws_cloudwatch_event_rule.portal_scheduler_weekly_rule.name
  111. input = "{\"frequency_identifier\":\"weekly\"}"
  112. arn = aws_lambda_function.portal_scheduler.arn
  113. }
  114. resource "aws_cloudwatch_event_target" "portal_scheduler_cloudwatch_target_monthly" {
  115. target_id = "PortalSchedulerMonthly"
  116. rule = aws_cloudwatch_event_rule.portal_scheduler_monthly_rule.name
  117. input = "{\"frequency_identifier\":\"monthly\"}"
  118. arn = aws_lambda_function.portal_scheduler.arn
  119. }
  120. ### Invoke permissions for Time-based rules for portal sync:
  121. resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_scheduler_quarter_hourly" {
  122. statement_id = "AllowExecutionFromCloudWatchQuarterHourly"
  123. action = "lambda:InvokeFunction"
  124. function_name = aws_lambda_function.portal_scheduler.function_name
  125. principal = "events.amazonaws.com"
  126. source_arn = aws_cloudwatch_event_rule.portal_scheduler_quarter_hourly_rule.arn
  127. }
  128. resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_scheduler_third_hourly" {
  129. statement_id = "AllowExecutionFromCloudWatchThirdHourly"
  130. action = "lambda:InvokeFunction"
  131. function_name = aws_lambda_function.portal_scheduler.function_name
  132. principal = "events.amazonaws.com"
  133. source_arn = aws_cloudwatch_event_rule.portal_scheduler_third_hourly_rule.arn
  134. }
  135. resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_scheduler_half_hourly" {
  136. statement_id = "AllowExecutionFromCloudWatchHalfHourly"
  137. action = "lambda:InvokeFunction"
  138. function_name = aws_lambda_function.portal_scheduler.function_name
  139. principal = "events.amazonaws.com"
  140. source_arn = aws_cloudwatch_event_rule.portal_scheduler_half_hourly_rule.arn
  141. }
  142. resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_scheduler_hourly" {
  143. statement_id = "AllowExecutionFromCloudWatchHourly"
  144. action = "lambda:InvokeFunction"
  145. function_name = aws_lambda_function.portal_scheduler.function_name
  146. principal = "events.amazonaws.com"
  147. source_arn = aws_cloudwatch_event_rule.portal_scheduler_hourly_rule.arn
  148. }
  149. resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_scheduler_four_hourly" {
  150. statement_id = "AllowExecutionFromCloudWatchFourHourly"
  151. action = "lambda:InvokeFunction"
  152. function_name = aws_lambda_function.portal_scheduler.function_name
  153. principal = "events.amazonaws.com"
  154. source_arn = aws_cloudwatch_event_rule.portal_scheduler_four_hourly_rule.arn
  155. }
  156. resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_scheduler_daily" {
  157. statement_id = "AllowExecutionFromCloudWatchDaily"
  158. action = "lambda:InvokeFunction"
  159. function_name = aws_lambda_function.portal_scheduler.function_name
  160. principal = "events.amazonaws.com"
  161. source_arn = aws_cloudwatch_event_rule.portal_scheduler_daily_rule.arn
  162. }
  163. resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_scheduler_weekly" {
  164. statement_id = "AllowExecutionFromCloudWatchWeekly"
  165. action = "lambda:InvokeFunction"
  166. function_name = aws_lambda_function.portal_scheduler.function_name
  167. principal = "events.amazonaws.com"
  168. source_arn = aws_cloudwatch_event_rule.portal_scheduler_weekly_rule.arn
  169. }
  170. resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_scheduler_monthly" {
  171. statement_id = "AllowExecutionFromCloudWatchMonthly"
  172. action = "lambda:InvokeFunction"
  173. function_name = aws_lambda_function.portal_scheduler.function_name
  174. principal = "events.amazonaws.com"
  175. source_arn = aws_cloudwatch_event_rule.portal_scheduler_monthly_rule.arn
  176. }