Cuplikan CloudWatch template Amazon - AWS CloudFormation

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Cuplikan CloudWatch template Amazon

Gunakan contoh cuplikan templat ini untuk membantu mendeskripsikan sumber daya Amazon CloudWatch Anda di AWS CloudFormation. Untuk informasi selengkapnya, lihat referensi jenis CloudWatch sumber daya Amazon.

Alarm penagihan

Dalam contoh berikut, Amazon CloudWatch mengirimkan pemberitahuan email saat menagih ke AWS akun melebihi ambang alarm. Untuk menerima notifikasi penggunaan, aktifkan peringatan penagihan. Untuk informasi selengkapnya, lihat Membuat alarm penagihan untuk memantau perkiraan AWS biaya di Panduan CloudWatch Pengguna Amazon. >

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: '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"

CPUalarm pemanfaatan

Contoh cuplikan berikut membuat alarm yang mengirimkan pemberitahuan ketika rata-rata CPU penggunaan EC2 instans Amazon melebihi 90 persen selama lebih dari 60 detik selama tiga periode evaluasi.

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"

Memulihkan instans Amazon Elastic Compute Cloud

CloudWatch Alarm berikut memulihkan EC2 instance ketika mengalami kegagalan pemeriksaan status selama 15 menit berturut-turut. Untuk informasi selengkapnya tentang tindakan alarm, lihat Membuat alarm untuk menghentikan, menghentikan, mem-boot ulang, atau memulihkan EC2 instance di CloudWatch Panduan Pengguna Amazon.

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

Membuat dasbor dasar

Contoh berikut membuat CloudWatch dasbor sederhana dengan satu widget metrik yang menampilkan CPU pemanfaatan, dan satu widget teks menampilkan pesan.

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

Buat dasbor dengan side-by-side widget

Contoh berikut membuat dasbor dengan dua widget metrik yang muncul berdampingan.

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\",\"amzn-s3-demo-bucket\"]],\"period\":86400,\"stat\":\"Maximum\",\"region\":\"us-east-1\",\"title\":\"amzn-s3-demo-bucket 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","amzn-s3-demo-bucket"]],"period":86400,"stat":"Maximum","region":"us-east-1","title":"amzn-s3-demo-bucket bytes"}}]}'