メニュー
AWS CloudFormation
ユーザーガイド (API Version 2010-05-15)

Amazon CloudWatch テンプレートスニペット

請求アラーム

次のサンプルでは、Amazon CloudWatch は AWS アカウントに対する課金がアラームしきい値を超えた場合に E メール通知を送信します。使用量の通知を受け取るには、請求アラートを有効にします。

JSON

Copy
"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

Copy
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 使用率が 3 つの評価期間にわたって 60 秒間以上 90% を超えたときに通知を送信するアラームを作成します。

JSON

Copy
"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

Copy
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

Copy
{ "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

Copy
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

基本ダッシュボードを作成する

次の例では、CPU 使用率を表示する 1 つのメトリクスウィジェットとメッセージを表示する 1 つのテキストウィジェットからなる単純な CloudWatch ダッシュボードを作成します。

JSON

Copy
{ "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

Copy
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"}}]}'

サイドバイサイドウィジェットでダッシュボードを作成する

次の例では、2 つのメトリクスウィジェットを並べて表示するダッシュボードを作成します。

JSON

Copy
{ "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

Copy
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"}}]}'