AWS CloudFormation
User Guide (API Version 2010-05-15)
AWS 服务或AWS文档中描述的功能,可能因地区/位置而异。请点击 Amazon AWS 入门,可查看中国地区的具体差异

Amazon CloudWatch 模板代码段

账单警报

在下面的示例中,Amazon CloudWatch 在您的 AWS 账户费用超过警报阈值时发送电子邮件通知。要接收使用情况通知,请启用账单提醒

JSON

"SpendingAlarm": { "Type": "AWS::CloudWatch::Alarm", "Properties": { "AlarmDescription": { "Fn::Join": ["", [ "Alarm if AWS spending is over $", { "Ref": "AlarmThreshold" } ]]}, "Namespace": "AWS/Billing", "MetricName": "EstimatedCharges", "Dimensions": [{ "Name": "Currency", "Value" : "USD" }], "Statistic": "Maximum", "Period": "21600", "EvaluationPeriods": "1", "Threshold": { "Ref": "AlarmThreshold" }, "ComparisonOperator": "GreaterThanThreshold", "AlarmActions": [{ "Ref": "BillingAlarmNotification" }], "InsufficientDataActions": [{ "Ref": "BillingAlarmNotification" }] } }

YAML

SpendingAlarm: Type: AWS::CloudWatch::Alarm Properties: AlarmDescription: !Sub > "Alarm if AWS spending is over $${AlarmThreshold}" Namespace: AWS/Billing MetricName: EstimatedCharges Dimensions: - Name: Currency Value: USD Statistic: Maximum Period: '21600' EvaluationPeriods: '1' Threshold: Ref: "AlarmThreshold" ComparisonOperator: GreaterThanThreshold AlarmActions: - Ref: "BillingAlarmNotification" InsufficientDataActions: - Ref: "BillingAlarmNotification"

CPU 使用率警报

下面的示例代码段创建一个警报,该警报在三个评估周期内 Amazon EC2 实例的平均 CPU 利用率超出 90% 的时间大于 60 秒时发送通知。

JSON

"CPUAlarm" : { "Type" : "AWS::CloudWatch::Alarm", "Properties" : { "AlarmDescription" : "CPU alarm for my instance", "AlarmActions" : [ { "Ref" : "logical name of an AWS::SNS::Topic resource" } ], "MetricName" : "CPUUtilization", "Namespace" : "AWS/EC2", "Statistic" : "Average", "Period" : "60", "EvaluationPeriods" : "3", "Threshold" : "90", "ComparisonOperator" : "GreaterThanThreshold", "Dimensions" : [ { "Name" : "InstanceId", "Value" : { "Ref" : "logical name of an AWS::EC2::Instance resource" } } ] } }

YAML

CPUAlarm: Type: AWS::CloudWatch::Alarm Properties: AlarmDescription: CPU alarm for my instance AlarmActions: - Ref: "logical name of an AWS::SNS::Topic resource" MetricName: CPUUtilization Namespace: AWS/EC2 Statistic: Average Period: '60' EvaluationPeriods: '3' Threshold: '90' ComparisonOperator: GreaterThanThreshold Dimensions: - Name: InstanceId Value: Ref: "logical name of an AWS::EC2::Instance resource"

恢复 Amazon Elastic Compute Cloud 实例

当以下 CloudWatch 警报在连续的 15 分钟时间内存在状态检查故障时,它将恢复 EC2 实例。有关警报操作的更多信息,请参阅 Amazon CloudWatch 用户指南 中的创建用于停止、终止或恢复实例的警报

JSON

{ "AWSTemplateFormatVersion": "2010-09-09", "Parameters" : { "RecoveryInstance" : { "Description" : "The EC2 instance ID to associate this alarm with.", "Type" : "AWS::EC2::Instance::Id" } }, "Resources": { "RecoveryTestAlarm": { "Type": "AWS::CloudWatch::Alarm", "Properties": { "AlarmDescription": "Trigger a recovery when instance status check fails for 15 consecutive minutes.", "Namespace": "AWS/EC2" , "MetricName": "StatusCheckFailed_System", "Statistic": "Minimum", "Period": "60", "EvaluationPeriods": "15", "ComparisonOperator": "GreaterThanThreshold", "Threshold": "0", "AlarmActions": [ {"Fn::Join" : ["", ["arn:aws:automate:", { "Ref" : "AWS::Region" }, ":ec2:recover" ]]} ], "Dimensions": [{"Name": "InstanceId","Value": {"Ref": "RecoveryInstance"}}] } } } }

