cloudwatch.tf 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. ###
  7. ### Trigger Portal Sync Lambda with Rules and Targets
  8. ###
  9. ### Time-based rules for portal sync:
  10. resource "aws_cloudwatch_event_rule" "portal_event_quarter_hourly_rule" {
  11. name = "aws-portal-lambda-data-sync-quarter-hourly"
  12. description = "Rule for portal data sync lambda function - every 15 minutes"
  13. schedule_expression = "rate(15 minutes)"
  14. is_enabled = var.environment == "test" ? false : true
  15. tags = merge(var.standard_tags, var.tags)
  16. }
  17. resource "aws_cloudwatch_event_rule" "portal_event_half_hourly_rule" {
  18. name = "aws-portal-lambda-data-sync-half-hourly"
  19. description = "Rule for portal data sync lambda function - every 30 minutes"
  20. schedule_expression = "rate(30 minutes)"
  21. is_enabled = var.environment == "test" ? false : true
  22. tags = merge(var.standard_tags, var.tags)
  23. }
  24. resource "aws_cloudwatch_event_rule" "portal_event_hourly_rule" {
  25. name = "aws-portal-lambda-data-sync-hourly"
  26. description = "Rule for portal data sync lambda function - every hour"
  27. schedule_expression = "rate(1 hour)"
  28. is_enabled = var.environment == "test" ? false : true
  29. tags = merge(var.standard_tags, var.tags)
  30. }
  31. resource "aws_cloudwatch_event_rule" "portal_event_four_hourly_rule" {
  32. name = "aws-portal-lambda-data-sync-four-hourly"
  33. description = "Rule for portal data sync lambda function - every 4 hours"
  34. schedule_expression = "rate(4 hours)"
  35. is_enabled = var.environment == "test" ? false : true
  36. tags = merge(var.standard_tags, var.tags)
  37. }
  38. resource "aws_cloudwatch_event_rule" "portal_event_daily_rule" {
  39. name = "aws-portal-lambda-data-sync-daily"
  40. description = "Rule for portal data sync lambda function - every day"
  41. schedule_expression = "cron(5 5 * * ? *)"
  42. is_enabled = var.environment == "test" ? false : true
  43. tags = merge(var.standard_tags, var.tags)
  44. }
  45. resource "aws_cloudwatch_event_rule" "portal_event_weekly_rule" {
  46. name = "aws-portal-lambda-data-sync-weekly"
  47. description = "Rule for portal data sync lambda function - every week"
  48. schedule_expression = "rate(7 days)"
  49. is_enabled = var.environment == "test" ? false : true
  50. tags = merge(var.standard_tags, var.tags)
  51. }
  52. resource "aws_cloudwatch_event_rule" "portal_event_monthly_rule" {
  53. name = "aws-portal-lambda-data-sync-monthly"
  54. description = "Rule for portal data sync lambda function - every month"
  55. schedule_expression = "rate(30 days)"
  56. is_enabled = var.environment == "test" ? false : true
  57. tags = merge(var.standard_tags, var.tags)
  58. }
  59. ### Time-based targets for portal sync:
  60. resource "aws_cloudwatch_event_target" "portal_lambda_cloudwatch_target_quarter_hourly" {
  61. target_id = "PortalSyncQuarterHourly"
  62. rule = aws_cloudwatch_event_rule.portal_event_quarter_hourly_rule.name
  63. input = "{\"frequency_identifier\":\"quarter-hourly\"}"
  64. arn = aws_lambda_function.portal_data_sync.arn
  65. }
  66. resource "aws_cloudwatch_event_target" "portal_lambda_cloudwatch_target_half_hourly" {
  67. target_id = "PortalSyncHalfHourly"
  68. rule = aws_cloudwatch_event_rule.portal_event_half_hourly_rule.name
  69. input = "{\"frequency_identifier\":\"half-hourly\"}"
  70. arn = aws_lambda_function.portal_data_sync.arn
  71. }
  72. resource "aws_cloudwatch_event_target" "portal_lambda_cloudwatch_target_hourly" {
  73. target_id = "PortalSyncHourly"
  74. rule = aws_cloudwatch_event_rule.portal_event_hourly_rule.name
  75. input = "{\"frequency_identifier\":\"hourly\"}"
  76. arn = aws_lambda_function.portal_data_sync.arn
  77. }
  78. resource "aws_cloudwatch_event_target" "portal_lambda_cloudwatch_target_four_hourly" {
  79. target_id = "PortalSyncFourHourly"
  80. rule = aws_cloudwatch_event_rule.portal_event_four_hourly_rule.name
  81. input = "{\"frequency_identifier\":\"four-hourly\"}"
  82. arn = aws_lambda_function.portal_data_sync.arn
  83. }
  84. resource "aws_cloudwatch_event_target" "portal_lambda_cloudwatch_target_daily" {
  85. target_id = "PortalSyncDaily"
  86. rule = aws_cloudwatch_event_rule.portal_event_daily_rule.name
  87. input = "{\"frequency_identifier\":\"daily\"}"
  88. arn = aws_lambda_function.portal_data_sync.arn
  89. }
  90. resource "aws_cloudwatch_event_target" "portal_lambda_cloudwatch_target_weekly" {
  91. target_id = "PortalSyncWeekly"
  92. rule = aws_cloudwatch_event_rule.portal_event_weekly_rule.name
  93. input = "{\"frequency_identifier\":\"weekly\"}"
  94. arn = aws_lambda_function.portal_data_sync.arn
  95. }
  96. resource "aws_cloudwatch_event_target" "portal_lambda_cloudwatch_target_monthly" {
  97. target_id = "PortalSyncMonthly"
  98. rule = aws_cloudwatch_event_rule.portal_event_monthly_rule.name
  99. input = "{\"frequency_identifier\":\"monthly\"}"
  100. arn = aws_lambda_function.portal_data_sync.arn
  101. }
  102. ### Invoke permissions for Time-based rules for portal sync:
  103. resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_lambda_quarter_hourly" {
  104. statement_id = "AllowExecutionFromCloudWatchQuarterHourly"
  105. action = "lambda:InvokeFunction"
  106. function_name = aws_lambda_function.portal_data_sync.function_name
  107. principal = "events.amazonaws.com"
  108. source_arn = aws_cloudwatch_event_rule.portal_event_quarter_hourly_rule.arn
  109. }
  110. resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_lambda_half_hourly" {
  111. statement_id = "AllowExecutionFromCloudWatchHalfHourly"
  112. action = "lambda:InvokeFunction"
  113. function_name = aws_lambda_function.portal_data_sync.function_name
  114. principal = "events.amazonaws.com"
  115. source_arn = aws_cloudwatch_event_rule.portal_event_half_hourly_rule.arn
  116. }
  117. resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_lambda_hourly" {
  118. statement_id = "AllowExecutionFromCloudWatchHourly"
  119. action = "lambda:InvokeFunction"
  120. function_name = aws_lambda_function.portal_data_sync.function_name
  121. principal = "events.amazonaws.com"
  122. source_arn = aws_cloudwatch_event_rule.portal_event_hourly_rule.arn
  123. }
  124. resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_lambda_four_hourly" {
  125. statement_id = "AllowExecutionFromCloudWatchFourHourly"
  126. action = "lambda:InvokeFunction"
  127. function_name = aws_lambda_function.portal_data_sync.function_name
  128. principal = "events.amazonaws.com"
  129. source_arn = aws_cloudwatch_event_rule.portal_event_four_hourly_rule.arn
  130. }
  131. resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_lambda_daily" {
  132. statement_id = "AllowExecutionFromCloudWatchDaily"
  133. action = "lambda:InvokeFunction"
  134. function_name = aws_lambda_function.portal_data_sync.function_name
  135. principal = "events.amazonaws.com"
  136. source_arn = aws_cloudwatch_event_rule.portal_event_daily_rule.arn
  137. }
  138. resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_lambda_weekly" {
  139. statement_id = "AllowExecutionFromCloudWatchWeekly"
  140. action = "lambda:InvokeFunction"
  141. function_name = aws_lambda_function.portal_data_sync.function_name
  142. principal = "events.amazonaws.com"
  143. source_arn = aws_cloudwatch_event_rule.portal_event_weekly_rule.arn
  144. }
  145. resource "aws_lambda_permission" "allow_cloudwatch_to_call_portal_lambda_monthly" {
  146. statement_id = "AllowExecutionFromCloudWatchMonthly"
  147. action = "lambda:InvokeFunction"
  148. function_name = aws_lambda_function.portal_data_sync.function_name
  149. principal = "events.amazonaws.com"
  150. source_arn = aws_cloudwatch_event_rule.portal_event_monthly_rule.arn
  151. }