Skip to content

Instantly share code, notes, and snippets.

@igabice
Last active October 13, 2023 10:43
Show Gist options
  • Save igabice/f3d8ebbc30a405abb5ca4dff38dae1a8 to your computer and use it in GitHub Desktop.
Save igabice/f3d8ebbc30a405abb5ca4dff38dae1a8 to your computer and use it in GitHub Desktop.
a CloudWatch Composite Alarm based on both CPUUtilization and NetworkIn metrics
##-----------------------------------------------------------------------------
## creates Composite Cloudwatch Alarm on AWS for monitoring
##-----------------------------------------------------------------------------
resource "awscc_cloudwatch_composite_alarm" "composite_alarm" {
alarm_name = "example-composite-alarm"
alarm_description = "Example of a composite alarm with various actions"
alarm_actions = [aws_autoscaling_policy.example_scaling_policy.arn]
alarm_rule = "ALARM(${aws_cloudwatch_metric_alarm.cpu_gte_80.alarm_name}) OR ALARM(${aws_cloudwatch_metric_alarm.network_in.alarm_name})"
}
resource "aws_autoscaling_policy" "example_scaling_policy" {
name = "example"
autoscaling_group_name = aws_autoscaling_group.example.name
policy_type = "SimpleScaling"
scaling_adjustment = 1
adjustment_type = "ChangeInCapacity"
}
resource "aws_cloudwatch_metric_alarm" "cpu_gte_80" {
alarm_name = "cpu-gte-80"
alarm_description = "This metric monitors ec2 cpu utilization"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = 2
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = 120
statistic = "Average"
threshold = 80
}
resource "aws_cloudwatch_metric_alarm" "network_in" {
# Define the second condition for NetworkIn
alarm_name = "network-in-1"
alarm_description = "Alarm when NetworkIn exceeds 10,000"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = 2
namespace = "AWS/EC2"
metric_name = "NetworkIn"
period = 60
statistic = "SampleCount"
threshold = 10000 # Adjust the threshold as needed
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment