A Pulumi package for creating and managing Amazon Web Services (AWS) cloud resources with infrastructure-as-code.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Amazon CloudWatch provides monitoring and observability for AWS resources and applications through metrics, logs, and alarms.
import * as aws from "@pulumi/aws";
import * as cloudwatch from "@pulumi/aws/cloudwatch";CloudWatch alarm for monitoring metrics.
const alarm = new aws.cloudwatch.MetricAlarm("high-cpu", {
name: "high-cpu-alarm",
comparisonOperator: "GreaterThanThreshold",
evaluationPeriods: 2,
metricName: "CPUUtilization",
namespace: "AWS/EC2",
period: 300,
statistic: "Average",
threshold: 80,
alarmDescription: "Triggers when CPU exceeds 80%",
dimensions: {
InstanceId: instance.id,
},
alarmActions: [snsTopic.arn],
});CloudWatch log group for storing logs.
const logGroup = new aws.cloudwatch.LogGroup("app-logs", {
name: "/aws/lambda/my-function",
retentionInDays: 30,
tags: {
Application: "my-app",
},
});CloudWatch log stream within a log group.
const logStream = new aws.cloudwatch.LogStream("app-stream", {
name: "instance-logs",
logGroupName: logGroup.name,
});CloudWatch dashboard for visualizing metrics.
const dashboard = new aws.cloudwatch.Dashboard("app-dashboard", {
dashboardName: "my-app-dashboard",
dashboardBody: JSON.stringify({
widgets: [{
type: "metric",
properties: {
metrics: [
["AWS/EC2", "CPUUtilization", { stat: "Average" }],
],
period: 300,
stat: "Average",
region: "us-west-2",
title: "EC2 CPU Utilization",
},
}],
}),
});const compositeAlarm = new aws.cloudwatch.CompositeAlarm("composite", {
alarmName: "composite-alarm",
alarmRule: `ALARM(${alarm1.arn}) AND ALARM(${alarm2.arn})`,
alarmDescription: "Composite alarm for multiple conditions",
alarmActions: [snsTopic.arn],
});const metricAlarm = new aws.cloudwatch.MetricAlarm("custom-metric", {
name: "custom-metric-alarm",
comparisonOperator: "GreaterThanThreshold",
evaluationPeriods: 1,
metricName: "CustomMetric",
namespace: "MyApp/Custom",
period: 60,
statistic: "Sum",
threshold: 100,
dimensions: {
Environment: "production",
},
});const metricFilter = new aws.cloudwatch.LogMetricFilter("error-filter", {
name: "error-count",
logGroupName: logGroup.name,
pattern: "[timestamp, request_id, event_type = ERROR*, ...]",
metricTransformation: {
name: "ErrorCount",
namespace: "MyApp/Logs",
value: "1",
},
});name - Alarm namecomparisonOperator - Comparison operatorevaluationPeriods - Number of periods to evaluatemetricName - Metric namenamespace - Metric namespaceperiod - Period in secondsstatistic - Statistic type (Average, Sum, etc.)threshold - Alarm thresholddimensions - Metric dimensionsname - Log group nameretentionInDays - Log retention periodkmsKeyId - KMS key for encryptionid - Resource identifierarn - Resource ARNname - Resource nameInstall with Tessl CLI
npx tessl i tessl/npm-pulumi--aws@7.16.0