YAML

AWSTemplateFormatVersion: '2010-09-09' Parameters: RecoveryInstance: Description: The EC2 instance ID to associate this alarm with. Type: AWS::EC2::Instance::Id Resources: RecoveryTestAlarm: Type: AWS::CloudWatch::Alarm Properties: AlarmDescription: Trigger a recovery when instance status check fails for 15 consecutive minutes. Namespace: AWS/EC2 MetricName: StatusCheckFailed_System Statistic: Minimum Period: '60' EvaluationPeriods: '15' ComparisonOperator: GreaterThanThreshold Threshold: '0' AlarmActions: !Sub > "arn:aws:automate:${AWS::Region}:ec2:recover" Dimensions: - Name: InstanceId Value: Ref: RecoveryInstance

创建基本控制面板

下面的示例创建一个简单的 CloudWatch 控制面板,其中包含一个显示 CPU 使用率的指标小部件和一个显示消息的文本小部件。

JSON

{ "BasicDashboard": { "Type": "AWS::CloudWatch::Dashboard", "Properties": { "DashboardName": "Dashboard1", "DashboardBody": "{\"widgets\":[{\"type\":\"metric\",\"x\":0,\"y\":0,\"width\":12,\"height\":6,\"properties\":{\"metrics\":[[\"AWS/EC2\",\"CPUUtilization\",\"InstanceId\",\"i-012345\"]],\"period\":300,\"stat\":\"Average\",\"region\":\"us-east-1\",\"title\":\"EC2 Instance CPU\"}},{\"type\":\"text\",\"x\":0,\"y\":7,\"width\":3,\"height\":3,\"properties\":{\"markdown\":\"Hello world\"}}]}" } } }

YAML

BasicDashboard: Type: AWS::CloudWatch::Dashboard Properties: DashboardName: Dashboard1 DashboardBody: '{"widgets":[{"type":"metric","x":0,"y":0,"width":12,"height":6,"properties":{"metrics":[["AWS/EC2","CPUUtilization","InstanceId","i-012345"]],"period":300,"stat":"Average","region":"us-east-1","title":"EC2 Instance CPU"}},{"type":"text","x":0,"y":7,"width":3,"height":3,"properties":{"markdown":"Hello world"}}]}'

使用并排显示小部件创建控制面板

下面的示例创建一个包含两个指标小部件 (并排显示) 的控制面板。

JSON

{ "DashboardSideBySide": { "Type": "AWS::CloudWatch::Dashboard", "Properties": { "DashboardName": "Dashboard1", "DashboardBody": "{\"widgets\":[{\"type\":\"metric\",\"x\":0,\"y\":0,\"width\":12,\"height\":6,\"properties\":{\"metrics\":[[\"AWS/EC2\",\"CPUUtilization\",\"InstanceId\",\"i-012345\"]],\"period\":300,\"stat\":\"Average\",\"region\":\"us-east-1\",\"title\":\"EC2 Instance CPU\"}},{\"type\":\"metric\",\"x\":12,\"y\":0,\"width\":12,\"height\":6,\"properties\":{\"metrics\":[[\"AWS/S3\",\"BucketSizeBytes\",\"BucketName\",\"MyBucketName\"]],\"period\":86400,\"stat\":\"Maximum\",\"region\":\"us-east-1\",\"title\":\"MyBucketName bytes\"}}]}" } } }

YAML

DashboardSideBySide: Type: AWS::CloudWatch::Dashboard Properties: DashboardName: Dashboard1 DashboardBody: '{"widgets":[{"type":"metric","x":0,"y":0,"width":12,"height":6,"properties":{"metrics":[["AWS/EC2","CPUUtilization","InstanceId","i-012345"]],"period":300,"stat":"Average","region":"us-east-1","title":"EC2 Instance CPU"}},{"type":"metric","x":12,"y":0,"width":12,"height":6,"properties":{"metrics":[["AWS/S3","BucketSizeBytes","BucketName","MyBucketName"]],"period":86400,"stat":"Maximum","region":"us-east-1","title":"MyBucketName bytes"}}]}'