$npx -y skills add giuseppe-trisciuoglio/developer-kit --skill aws-cloudformation-cloudwatchProvides AWS CloudFormation patterns for CloudWatch monitoring, metrics, alarms, dashboards, logs, and observability. Use when creating CloudWatch metrics, alarms, dashboards, log groups, log subscriptions, anomaly detection, synthesized canaries, Application Signals, and impleme
| 1 | # AWS CloudFormation CloudWatch Monitoring |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | Creates CloudWatch monitoring infrastructure using CloudFormation templates: metrics, alarms, dashboards, log groups, anomaly detection, synthesized canaries, and Application Signals. |
| 6 | |
| 7 | ## When to Use |
| 8 | |
| 9 | - Creating CloudWatch metrics and alarms for production infrastructure |
| 10 | - Building CloudWatch dashboards for multi-region visualization |
| 11 | - Implementing log groups with retention, encryption, and metric filters |
| 12 | - Configuring anomaly detection and composite alarms |
| 13 | - Setting up cross-stack references with Parameters and Outputs |
| 14 | - Validating and deploying monitoring stacks with CloudFormation |
| 15 | |
| 16 | ## Instructions |
| 17 | |
| 18 | Follow these steps to create CloudWatch monitoring infrastructure with CloudFormation: |
| 19 | |
| 20 | ### 1. Define Alarm Parameters |
| 21 | |
| 22 | Specify metric namespaces, dimensions, and threshold values: |
| 23 | |
| 24 | ```yaml |
| 25 | Parameters: |
| 26 | ErrorRateThreshold: |
| 27 | Type: Number |
| 28 | Default: 5 |
| 29 | Description: Error rate threshold for alarms (percentage) |
| 30 | |
| 31 | LatencyThreshold: |
| 32 | Type: Number |
| 33 | Default: 1000 |
| 34 | Description: Latency threshold in milliseconds |
| 35 | |
| 36 | CpuUtilizationThreshold: |
| 37 | Type: Number |
| 38 | Default: 80 |
| 39 | Description: CPU utilization threshold (percentage) |
| 40 | |
| 41 | LogRetentionDays: |
| 42 | Type: Number |
| 43 | Default: 30 |
| 44 | AllowedValues: |
| 45 | - 1 |
| 46 | - 3 |
| 47 | - 7 |
| 48 | - 14 |
| 49 | - 30 |
| 50 | - 60 |
| 51 | - 90 |
| 52 | - 120 |
| 53 | - 365 |
| 54 | Description: Number of days to retain log events |
| 55 | ``` |
| 56 | |
| 57 | ### 2. Create CloudWatch Alarms |
| 58 | |
| 59 | Set up alarms for CPU, memory, disk, and custom metrics: |
| 60 | |
| 61 | ```yaml |
| 62 | Resources: |
| 63 | HighCpuAlarm: |
| 64 | Type: AWS::CloudWatch::Alarm |
| 65 | Properties: |
| 66 | AlarmName: !Sub "${AWS::StackName}-high-cpu" |
| 67 | AlarmDescription: Trigger when CPU utilization exceeds threshold |
| 68 | MetricName: CPUUtilization |
| 69 | Namespace: AWS/EC2 |
| 70 | Dimensions: |
| 71 | - Name: InstanceId |
| 72 | Value: !Ref InstanceId |
| 73 | Statistic: Average |
| 74 | Period: 60 |
| 75 | EvaluationPeriods: 3 |
| 76 | Threshold: !Ref CpuUtilizationThreshold |
| 77 | ComparisonOperator: GreaterThanThreshold |
| 78 | AlarmActions: |
| 79 | - !Ref AlarmTopic |
| 80 | |
| 81 | ErrorRateAlarm: |
| 82 | Type: AWS::CloudWatch::Alarm |
| 83 | Properties: |
| 84 | AlarmName: !Sub "${AWS::StackName}-error-rate" |
| 85 | MetricName: ErrorRate |
| 86 | Namespace: !Ref CustomNamespace |
| 87 | Dimensions: |
| 88 | - Name: Service |
| 89 | Value: !Ref ServiceName |
| 90 | Statistic: Average |
| 91 | Period: 60 |
| 92 | EvaluationPeriods: 5 |
| 93 | Threshold: !Ref ErrorRateThreshold |
| 94 | ComparisonOperator: GreaterThanThreshold |
| 95 | ``` |
| 96 | |
| 97 | ### 3. Configure Alarm Actions |
| 98 | |
| 99 | Define SNS topics for notification delivery: |
| 100 | |
| 101 | ```yaml |
| 102 | Resources: |
| 103 | AlarmNotificationTopic: |
| 104 | Type: AWS::SNS::Topic |
| 105 | Properties: |
| 106 | DisplayName: !Sub "${AWS::StackName}-alarms" |
| 107 | TopicName: !Sub "${AWS::StackName}-alarms" |
| 108 | |
| 109 | AlarmTopicPolicy: |
| 110 | Type: AWS::SNS::TopicPolicy |
| 111 | Properties: |
| 112 | PolicyDocument: |
| 113 | Statement: |
| 114 | - Effect: Allow |
| 115 | Principal: |
| 116 | Service: cloudwatch.amazonaws.com |
| 117 | Action: sns:Publish |
| 118 | Resource: !Ref AlarmNotificationTopic |
| 119 | Topics: |
| 120 | - !Ref AlarmNotificationTopic |
| 121 | ``` |
| 122 | |
| 123 | ### 4. Create Dashboards |
| 124 | |
| 125 | Build visualization widgets for metrics across resources: |
| 126 | |
| 127 | ```yaml |
| 128 | Resources: |
| 129 | MonitoringDashboard: |
| 130 | Type: AWS::CloudWatch::Dashboard |
| 131 | Properties: |
| 132 | DashboardName: !Sub "${AWS::StackName}-dashboard" |
| 133 | DashboardBody: !Sub | |
| 134 | { |
| 135 | "widgets": [ |
| 136 | { |
| 137 | "type": "metric", |
| 138 | "x": 0, |
| 139 | "y": 0, |
| 140 | "width": 12, |
| 141 | "height": 6, |
| 142 | "properties": { |
| 143 | "title": "CPU Utilization", |
| 144 | "metrics": [["AWS/EC2", "CPUUtilization", "InstanceId", "${InstanceId}"]], |
| 145 | "period": 300, |
| 146 | "stat": "Average", |
| 147 | "region": "${AWS::Region}" |
| 148 | } |
| 149 | } |
| 150 | ] |
| 151 | } |
| 152 | ``` |
| 153 | |
| 154 | ### 5. Set Up Log Groups |
| 155 | |
| 156 | Configure retention policies and encryption settings: |
| 157 | |
| 158 | ```yaml |
| 159 | Resources: |
| 160 | ApplicationLogGroup: |
| 161 | Type: AWS::Logs::LogGroup |
| 162 | Properties: |
| 163 | LogGroupName: !Sub "/aws/applications/${Environment}/${ApplicationName}" |
| 164 | RetentionInDays: !Ref LogRetentionDays |
| 165 | KmsKeyId: !Ref LogEncryptionKey |
| 166 | ``` |
| 167 | |
| 168 | ### 6. Implement Metric Filters |
| 169 | |
| 170 | Create metrics from log data: |
| 171 | |
| 172 | ```yaml |
| 173 | Resources: |
| 174 | ErrorMetricFilter: |
| 175 | Type: AWS::Logs::MetricFilter |
| 176 | Properties: |
| 177 | LogGroupName: !Ref ApplicationLogGroup |
| 178 | FilterPattern: '[level="ERROR", m |