• 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

  • 相关阅读:
    JavaScript算法43- 分类求和并作差(leetCode:100103easy)周赛
    在antd里面渲染MarkDown并且自定义一个锚点目录TOC(重点解决导航目录不跟随文档滚动的问题)
    每日一题----昂贵的婚礼
    webpack编译报错Cannot find module ‘@babel/core‘且无法识别es6的reset语法
    Mybatis-Plus主键生成策略
    开发工程师的面经
    UVM RAL模型和内置seq
    数据科学与大数据(学习记录)
    Java学习笔记(一):类和对象
    【无标题】
  • 原文地址:https://blog.csdn.net/qfljg/article/details/133588889