cloudwatch.tf 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. resource "aws_cloudwatch_log_group" "function" {
  2. name = "/aws/lambda/${aws_lambda_function.portal_data_sync.function_name}"
  3. retention_in_days = 14
  4. tags = merge(var.standard_tags, var.tags)
  5. }
  6. resource "aws_cloudwatch_log_group" "function_scheduler" {
  7. name = "/aws/lambda/${aws_lambda_function.portal_scheduler.function_name}"
  8. retention_in_days = 14
  9. tags = merge(var.standard_tags, var.tags)
  10. }
  11. resource "aws_cloudwatch_log_group" "function_customer_sync" {
  12. name = "/aws/lambda/${aws_lambda_function.portal_customer_sync.function_name}"
  13. retention_in_days = 14
  14. tags = merge(var.standard_tags, var.tags)
  15. }
  16. ###
  17. ### Trigger Portal Sync Lambda with Rules and Targets
  18. ###
  19. ### Time-based rules for portal sync:
  20. resource "aws_cloudwatch_event_rule" "portal_event_quarter_hourly_rule" {
  21. name = "aws-portal-lambda-data-sync-quarter-hourly"
  22. description = "Rule for portal data sync lambda function - every 15 minutes"
  23. schedule_expression = "rate(15 minutes)"
  24. is_enabled = var.environment == "test" ? false : true
  25. tags = merge(var.standard_tags, var.tags)
  26. }
  27. resource "aws_cloudwatch_event_rule" "portal_event_half_hourly_rule" {
  28. name = "aws-portal-lambda-data-sync-half-hourly"
  29. description = "Rule for portal data sync lambda function - every 30 minutes"
  30. schedule_expression = "rate(30 minutes)"
  31. is_enabled = var.environment == "test" ? false : true
  32. tags = merge(var.standard_tags, var.tags)
  33. }
  34. resource "aws_cloudwatch_event_rule" "portal_event_hourly_rule" {
  35. name = "aws-portal-lambda-data-sync-hourly"
  36. description = "Rule for portal data sync lambda function - every hour"
  37. schedule_expression = "rate(1 hour)"
  38. is_enabled = var.environment == "test" ? false : true
  39. tags = merge(var.standard_tags, var.tags)
  40. }
  41. resource "aws_cloudwatch_event_rule" "portal_event_four_hourly_rule" {
  42. name = "aws-portal-lambda-data-sync-four-hourly"
  43. description = "Rule for portal data sync lambda function - every 4 hours"
  44. schedule_expression = "rate(4 hours)"
  45. is_enabled = var.environment == "test" ? false : true
  46. tags = merge(var.standard_tags, var.tags)
  47. }
  48. resource "aws_cloudwatch_event_rule" "portal_event_daily_rule" {
  49. name = "aws-portal-lambda-data-sync-daily"
  50. description = "Rule for portal data sync lambda function - every day"
  51. schedule_expression = "cron(5 5 * * ? *)"
  52. is_enabled = var.environment == "test" ? false : true
  53. tags = merge(var.standard_tags, var.tags)
  54. }
  55. resource "aws_cloudwatch_event_rule" "portal_event_weekly_rule" {
  56. name = "aws-portal-lambda-data-sync-weekly"
  57. description = "Rule for portal data sync lambda function - every week"
  58. schedule_expression = "rate(7 days)"
  59. is_enabled = var.environment == "test" ? false : true
  60. tags = merge(var.standard_tags, var.tags)
  61. }
  62. resource "aws_cloudwatch_event_rule" "portal_event_monthly_rule" {
  63. name = "aws-portal-lambda-data-sync-monthly"
  64. description = "Rule for portal data sync lambda function - every month"
  65. schedule_expression = "rate(30 days)"
  66. is_enabled = var.environment == "test" ? false : true
  67. tags = merge(var.standard_tags, var.tags)
  68. }
  69. ### Time-based targets for portal sync:
  70. resource "aws_cloudwatch_event_target" "portal_lambda_cloudwatch_target_quarter_hourly" {
  71. target_id = "PortalSyncQuarterHourly"
  72. rule = aws_cloudwatch_event_rule.portal_event_quarter_hourly_rule.name
  73. input = "{\"frequency_identifier\":\"quarter-hourly\"}"
  74. arn = aws_lambda_function.portal_data_sync.arn
  75. }
  76. resource "aws_cloudwatch_event_target" "portal_lambda_cloudwatch_target_half_hourly" {
  77. target_id = "PortalSyncHalfHourly"
  78. rule = aws_cloudwatch_event_rule.portal_event_half_hourly_rule.name
  79. input = "{\"frequency_identifier\":\"half-hourly\"}"
  80. arn = aws_lambda_function.portal_data_sync.arn
  81. }
  82. resource "aws_cloudwatch_event_target" "portal_lambda_cloudwatch_target_hourly" {
  83. target_id = "PortalSyncHourly"
  84. rule = aws_cloudwatch_event_rule.portal_event_hourly_rule.name
  85. input = "{\"frequency_identifier\":\"hourly\"}"
  86. arn = aws_lambda_function.portal_data_sync.arn
  87. }
  88. resource "aws_cloudwatch_event_target" "portal_lambda_cloudwatch_target_four_hourly" {
  89. target_id = "PortalSyncFourHourly"
  90. rule = aws_cloudwatch_event_rule.portal_event_four_hourly_rule.name
  91. input = "{\"frequency_identifier\":\"four-hourly\"}"
  92. arn = aws_lambda_function.portal_data_sync.arn
  93. }
  94. resource "aws_cloudwatch_event_target" "portal_lambda_cloudwatch_target_daily" {
  95. target_id = "PortalSyncDaily"
  96. rule = aws_cloudwatch_event_rule.portal_event_daily_rule.name
  97. input = "{\"frequency_identifier\":\"daily\"}"
  98. arn = aws_lambda_function.portal_data_sync.arn
  99. }
  100. resource "aws_cloudwatch_event_target" "portal_lambda_cloudwatch_target_weekly" {
  101. target_id = "PortalSyncWeekly"
  102. rule = aws_cloudwatch_event_rule.portal_event_weekly_rule.name
  103. input = "{\"frequency_identifier\":\"weekly\"}"
  104. arn = aws_lambda_function.portal_data_sync.arn
  105. }
  106. resource "aws_cloudwatch_event_target" "portal_lambda_cloudwatch_target_monthly" {
  107. target_id = "PortalSyncMonthly"
  108. rule = aws_cloudwatch_event_rule.portal_event_monthly_rule.name
  109. input = "{\"frequency_identifier\":\"monthly\"}"
  110. arn = aws_lambda_function.portal_data_sync.arn
  111. }
  112. ### Invoke permissions for Time-based rules for portal sync:
  113. resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_lambda_quarter_hourly" {
  114. statement_id = "AllowExecutionFromCloudWatchQuarterHourly"
  115. action = "lambda:InvokeFunction"
  116. function_name = aws_lambda_function.portal_data_sync.function_name
  117. principal = "events.amazonaws.com"
  118. source_arn = aws_cloudwatch_event_rule.portal_event_quarter_hourly_rule.arn
  119. }
  120. resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_lambda_half_hourly" {
  121. statement_id = "AllowExecutionFromCloudWatchHalfHourly"
  122. action = "lambda:InvokeFunction"
  123. function_name = aws_lambda_function.portal_data_sync.function_name
  124. principal = "events.amazonaws.com"
  125. source_arn = aws_cloudwatch_event_rule.portal_event_half_hourly_rule.arn
  126. }
  127. resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_lambda_hourly" {
  128. statement_id = "AllowExecutionFromCloudWatchHourly"
  129. action = "lambda:InvokeFunction"
  130. function_name = aws_lambda_function.portal_data_sync.function_name
  131. principal = "events.amazonaws.com"
  132. source_arn = aws_cloudwatch_event_rule.portal_event_hourly_rule.arn
  133. }
  134. resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_lambda_four_hourly" {
  135. statement_id = "AllowExecutionFromCloudWatchFourHourly"
  136. action = "lambda:InvokeFunction"
  137. function_name = aws_lambda_function.portal_data_sync.function_name
  138. principal = "events.amazonaws.com"
  139. source_arn = aws_cloudwatch_event_rule.portal_event_four_hourly_rule.arn
  140. }
  141. resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_lambda_daily" {
  142. statement_id = "AllowExecutionFromCloudWatchDaily"
  143. action = "lambda:InvokeFunction"
  144. function_name = aws_lambda_function.portal_data_sync.function_name
  145. principal = "events.amazonaws.com"
  146. source_arn = aws_cloudwatch_event_rule.portal_event_daily_rule.arn
  147. }
  148. resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_lambda_weekly" {
  149. statement_id = "AllowExecutionFromCloudWatchWeekly"
  150. action = "lambda:InvokeFunction"
  151. function_name = aws_lambda_function.portal_data_sync.function_name
  152. principal = "events.amazonaws.com"
  153. source_arn = aws_cloudwatch_event_rule.portal_event_weekly_rule.arn
  154. }
  155. resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_lambda_monthly" {
  156. statement_id = "AllowExecutionFromCloudWatchMonthly"
  157. action = "lambda:InvokeFunction"
  158. function_name = aws_lambda_function.portal_data_sync.function_name
  159. principal = "events.amazonaws.com"
  160. source_arn = aws_cloudwatch_event_rule.portal_event_monthly_rule.arn
  161. }
  162. ###
  163. ### Trigger Portal Scheduler Lambda with Rules and Targets
  164. ###
  165. ### Time-based rules for portal sync:
  166. resource "aws_cloudwatch_event_rule" "portal_scheduler_quarter_hourly_rule" {
  167. name = "aws-portal-lambda-scheduler-quarter-hourly"
  168. description = "Rule for portal scheduler lambda function - every 15 minutes"
  169. schedule_expression = "rate(15 minutes)"
  170. is_enabled = var.environment == "test" ? false : true
  171. tags = merge(var.standard_tags, var.tags)
  172. }
  173. resource "aws_cloudwatch_event_rule" "portal_scheduler_half_hourly_rule" {
  174. name = "aws-portal-lambda-scheduler-half-hourly"
  175. description = "Rule for portal scheduler lambda function - every 30 minutes"
  176. schedule_expression = "rate(30 minutes)"
  177. is_enabled = var.environment == "test" ? false : true
  178. tags = merge(var.standard_tags, var.tags)
  179. }
  180. resource "aws_cloudwatch_event_rule" "portal_scheduler_hourly_rule" {
  181. name = "aws-portal-lambda-scheduler-hourly"
  182. description = "Rule for portal scheduler lambda function - every hour"
  183. schedule_expression = "rate(1 hour)"
  184. is_enabled = var.environment == "test" ? false : true
  185. tags = merge(var.standard_tags, var.tags)
  186. }
  187. resource "aws_cloudwatch_event_rule" "portal_scheduler_four_hourly_rule" {
  188. name = "aws-portal-lambda-scheduler-four-hourly"
  189. description = "Rule for portal scheduler lambda function - every 4 hours"
  190. schedule_expression = "rate(4 hours)"
  191. is_enabled = var.environment == "test" ? false : true
  192. tags = merge(var.standard_tags, var.tags)
  193. }
  194. resource "aws_cloudwatch_event_rule" "portal_scheduler_daily_rule" {
  195. name = "aws-portal-lambda-scheduler-daily"
  196. description = "Rule for portal scheduler lambda function - every day"
  197. schedule_expression = "cron(5 5 * * ? *)"
  198. is_enabled = var.environment == "test" ? false : true
  199. tags = merge(var.standard_tags, var.tags)
  200. }
  201. resource "aws_cloudwatch_event_rule" "portal_scheduler_weekly_rule" {
  202. name = "aws-portal-lambda-scheduler-weekly"
  203. description = "Rule for portal scheduler lambda function - every week"
  204. schedule_expression = "rate(7 days)"
  205. is_enabled = var.environment == "test" ? false : true
  206. tags = merge(var.standard_tags, var.tags)
  207. }
  208. resource "aws_cloudwatch_event_rule" "portal_scheduler_monthly_rule" {
  209. name = "aws-portal-lambda-scheduler-monthly"
  210. description = "Rule for portal scheduler lambda function - every month"
  211. schedule_expression = "rate(30 days)"
  212. is_enabled = var.environment == "test" ? false : true
  213. tags = merge(var.standard_tags, var.tags)
  214. }
  215. ### Time-based targets for portal scheduler:
  216. resource "aws_cloudwatch_event_target" "portal_scheduler_cloudwatch_target_quarter_hourly" {
  217. target_id = "PortalSchedulerQuarterHourly"
  218. rule = aws_cloudwatch_event_rule.portal_scheduler_quarter_hourly_rule.name
  219. input = "{\"frequency_identifier\":\"quarter-hourly\"}"
  220. arn = aws_lambda_function.portal_scheduler.arn
  221. }
  222. resource "aws_cloudwatch_event_target" "portal_scheduler_cloudwatch_target_half_hourly" {
  223. target_id = "PortalSchedulerHalfHourly"
  224. rule = aws_cloudwatch_event_rule.portal_scheduler_half_hourly_rule.name
  225. input = "{\"frequency_identifier\":\"half-hourly\"}"
  226. arn = aws_lambda_function.portal_scheduler.arn
  227. }
  228. resource "aws_cloudwatch_event_target" "portal_scheduler_cloudwatch_target_hourly" {
  229. target_id = "PortalSchedulerHourly"
  230. rule = aws_cloudwatch_event_rule.portal_scheduler_hourly_rule.name
  231. input = "{\"frequency_identifier\":\"hourly\"}"
  232. arn = aws_lambda_function.portal_scheduler.arn
  233. }
  234. resource "aws_cloudwatch_event_target" "portal_scheduler_cloudwatch_target_four_hourly" {
  235. target_id = "PortalSchedulerFourHourly"
  236. rule = aws_cloudwatch_event_rule.portal_scheduler_four_hourly_rule.name
  237. input = "{\"frequency_identifier\":\"four-hourly\"}"
  238. arn = aws_lambda_function.portal_scheduler.arn
  239. }
  240. resource "aws_cloudwatch_event_target" "portal_scheduler_cloudwatch_target_daily" {
  241. target_id = "PortalSchedulerDaily"
  242. rule = aws_cloudwatch_event_rule.portal_scheduler_daily_rule.name
  243. input = "{\"frequency_identifier\":\"daily\"}"
  244. arn = aws_lambda_function.portal_scheduler.arn
  245. }
  246. resource "aws_cloudwatch_event_target" "portal_scheduler_cloudwatch_target_weekly" {
  247. target_id = "PortalSchedulerWeekly"
  248. rule = aws_cloudwatch_event_rule.portal_scheduler_weekly_rule.name
  249. input = "{\"frequency_identifier\":\"weekly\"}"
  250. arn = aws_lambda_function.portal_scheduler.arn
  251. }
  252. resource "aws_cloudwatch_event_target" "portal_scheduler_cloudwatch_target_monthly" {
  253. target_id = "PortalSchedulerMonthly"
  254. rule = aws_cloudwatch_event_rule.portal_scheduler_monthly_rule.name
  255. input = "{\"frequency_identifier\":\"monthly\"}"
  256. arn = aws_lambda_function.portal_scheduler.arn
  257. }
  258. ### Invoke permissions for Time-based rules for portal sync:
  259. resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_scheduler_quarter_hourly" {
  260. statement_id = "AllowExecutionFromCloudWatchQuarterHourly"
  261. action = "lambda:InvokeFunction"
  262. function_name = aws_lambda_function.portal_scheduler.function_name
  263. principal = "events.amazonaws.com"
  264. source_arn = aws_cloudwatch_event_rule.portal_scheduler_quarter_hourly_rule.arn
  265. }
  266. resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_scheduler_half_hourly" {
  267. statement_id = "AllowExecutionFromCloudWatchHalfHourly"
  268. action = "lambda:InvokeFunction"
  269. function_name = aws_lambda_function.portal_scheduler.function_name
  270. principal = "events.amazonaws.com"
  271. source_arn = aws_cloudwatch_event_rule.portal_scheduler_half_hourly_rule.arn
  272. }
  273. resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_scheduler_hourly" {
  274. statement_id = "AllowExecutionFromCloudWatchHourly"
  275. action = "lambda:InvokeFunction"
  276. function_name = aws_lambda_function.portal_scheduler.function_name
  277. principal = "events.amazonaws.com"
  278. source_arn = aws_cloudwatch_event_rule.portal_scheduler_hourly_rule.arn
  279. }
  280. resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_scheduler_four_hourly" {
  281. statement_id = "AllowExecutionFromCloudWatchFourHourly"
  282. action = "lambda:InvokeFunction"
  283. function_name = aws_lambda_function.portal_scheduler.function_name
  284. principal = "events.amazonaws.com"
  285. source_arn = aws_cloudwatch_event_rule.portal_scheduler_four_hourly_rule.arn
  286. }
  287. resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_scheduler_daily" {
  288. statement_id = "AllowExecutionFromCloudWatchDaily"
  289. action = "lambda:InvokeFunction"
  290. function_name = aws_lambda_function.portal_scheduler.function_name
  291. principal = "events.amazonaws.com"
  292. source_arn = aws_cloudwatch_event_rule.portal_scheduler_daily_rule.arn
  293. }
  294. resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_scheduler_weekly" {
  295. statement_id = "AllowExecutionFromCloudWatchWeekly"
  296. action = "lambda:InvokeFunction"
  297. function_name = aws_lambda_function.portal_scheduler.function_name
  298. principal = "events.amazonaws.com"
  299. source_arn = aws_cloudwatch_event_rule.portal_scheduler_weekly_rule.arn
  300. }
  301. resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_scheduler_monthly" {
  302. statement_id = "AllowExecutionFromCloudWatchMonthly"
  303. action = "lambda:InvokeFunction"
  304. function_name = aws_lambda_function.portal_scheduler.function_name
  305. principal = "events.amazonaws.com"
  306. source_arn = aws_cloudwatch_event_rule.portal_scheduler_monthly_rule.arn
  307. }