Browse the Repo

file-type-icon.circleci
file-type-icon_docs
file-type-iconexamples
file-type-iconmodules
file-type-iconagents
file-type-iconalarms
file-type-iconalb-alarms
file-type-iconalb-target-group-alarms
file-type-iconasg-cpu-alarms
file-type-iconasg-disk-alarms
file-type-iconasg-memory-alarms
file-type-iconec2-cpu-alarms
file-type-iconec2-disk-alarms
file-type-iconec2-memory-alarms
file-type-iconecs-cluster-alarms
file-type-iconecs-service-alarms
file-type-iconecs-service-with-alb-alarms
file-type-iconelasticache-memcached-alarms
file-type-iconelasticache-redis-alarms
file-type-iconelasticsearch-alarms
file-type-iconelb-alarms
file-type-iconlambda-alarms
file-type-iconrds-alarms
file-type-iconroute53-health-check-alarms
file-type-iconscheduled-job-alarm
file-type-iconREADME.md
file-type-iconmain.tf
file-type-iconvars.tf
file-type-iconsns-to-slack
file-type-iconsqs-alarms
file-type-iconREADME.md
file-type-iconlogs
file-type-iconmetrics
file-type-icontest
file-type-icon.gitignore
file-type-icon.pre-commit-config.yaml
file-type-iconCODEOWNERS
file-type-iconLICENSE.txt
file-type-iconREADME.md
file-type-iconterraform-cloud-enterprise-private-module-...

Browse the Repo

file-type-icon.circleci
file-type-icon_docs
file-type-iconexamples
file-type-iconmodules
file-type-iconagents
file-type-iconalarms
file-type-iconalb-alarms
file-type-iconalb-target-group-alarms
file-type-iconasg-cpu-alarms
file-type-iconasg-disk-alarms
file-type-iconasg-memory-alarms
file-type-iconec2-cpu-alarms
file-type-iconec2-disk-alarms
file-type-iconec2-memory-alarms
file-type-iconecs-cluster-alarms
file-type-iconecs-service-alarms
file-type-iconecs-service-with-alb-alarms
file-type-iconelasticache-memcached-alarms
file-type-iconelasticache-redis-alarms
file-type-iconelasticsearch-alarms
file-type-iconelb-alarms
file-type-iconlambda-alarms
file-type-iconrds-alarms
file-type-iconroute53-health-check-alarms
file-type-iconscheduled-job-alarm
file-type-iconREADME.md
file-type-iconmain.tf
file-type-iconvars.tf
file-type-iconsns-to-slack
file-type-iconsqs-alarms
file-type-iconREADME.md
file-type-iconlogs
file-type-iconmetrics
file-type-icontest
file-type-icon.gitignore
file-type-icon.pre-commit-config.yaml
file-type-iconCODEOWNERS
file-type-iconLICENSE.txt
file-type-iconREADME.md
file-type-iconterraform-cloud-enterprise-private-module-...
CloudWatch

CloudWatch

Send all metrics to CloudWatch, including those not visible to the EC2 hypervisor. Create dashboards and alerts as code.

Code Preview

Preview the Code

mobile file icon

README.md

down

Sheduled Job Alarm Module

This module creates an alarm that goes off if a specified metric drops below a specified threshold over a specified time period. This is most useful for detecting if a scheduled job failed. For example, if you have a CRON job that runs once per night, you could have the CRON job set a metric to the value "1" after each successful run and then use this module to trigger an alarm if the metric drops below the value "1" over a 24 hour period. This module works especially well with ec2-snapper, which you can run in a CRON job to automatically backup EC2 Instances and write CloudWatch metrics.

Example

Check out the examples/scheduled-job-alarms example.

How do you use this module?

The basic idea is to use the module resource in your templates and to specify:

  1. The name of your scheduled job
  2. The namespace in CloudWatch for your metrics
  3. The name of the metric that is incremented whenever the scheduled job runs
  4. How often that metric should be updated
  5. The ARN of an SNS topic to notify whenever the alarm goes into OK state, which indicates the scheduled job is working, or INSUFFICIENT_DATA state, which indicates the metric is not being written, and therefore the scheduled job is probably failing.

Example:

module "scheduled_job_alarm" {
  source = "git::git@github.com:gruntwork-io/terraform-aws-monitoring.git//modules/alarms/scheduled-job-alarm?ref=v1.0.8"

  name = "my-scheduled-job-foo"
  namespace = "MyScheduledJobs"
  metric_name = "ScheduledJobFoo"
  alarm_sns_topic_arns = "${aws_sns_topic.cloudwatch_alarms.arn}"

  # We expect the job to run once per day, which is 86,400 seconds. However, the job itself may take some time to run,
  # so we add two hours (7200 seconds) of buffer room and expect the metric to be updated once every 93,600 seconds.
  period = 93600
}

# Create an SNS topic that will be notified whenever this alarm is in OK or INSUFFICIENT_DATA state. You can subscribe
# to notifications from this SNS topic by email or text message.
resource "aws_sns_topic" "cloudwatch_alarms" {
  name = "my-scheduled-job-cloudwatch-alarms"
}

See vars.tf for documentation on all the parameters you can set in this module.

How do I get notifications from these alarms?

One of the parameters you pass to this module is a list of SNS topic ARNs to notify when the website goes down. Here is how to configure an SNS topic:

  1. Create an SNS topic using the Terraform aws_sns_topic resource.
  2. Pass the topic's ARN to this module (e.g. alarm_sns_topic_arns = "${aws_sns_topic.my_topic.arn}")
  3. Login to the SNS console.
  4. Click the "Topics" link in the menu on the left.
  5. Find your topic in the list and click the checkbox next to it.
  6. Click the "Actions" button and select "Subscribe to Topic".
  7. Choose "Email" or "SMS Message" as the protocol, enter your email or phone number, and click "Create Subscription".
  8. AWS will email or message you to confirm the subscription. Be sure to confirm it, or you won't receive any notifications, and the alarm will report its status as INSUFFICIENT_DATA!

Questions? Ask away.

We're here to talk about our services, answer any questions, give advice, or just to chat.

Ready to hand off the Gruntwork?