Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. ### Cloudwatch Events ###
  2. # Event rule: Runs at 8pm during working days
  3. resource "aws_cloudwatch_event_rule" "start_instances_event_rule" {
  4. name = "start_instances_event_rule"
  5. description = "Starts stopped EC2 instances"
  6. schedule_expression = "cron(0 8 ? * MON-FRI *)"
  7. depends_on = ["aws_lambda_function.ec2_start_scheduler_lambda"]
  8. }
  9.  
  10. # Runs at 8am during working days
  11. resource "aws_cloudwatch_event_rule" "stop_instances_event_rule" {
  12. name = "stop_instances_event_rule"
  13. description = "Stops running EC2 instances"
  14. schedule_expression = "cron(0 20 ? * MON-FRI *)"
  15. depends_on = ["aws_lambda_function.ec2_stop_scheduler_lambda"]
  16. }
  17.  
  18. # Event target: Associates a rule with a function to run
  19. resource "aws_cloudwatch_event_target" "start_instances_event_target" {
  20. target_id = "start_instances_lambda_target"
  21. rule = "${aws_cloudwatch_event_rule.start_instances_event_rule.name}"
  22. arn = "${aws_lambda_function.ec2_start_scheduler_lambda.arn}"
  23. }
  24.  
  25. resource "aws_cloudwatch_event_target" "stop_instances_event_target" {
  26. target_id = "stop_instances_lambda_target"
  27. rule = "${aws_cloudwatch_event_rule.stop_instances_event_rule.name}"
  28. arn = "${aws_lambda_function.ec2_stop_scheduler_lambda.arn}"
  29. }
  30.  
  31. # AWS Lambda Permissions: Allow CloudWatch to execute the Lambda Functions
  32. resource "aws_lambda_permission" "allow_cloudwatch_to_call_start_scheduler" {
  33. statement_id = "AllowExecutionFromCloudWatch"
  34. action = "lambda:InvokeFunction"
  35. function_name = "${aws_lambda_function.ec2_start_scheduler_lambda.function_name}"
  36. principal = "events.amazonaws.com"
  37. source_arn = "${aws_cloudwatch_event_rule.start_instances_event_rule.arn}"
  38. }
  39.  
  40. resource "aws_lambda_permission" "allow_cloudwatch_to_call_stop_scheduler" {
  41. statement_id = "AllowExecutionFromCloudWatch"
  42. action = "lambda:InvokeFunction"
  43. function_name = "${aws_lambda_function.ec2_stop_scheduler_lambda.function_name}"
  44. principal = "events.amazonaws.com"
  45. source_arn = "${aws_cloudwatch_event_rule.stop_instances_event_rule.arn}"
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement