cloud-monitoring-notification-channels-configuration
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCloud Monitoring Notification Channels Configuration
Cloud Monitoring 通知渠道配置
Context Provided
提供的上下文
-
Terraform Only: Write the generated observability configuration ONLY as Terraform () files (e.g.,
.tf).notification_channels.tf- Install Terraform ONLY if you are asked to deploy the channels AND there is no valid Terraform installation.
- If you are NOT asked to deploy the channels, DO NOT install Terraform.
-
Working Directory Inference: Determine the target path forfiles using the following order:
.tf- If the user provides a path, use that exact path for the file.
.tf - If a path is not provided, search the workspace for existing files (preferring files that already define
.tforgoogle_monitoring_notification_channelresources, or existing directories in the strict preference order:google_monitoring_alert_policy, thenmonitoring/, thenops/). If found, writesre/in that directory (or update the existingnotification_channels.tffile when modifying an existing resource)..tf - If a path is not provided and no files or target directories exist in the workspace, create and write to
.tf.monitoring/notification_channels.tf
- If the user provides a path, use that exact path for the
-
Google Cloud Prerequisites: When runningCLI discovery commands or deploying configurations with the
gcloudCLI against a Google Cloud project, ensure the following prerequisites are met in order (do not execute these commands if the user only asks to generate or print Terraform HCL):terraform- Target Project ID & Project Number:
- Identify the target Google Cloud from the user's request or via
{project_id}. If neither is available, prompt the user to providegcloud config get-value projectbefore continuing.{project_id} - When the Google Cloud project number () is needed (such as constructing the Cloud Monitoring Service Agent identity for Pub/Sub channels):
{project_number}- Method A (Standard): Run .
gcloud projects describe {project_id} --format='value(projectNumber)' - Method B (Ask User): If the command fails or you cannot retrieve the project number, ask the user to provide it.
- Method A (Standard): Run
- Identify the target Google Cloud
- Authentication: Authenticate the CLI via
gcloudand configure Application Default Credentials (ADC) for Terraform viagcloud auth login.gcloud auth application-default login - Project Configuration & Billing: Configure the target Google Cloud
project via (and ensure the project has an active billing account attached).
gcloud config set project {project_id}- Recovery Actions: If or
gcloud config set project {project_id}fails, verify active credentials withgcloud projects describe {project_id}, list accessible projects withgcloud auth list, or prompt the user to confirm the project ID and access permissions. If deployment fails due to a missing or inactive billing account, stop deployment and instruct the user to link an active billing account (viagcloud projects listor in the Google Cloud console) before retrying.gcloud billing projects link {project_id} --billing-account={account_id}
- Recovery Actions: If
- IAM Permissions: Ensure the principal has permission to manage
notification channels—either via the predefined Monitoring
NotificationChannel Editor role
(, plus
roles/monitoring.notificationChannelEditorwhen adding a channel to an existing alert policy) or via a custom IAM role containing the equivalent permissions (roles/monitoring.alertPolicyEditor,monitoring.notificationChannels.get,monitoring.notificationChannels.list,monitoring.notificationChannels.create,monitoring.notificationChannels.update, andmonitoring.notificationChannels.delete/monitoring.alertPolicies.getwhen updating an alert policy).monitoring.alertPolicies.update- Recovery Actions: Users with a custom role granting these
permissions do not need the predefined role name. If a or
gcloudcommand fails withterraform(HTTP 403), stop execution, identify the missingPERMISSION_DENIEDormonitoring.notificationChannels.*permission, and instruct the user to ask a project administrator to grantmonitoring.alertPolicies.*(or a custom role with the required permissions) to their principal.roles/monitoring.notificationChannelEditor
- Recovery Actions: Users with a custom role granting these
permissions do not need the predefined role name. If a
- Target Project ID & Project Number:
-
Discovery Tips:
- Listing Notification Channels: You MUST use to list notification channels. Do NOT use any other
gcloud beta monitoring channels list --project={project_id}commands to list notification channels.gcloud - Verifying Pub/Sub Topics: When configuring a Pub/Sub notification
channel, run
scripts/verify_pubsub_topic.shto verify that:{monitoring_project_id} {topic_name_or_path}- The Pub/Sub API () is enabled in the topic's project (supporting both same-project topics and cross-project topics specified as
pubsub.googleapis.com).projects/{topic_project_id}/topics/{topic_name} - The topic exists and the monitoring project's Cloud Monitoring
Service Agent
() holds
service-{monitoring_project_number}@gcp-sa-monitoring-notification.iam.gserviceaccount.comon the topic or topic project.roles/pubsub.publisher - The topic does not define a message storage policy
(/
messageStoragePolicy) and does not have in-transit enforcement (message_storage_policy/enforceInTransit) enabled. DO NOT use a Pub/Sub topic that defines a message storage policy or enforces in-transit restrictions as a notification channel, because Cloud Monitoring cannot deliver messages to such topics.enforce_in_transit
- The Pub/Sub API (
- Listing Notification Channels: You MUST use
-
Attribution: You MUST tag EVERY applicable command and Terraform resource below that you create, modify, or suggest to the user.
-
Terraform HCL ():
user_labels-
Everyresource that you create MUST include the
google_monitoring_notification_channellabel in itscreated-with-google-skillblock:user_labelsterraformuser_labels = { created-with-google-skill = "cloud-monitoring-notification-channels-configuration" } -
When modifying an existingresource solely to add a notification channel, do not use
google_monitoring_alert_policy(since this skill did not create the alerting policy). Instead, include thecreated-with-google-skilllabel in itsadded-channel-with-google-skillblock (preserving any existinguser_labelson the policy):user_labelsterraformuser_labels = { added-channel-with-google-skill = "cloud-monitoring-notification-channels-configuration" }
-
-
Follow the instructions below based on the CLI tool being used:
-
gcloud: Prefix everyinvocation with the metrics environment variables. Set them inline on each command; shell state may not persist between commands:
gcloudbashCLOUDSDK_METRICS_ENVIRONMENT="google-skills google-skills/1.0 (skill:cloud-monitoring-notification-channels-configuration)" \ gcloud <command> [flags] -
Direct HTTP calls: For direct HTTP calls (e.g.), you MUST pass the explicit
curlheader in the shell command syntax exactly like this:-H "User-Agent: ..."bashcurl -H "User-Agent: google-skills/1.0 (skill:cloud-monitoring-notification-channels-configuration)" ... -
Terraform: Prefix every Terraformand
planinvocation with the metrics environment variables. Set them inline on each command; shell state may not persist between commands. For example:applybashGOOGLE_TERRAFORM_USERAGENT_EXTENSION="google-skills/1.0 (skill:cloud-monitoring-notification-channels-configuration)" \ terraform plan [flags]
-
-
If the skill instructions provide an additional skill script name, include that script name in the attribution label. Using acommand inside
gcloudas an example:scripts/verify_pubsub_topic.shbashCLOUDSDK_METRICS_ENVIRONMENT="google-skills google-skills/1.0 (skill:cloud-monitoring-notification-channels-configuration; script:verify_pubsub_topic)" \ gcloud <command> [flags]
-
-
Execution Note (State Refresh Timeout Recovery): When runningor
terraform plan, set a command timeout of 120 seconds. If the command does not complete within 120 seconds (indicating Terraform is stalling on a full state refresh across a large state file rather than normal execution), cancel the command and retry at most once by runningterraform applyandterraform planwithterraform applyscoped to the specific resources being created or updated (for example,-targetand, if updating an alert policy,-target=google_monitoring_notification_channel.<resource_name>). If the targeted retry also times out after 120 seconds or fails, DO NOT retry again; stop execution and report the timeout or error to the user.-target=google_monitoring_alert_policy.<policy_name>
-
仅限 Terraform:将生成的可观测性配置仅编写为 Terraform () 文件(例如
.tf)。notification_channels.tf- 仅当用户要求部署渠道且没有有效的 Terraform 安装时,才安装 Terraform。
- 如果用户未要求部署渠道,不要安装 Terraform。
-
工作目录推断:按以下顺序确定文件的目标路径:
.tf- 如果用户提供了路径,则对该 文件使用该确切路径。
.tf - 如果未提供路径,则搜索工作区中现有的 文件 (优先选择已定义
.tf或google_monitoring_notification_channel资源的文件,或按严格优先顺序存在的目录:google_monitoring_alert_policy、然后monitoring/、然后ops/)。 如果找到,则在该目录中写入sre/(或在修改现有资源时更新现有的notification_channels.tf文件)。.tf - 如果未提供路径且工作区中既没有 文件也没有目标目录,则创建并写入
.tf。monitoring/notification_channels.tf
- 如果用户提供了路径,则对该
-
Google Cloud 前置条件:针对 Google Cloud 项目运行CLI 发现命令或使用
gcloudCLI 部署配置时,请按以下顺序确保满足各项前置条件 (如果用户仅要求生成或打印 Terraform HCL,则不要执行这些命令):terraform- 目标项目 ID 与项目编号:
- 从用户请求或 中识别目标 Google Cloud
gcloud config get-value project。如果两者均不可用,请在继续之前提示用户提供{project_id}。{project_id} - 当需要 Google Cloud 项目编号()时 (例如,为 Pub/Sub 渠道构建 Cloud Monitoring Service Agent 身份):
{project_number}- 方法 A(标准方式):运行 。
gcloud projects describe {project_id} --format='value(projectNumber)' - 方法 B(询问用户):如果命令失败或无法 获取项目编号,请要求用户提供。
- 方法 A(标准方式):运行
- 从用户请求或
- 身份验证:通过 验证
gcloud auth loginCLI,并通过gcloud为 Terraform 配置 Application Default Credentials(ADC)。gcloud auth application-default login - 项目配置与结算:通过 配置目标 Google Cloud 项目(并确保项目已关联有效的结算账号)。
gcloud config set project {project_id}- 恢复操作:如果 或
gcloud config set project {project_id}失败,使用gcloud projects describe {project_id}验证当前有效凭据,使用gcloud auth list列出可访问的项目, 或提示用户确认项目 ID 和访问权限。如果部署因缺少或未启用结算账号而失败, 停止部署,并指示用户先关联有效结算账号 (通过gcloud projects list或在 Google Cloud 控制台操作), 然后重试。gcloud billing projects link {project_id} --billing-account={account_id}
- 恢复操作:如果
- IAM 权限:确保主体拥有管理通知渠道的权限——可通过预定义的 Monitoring
NotificationChannel Editor 角色
(;将渠道添加到现有告警策略时还需
roles/monitoring.notificationChannelEditor) 授予,也可通过包含等效权限的自定义 IAM 角色授予 (roles/monitoring.alertPolicyEditor,monitoring.notificationChannels.get,monitoring.notificationChannels.list,monitoring.notificationChannels.create,monitoring.notificationChannels.update,以及monitoring.notificationChannels.delete/monitoring.alertPolicies.get)。monitoring.alertPolicies.update- 恢复操作:拥有授予这些权限的自定义角色的用户不需要使用预定义角色名称。
如果 或
gcloud命令因terraform(HTTP 403)而失败, 停止执行,识别缺失的PERMISSION_DENIED或monitoring.notificationChannels.*权限,并指示用户要求项目管理员向其主体授予monitoring.alertPolicies.*(或包含所需权限的自定义角色)。roles/monitoring.notificationChannelEditor
- 恢复操作:拥有授予这些权限的自定义角色的用户不需要使用预定义角色名称。
如果
- 目标项目 ID 与项目编号:
-
资源发现提示:
- 列出通知渠道:必须使用 列出通知渠道。不要 使用任何其他
gcloud beta monitoring channels list --project={project_id}命令列出通知渠道。gcloud - 验证 Pub/Sub 主题:配置 Pub/Sub 通知
渠道时,运行
scripts/verify_pubsub_topic.sh,以验证:{monitoring_project_id} {topic_name_or_path}- Pub/Sub API () 已在主题所在项目中启用 (同时支持同一项目中的主题,以及以
pubsub.googleapis.com指定的跨项目主题)。projects/{topic_project_id}/topics/{topic_name} - 主题存在,并且监控项目的 Cloud Monitoring
Service Agent
() 对该主题或主题所在项目拥有
service-{monitoring_project_number}@gcp-sa-monitoring-notification.iam.gserviceaccount.com。roles/pubsub.publisher - 主题未定义消息存储策略
(/
messageStoragePolicy),且未 启用传输中强制执行 (message_storage_policy/enforceInTransit)。不要将定义了 消息存储策略或启用了传输中限制的主题用作通知渠道, 因为 Cloud Monitoring 无法向此类主题传送消息。enforce_in_transit
- Pub/Sub API (
- 列出通知渠道:必须使用
-
归因标记:必须为下文创建、修改或建议给用户的每条适用命令和 Terraform 资源添加标记。
-
Terraform HCL ():
user_labels-
创建的每个资源都必须在 其
google_monitoring_notification_channel块中包含user_labels标签:created-with-google-skillterraformuser_labels = { created-with-google-skill = "cloud-monitoring-notification-channels-configuration" } -
当仅为了添加通知渠道而修改现有资源时,不要使用
google_monitoring_alert_policy(因为此 skill 并未创建该告警策略)。相反,在其created-with-google-skill块中包含user_labels标签(并保留策略上现有的added-channel-with-google-skill):user_labelsterraformuser_labels = { added-channel-with-google-skill = "cloud-monitoring-notification-channels-configuration" }
-
-
根据所使用的 CLI 工具遵循以下说明:
-
gcloud: 为每次调用添加指标环境变量前缀。 在每条命令中内联设置;shell 状态可能在命令之间不会保留:
gcloudbashCLOUDSDK_METRICS_ENVIRONMENT="google-skills google-skills/1.0 (skill:cloud-monitoring-notification-channels-configuration)" \ gcloud <command> [flags] -
直接 HTTP 调用:对于直接 HTTP 调用(例如),必须按照以下确切语法, 在 shell 命令中传入显式的
curl请求头:-H "User-Agent: ..."bashcurl -H "User-Agent: google-skills/1.0 (skill:cloud-monitoring-notification-channels-configuration)" ... -
Terraform: 为每次 Terraform和
plan调用添加指标环境变量前缀。 在每条命令中内联设置;shell 状态可能在命令之间不会保留。例如:applybashGOOGLE_TERRAFORM_USERAGENT_EXTENSION="google-skills/1.0 (skill:cloud-monitoring-notification-channels-configuration)" \ terraform plan [flags]
-
-
如果 skill 指令提供了额外的 skill 脚本名称,请将该脚本名称包含在归因标签中。 例如,在中使用
scripts/verify_pubsub_topic.sh命令:gcloudbashCLOUDSDK_METRICS_ENVIRONMENT="google-skills google-skills/1.0 (skill:cloud-monitoring-notification-channels-configuration; script:verify_pubsub_topic)" \ gcloud <command> [flags]
-
-
执行说明(状态刷新超时恢复):运行或
terraform plan时,将命令超时设置为 120 秒。如果 命令未能在 120 秒内完成(这表明 Terraform 正在对大型状态文件执行完整状态刷新并因此停滞, 而非正常执行),请取消该命令,并且最多重试一次:运行带terraform apply的-target和terraform plan,将范围限定到正在创建或更新的特定资源 (例如terraform apply,以及在更新告警策略时使用-target=google_monitoring_notification_channel.<resource_name>)。如果定向重试再次达到 120 秒超时或失败, 不要再次重试;停止执行并向用户报告超时或错误。-target=google_monitoring_alert_policy.<policy_name>
Generation
生成
Input Assumptions (checklist)
输入假设(检查清单)
- Notification Channel Type: When creating a new notification channel, the
user MUST provide the notification channel type to configure.
- If no type is provided (and the user is not attaching an existing notification channel to an alert policy), you MUST prompt the user to ask for a type before continuing.
- Sensitive Credentials File: Some notification channels require the use
of sensitive credentials in their configuration. You MUST verify that the
user has a file in the working directory. If the file does not exist, create one using the template provided in
secret.tfvarsand notify the user that credentials should be stored there.assets/secret.tfvars
- 通知渠道类型:创建新通知渠道时,
用户必须提供要配置的通知渠道类型。
- 如果未提供类型(且用户并非将现有通知渠道附加到告警策略), 必须提示用户提供类型后再继续。
- 敏感凭据文件:某些通知渠道的配置需要使用敏感凭据。
必须验证工作目录中是否存在 文件。如果该文件不存在, 则使用
secret.tfvars中提供的模板创建一个文件, 并告知用户应将凭据存储在该文件中。assets/secret.tfvars
Execution Steps
执行步骤
-
Open the reference file corresponding to the notification channel the user wants to configure:
Notification Channel Type Reference File email email.md sms sms.md slack slack.md pubsub pubsub.md pagerduty pagerduty.md webhook webhook.md -
Follow the guidelines in thesection (if any) to complete all requirements for the channel configuration.
Prerequisites -
If the reference file contains asection under
Required Credentials, you MUST confirm that the listed required credentials are configured in theSpecificationfile.secret.tfvars- If any of the credentials are set to empty or not found in the file, you MUST prompt the user to provide them before proceeding.
-
Use the template provided in thesection under
Terraform HCLinside the reference file replacing the specified variables as needed to write the new configuration into the output file. If the user explicitly specifies aSpecificationfor the notification channel, setdisplay_nameto that exact string instead of the defaultdisplay_nametemplate string."<Type> Notification Channel (...)" -
Adding a Channel to an Existing Alert Policy: When modifying an existingresource to use an existing
google_monitoring_alert_policyresource (without creating a new channel), setgoogle_monitoring_notification_channelon thenotification_channelsblock and set thegoogle_monitoring_alert_policylabel toadded-channel-with-google-skillin its"cloud-monitoring-notification-channels-configuration"block (preserving any existinguser_labels):user_labelsterraformnotification_channels = [ google_monitoring_notification_channel.<channel_resource_name>.name, ] user_labels = { added-channel-with-google-skill = "cloud-monitoring-notification-channels-configuration" }
-
打开用户要配置的通知渠道所对应的参考文件:
通知渠道类型 参考文件 email email.md sms sms.md slack slack.md pubsub pubsub.md pagerduty pagerduty.md webhook webhook.md -
遵循部分中的指南(如果有),完成 渠道配置的所有要求。
Prerequisites -
如果参考文件的下包含
Specification部分, 必须确认其中列出的所需凭据已配置在Required Credentials文件中。secret.tfvars- 如果任何凭据为空或未在该文件中找到, 必须提示用户提供后再继续。
-
使用参考文件下
Specification部分提供的模板, 根据需要替换指定变量,以将新配置写入输出文件。如果用户明确为通知渠道指定了Terraform HCL,则将其设为该确切字符串,而不是默认模板字符串display_name。"<Type> Notification Channel (...)" -
将渠道添加到现有告警策略:修改现有资源以使用现有的
google_monitoring_alert_policy资源(不创建新渠道)时, 在google_monitoring_notification_channel块上设置google_monitoring_alert_policy, 并在其notification_channels块中将user_labels标签设为added-channel-with-google-skill(同时保留现有的"cloud-monitoring-notification-channels-configuration"):user_labelsterraformnotification_channels = [ google_monitoring_notification_channel.<channel_resource_name>.name, ] user_labels = { added-channel-with-google-skill = "cloud-monitoring-notification-channels-configuration" }
Outputs & Formats
输出与格式
Write all channel configurations into a file under
the directory chosen in the #context-provided section (or update the target
file when adding notification channels to an existing
). Ensure every generated
resource sets
to
in , and
every modified resource sets
to
in .
notification_channels.tf.tfgoogle_monitoring_alert_policygoogle_monitoring_notification_channelcreated-with-google-skill"cloud-monitoring-notification-channels-configuration"user_labelsgoogle_monitoring_alert_policyadded-channel-with-google-skill"cloud-monitoring-notification-channels-configuration"user_labels将所有渠道配置写入 部分所选目录下的
文件(或在向现有
添加通知渠道时更新目标 文件)。
确保每个生成的 资源都在
中将 设为
,并且每个修改的
资源都在 中将
设为
。
#context-providednotification_channels.tfgoogle_monitoring_alert_policy.tfgoogle_monitoring_notification_channeluser_labelscreated-with-google-skill"cloud-monitoring-notification-channels-configuration"google_monitoring_alert_policyuser_labelsadded-channel-with-google-skill"cloud-monitoring-notification-channels-configuration"