https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sns_topic
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_metric_alarm
AWS Chatbotは公式でTerraform化されていないため、以下のように手動で行う。今後はCloudFormationが良さそう。
https://blog.chakimar.net/aws-chatbot-cloudwatch-logs/
目的としては以下、PostgreSQLのACCESS EXCLUSIVEやdeadlockが出た場合にエラー検知する流れ。
https://aws.amazon.com/jp/blogs/news/working-with-rds-and-aurora-postgresql-logs-part-1/
1 |
2024-03-15 08:47:09 UTC:xxx.xxx.xxx.xx.(xxxxx):xxxxxxx@xxxx:[3211]:DETAIL: Process holding the lock: 3217. Wait queue: 3211. |
- rds_parametergroup.tf
1 2 3 4 5 6 |
~省略~ parameter { apply_method = "immediate" name = "deadlock_timeout" value = "5000" } |
- sns.tf
1 2 3 |
resource "aws_sns_topic" "hoge_cloudwatch_logs_rds_monitor" { name = "hoge_cloudwatch-logs-rds-monitor" } |
- cloudwatchlogs.tf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
resource "aws_cloudwatch_log_group" "hoge_db" { name = "/aws/rds/cluster/hoge-db/postgresql" retention_in_days = 7 } resource "aws_cloudwatch_log_metric_filter" "hoge_db_error" { name = "hoge-db-lock-error" pattern = "Process holding the lock" log_group_name = aws_cloudwatch_log_group.hoge_db.name metric_transformation { name = "EventCount" namespace = "hoge_db-lock-error" value = "1" } } |
- cloudwatchlogs_alarm.tf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
resource "aws_cloudwatch_metric_alarm" "hoge_db_lock_error_alarm" { alarm_name = "hoge-db-lock-error-alarm" comparison_operator = "GreaterThanOrEqualToThreshold" evaluation_periods = "1" metric_name = "EventCount" namespace = "hoge-db-lock-error" period = "60" statistic = "Sum" threshold = "1" alarm_description = "This alarm triggers when a 'hoge-db-lock-error' error occurs." actions_enabled = true alarm_actions = [aws_sns_topic.hoge_cloudwatch_logs_rds_monitor.arn] datapoints_to_alarm = 1 } |
- CloudWatch Logsにテストログを送る
1 |
$ aws logs put-log-events --log-group-name "/aws/rds/cluster/hoge-db/postgresql" --log-stream-name "hoge" --log-events timestamp=$(date +%s%3N),message="Process holding the lock" |
Was this helpful?
0 / 0
1989年生まれのSRE。ホスティングから大規模なアドテクなどのインフラエンジニアとして携わる。現在はサービスの信頼性向上、DevOps、可用性、レイテンシ、パフォーマンス、モニタリング、オブザーバビリティ、緊急対応、AWS/Azureでのインフラ構築、Docker開発環境の提供、Kubernetes保守、インフラコード化、新技術の検証、リファクタリング、セキュリティ強化などを担当している。個人事業主では数社サーバー保守とベンチャー企業のSREインフラコンサルティングやMENTAで未経験者にインフラのコーチング/コミュニティの運用を実施している。また、「脆弱性スキャナVuls」のOSS活動もしており、自称エバンジェリスト/技術広報/テクニカルサポート/コントリビュータでもある。