cloudwatch.tf 6.0 KB

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