Skip to content
GitHubXDiscord

Alarm

The Alarm resource lets you manage AWS CloudWatch Alarms for monitoring your AWS resources and applications. You can set alarms based on specific metrics, and take automated actions when those metrics reach a defined threshold.

Create a basic CloudWatch Alarm that triggers when the CPU utilization exceeds 80%.

import AWS from "alchemy/aws/control";
const cpuAlarm = await AWS.CloudWatch.Alarm("cpuAlarm", {
ComparisonOperator: "GreaterThanThreshold",
EvaluationPeriods: 1,
MetricName: "CPUUtilization",
Namespace: "AWS/EC2",
Period: 300,
Statistic: "Average",
Threshold: 80,
AlarmDescription: "Alarm when CPU exceeds 80%",
AlarmActions: ["arn:aws:sns:us-east-1:123456789012:NotifyMe"],
OKActions: ["arn:aws:sns:us-east-1:123456789012:NotifyMe"],
Dimensions: [
{
Name: "InstanceId",
Value: "i-0abcd1234efgh5678"
}
]
});

Configure a CloudWatch Alarm with additional options, such as treating missing data and using extended statistics.

const advancedCpuAlarm = await AWS.CloudWatch.Alarm("advancedCpuAlarm", {
ComparisonOperator: "GreaterThanThreshold",
EvaluationPeriods: 2,
MetricName: "CPUUtilization",
Namespace: "AWS/EC2",
Period: 300,
Statistic: "Average",
Threshold: 75,
AlarmDescription: "Advanced Alarm when CPU exceeds 75% for 2 periods",
AlarmActions: ["arn:aws:sns:us-east-1:123456789012:NotifyMe"],
InsufficientDataActions: ["arn:aws:sns:us-east-1:123456789012:NotifyMe"],
TreatMissingData: "breaching",
ExtendedStatistic: "p90",
Dimensions: [
{
Name: "InstanceId",
Value: "i-0abcd1234efgh5678"
}
]
});

Set an alarm based on a custom metric that tracks application-specific performance.

const customMetricAlarm = await AWS.CloudWatch.Alarm("customMetricAlarm", {
ComparisonOperator: "GreaterThanThreshold",
EvaluationPeriods: 3,
MetricName: "CustomErrorCount",
Namespace: "MyApplication",
Period: 60,
Statistic: "Sum",
Threshold: 5,
AlarmDescription: "Alarm for high error count in application",
AlarmActions: ["arn:aws:sns:us-east-1:123456789012:NotifyMe"],
Dimensions: [
{
Name: "ServiceName",
Value: "MyService"
}
]
});

Create an alarm that monitors multiple metrics using metric math.

const multiMetricAlarm = await AWS.CloudWatch.Alarm("multiMetricAlarm", {
ComparisonOperator: "GreaterThanThreshold",
EvaluationPeriods: 1,
Metrics: [
{
Id: "m1",
Expression: "SUM(METRICS())",
Label: "TotalRequests",
ReturnData: true
},
{
Id: "m2",
MetricStat: {
MetricName: "5XXError",
Namespace: "AWS/ApiGateway",
Period: 60,
Stat: "Sum"
},
Label: "5XX Errors",
ReturnData: true
}
],
Threshold: 10,
AlarmDescription: "Alarm when total requests exceed errors threshold",
AlarmActions: ["arn:aws:sns:us-east-1:123456789012:NotifyMe"]
});