• transmit GCP monitoring alerting messages via Pub/Sub with Terraform


    To transmit GCP monitoring alerting messages via Pub/Sub with Terraform, you can use the following steps:

    1. Create a Pub/Sub topic to receive the alert messages:
    1. resource "google_pubsub_topic" "pubsub_topic" {
    2. name = "my-pubsub-topic"
    3. }
    1. Create a Cloud Function to process the alert messages and publish them to the Pub/Sub topic:
    1. resource "google_cloud_function" "pubsub_forward_function" {
    2. name = "my-pubsub-forward-function"
    3. runtime = "python37"
    4. trigger_http = false
    5. source_archive_bucket = "my-bucket"
    6. source_archive_object = "my-function.zip"
    7. handler = "my_function.handler"
    8. environment_variables = {
    9. PUBSUB_TOPIC = google_pubsub_topic.pubsub_topic.name
    10. }
    11. }
    1. Create a google_monitoring_alert_policy resource to configure the alert and specify the Cloud Function as the notification channel:
    1. resource "google_monitoring_alert_policy" "pubsub_alert" {
    2. name = "pubsub_alert"
    3. display_name = "Pub/Sub alert"
    4. description = "Alert when the number of messages received by a subscription exceeds a threshold."
    5. conditions {
    6. metric_name = "pubsub.subscription.messages_received"
    7. resource.type = "pubsub.subscription"
    8. resource.labels.subscription = "my-subscription"
    9. trigger {
    10. count = 100
    11. duration = "1m"
    12. type = "COUNT"
    13. }
    14. }
    15. notification_channels = [google_cloud_function.pubsub_forward_function.name]
    16. }
    1. Create a Terraform configuration file that contains the above resources and deploy it using the terraform apply command.

    Once the Terraform configuration has been deployed, the alert policy will be configured to send notifications to the Cloud Function when the specified conditions are met. The Cloud Function will then process the alert message and publish it to the Pub/Sub topic.

    You can then subscribe to the Pub/Sub topic and process the alert messages in any way that you need. For example, you could use a Cloud Function to send the alert messages to a Slack channel or to a PagerDuty incident.

    Here is an example of a complete Terraform configuration file for transmitting GCP monitoring alerting messages via Pub/Sub:

    1. resource "google_pubsub_topic" "pubsub_topic" {
    2. name = "my-pubsub-topic"
    3. }
    4. resource "google_cloud_function" "pubsub_forward_function" {
    5. name = "my-pubsub-forward-function"
    6. runtime = "python37"
    7. trigger_http = false
    8. source_archive_bucket = "my-bucket"
    9. source_archive_object = "my-function.zip"
    10. handler = "my_function.handler"
    11. environment_variables = {
    12. PUBSUB_TOPIC = google_pubsub_topic.pubsub_topic.name
    13. }
    14. }
    15. resource "google_monitoring_alert_policy" "pubsub_alert" {
    16. name = "pubsub_alert"
    17. display_name = "Pub/Sub alert"
    18. description = "Alert when the number of messages received by a subscription exceeds a threshold."
    19. conditions {
    20. metric_name = "pubsub.subscription.messages_received"
    21. resource.type = "pubsub.subscription"
    22. resource.labels.subscription = "my-subscription"
    23. trigger {
    24. count = 100
    25. duration = "1m"
    26. type = "COUNT"
    27. }
    28. }
    29. notification_channels = [google_cloud_function.pubsub_forward_function.name]
    30. }

    To deploy this configuration, you can use the following command:

    terraform apply
    

    Once the configuration has been deployed, you can test the alert by publishing some messages to the my-subscription subscription. If the number of messages received by the subscription exceeds the threshold, you should receive an alert message on the Pub/Sub topic

  • 相关阅读:
    【云原生 | Kubernetes 系列】----HPA自动伸缩
    2022杭电多校九 1007-Matryoshka Doll(动态规划)
    Pycharm----将Anaconda建立的环境导入
    开发工具记录
    动静态链接库
    LeetCode //C - 208. Implement Trie (Prefix Tree)
    Spring注入bean的常用的六种方式
    固态硬盘接口类型介绍
    Docker无法连接到docker守护程序
    SQL语句中 LEFT JOIN 后 ON 和 WHERE 的区别
  • 原文地址:https://blog.csdn.net/qfljg/article/details/133588889