Creating SMS alerts for high CPU utilization
TypeScriptTo create SMS alerts for high CPU utilization, you can use cloud services like AWS CloudWatch, which offers monitoring and alerting capabilities. Pulumi allows us to define these resources as code, managing them alongside our application code. In this example, I will show how to use Pulumi with AWS to create a CloudWatch alarm that triggers an SMS (text message) notification when CPU utilization crosses a specified threshold.
Here's what you'll typically do:
- Define a CloudWatch metric alarm: We will create an alarm for high CPU utilization on an EC2 instance.
- Set up an SNS topic: This is where notifications will be sent when the alarm state is reached.
- Subscribe an SMS endpoint to the SNS topic: We will subscribe a phone number to the SNS topic to receive SMS alerts.
- Provide permissions: Lastly, we ensure that CloudWatch has permission to publish to the SNS topic by setting up the correct policies.
Before running the program below, make sure you've already set up Pulumi with the appropriate AWS credentials. Here is a Pulumi program in TypeScript that accomplishes the task:
import * as aws from "@pulumi/aws"; // Replace with your actual EC2 instance ID and phone number const instanceId = "i-1234567890abcdef0"; const phoneNumber = "+15555555555"; // ensure the number is in E.164 format // Create an SNS topic that will receive the notifications const topic = new aws.sns.Topic("cpuUtilizationTopic", {}); // Subscribe your phone number to the SNS topic for SMS notifications const smsSubscription = new aws.sns.TopicSubscription("smsSubscription", { topic: topic.arn, protocol: "sms", endpoint: phoneNumber, }); // Create a metric alarm that watches for high CPU utilization const highCpuAlarm = new aws.cloudwatch.MetricAlarm("highCpuUsage", { comparisonOperator: "GreaterThanOrEqualToThreshold", evaluationPeriods: 2, metricName: "CPUUtilization", namespace: "AWS/EC2", period: 60, statistic: "Average", threshold: 75, // Set your CPU utilization threshold (percentage) here alarmDescription: "This alarm monitors EC2 CPU utilization", alarmActions: [topic.arn], dimensions: { InstanceId: instanceId, }, }); // Export the names of the resources export const topicName = topic.name; export const subscriptionArn = smsSubscription.arn; export const alarmName = highCpuAlarm.name;
In this program, you need to replace
instanceId
with the ID of the EC2 instance you wish to monitor, andphoneNumber
with your actual phone number in E.164 format.A MetricAlarm resource is created through
new aws.cloudwatch.MetricAlarm
. This alarm is configured to watch theCPUUtilization
metric of the specified EC2 instance. If the average CPU utilization is greater than or equal to 75% for two consecutive periods of 60 seconds, the alarm state will change toALARM
, and a notification will be sent to the SNS topic.We then create an SNS Topic using
new aws.sns.Topic
, to which we subscribe our phone number withnew aws.sns.TopicSubscription
. Once an alarm state is reached, the SNS topic publishes this event, and an SMS notification will be sent to the subscribed phone number.Finally, the program exports the names of the resources so that you can easily identify them within the AWS console or when you want to reference them in your other Pulumi programs.
To run this program, save it to a file (for example
index.ts
), then runpulumi up
via the command line in the same directory. Pulumi will perform the deployment, and once completed, your alerting system for high CPU utilization will be active.