LoginSignup
1
0

More than 1 year has passed since last update.

CloudWatchlogsからNewRelic経由でPDへ起票する

Posted at

はじめに

CloudWatchlogsに特定の文字列が出力された時にNewRelicを経由してPagerdutyへ起票する流れを試した時の記録です。
誰かの参考になれば幸いです。

完成イメージ

pdnr.drawio.png

前提条件

・Terraform v0.13.5
・PagerdutyのService等は作成済であること
・NRとPDのインテグレーションは設定済であること
・対象のCloudWatchlogsが作成されていること

Terraform

ディレクトリ構成は以下

root@:~/-newrelic# tree .
.
├── backend.tf
├── condition-log.tf
├── notification.tf
├── policy.tf
├── provider.tf
└── variables.tf

各ファイル

backend.tf

backend.tf
terraform {
  backend "s3" {
    bucket = "S3バケット名"
    key    = "キー名.tfstate"
    acl    = "bucket-owner-full-control"
    region = "AWSリージョン"
  }
}

notification.tf

notification.tf
# Add a notification channel
resource "newrelic_alert_channel" "pagerduty_channel" {
  name = "test-pagerduty"
  type = "pagerduty"

  config {
    service_key = var.pd_service_key
  }
}

# Link the channel to the policy
resource "newrelic_alert_policy_channel" "alert_pagerduty" {
  policy_id  = newrelic_alert_policy.alert_policy.id
  channel_ids = [
    newrelic_alert_channel.pagerduty_channel.id
  ]
}

policy.tf

policy.tf
resource "newrelic_alert_policy" "alert_policy" {
  name = var.project_name
  incident_preference = "PER_CONDITION_AND_TARGET"
}

provider.tf

provier.tf

terraform {
  required_version = "~> 0.13.0"
  required_providers {
    newrelic = {
      source  = "newrelic/newrelic"
      version = "2.14.0"
    }
  }
}

# Configure the NewRelic provider
provider "newrelic" {
  account_id = var.nr_account_id
  api_key = var.api_key
  insecure_skip_verify = true
}

variable.tf

variable.tf
#NewRelicのアカウントID
variable nr_account_id {
  default = NRのアカウントID 
}

#NewRelicのAPIキー
variable api_key {
  default = "NRapiキー NRAK-xxxxxxxxxxxx"
}

#プロジェクト名
variable project_name{
  default = "practice-pj"
}

#AWSアカウント
variable "aws_account_id" {
  default = "AWSアカウントID"
}

#連携するPagerutyのAPIキー
variable pd_service_key {
  default = "PagerutyのAPIキー"
}

#アラート有効/無効 true/false
variable "alert_enabled" {
  default = "true"
}

#ロググループ
variable "log_map_data" {
  type = map

  default = {

    log1 = {
      name      = "practice"
      group     = "ロググループ名"
      target    = "例:resultCode = 200"
    }
  }
}

condition-log.tf

condition-log.tf
resource "newrelic_nrql_alert_condition" "ec2_log_map" {
  for_each                     = var.log_map_data 

  policy_id                    = newrelic_alert_policy.alert_policy.id
  type                         = "static"
  name                         = "ログ監視
  description                  = "Test Logs"
  runbook_url                  = ""
  enabled                      = var.alert_enabled
  value_function               = "single_value"
  violation_time_limit_seconds = 300
  aggregation_window           = 60
  fill_option                  = "none"

  nrql {
    query             = "SELECT COUNT(*) FROM Log FACET aws.logGroup,aws.logStream WHERE message LIKE '%${each.value.target}%' AND aws.logGroup = '${each.value.group}'"
    evaluation_offset = 7
  }

  critical {
    operator              = "above"
    threshold             = 0
    threshold_duration    = 60
    threshold_occurrences = "at_least_once"
  }
}

実行

上記、コードが配置されたディレクトリにて実行する。

terraform init
terraform plan  #作成されるリソースを確認
terraform apply #デプロイ

AWS

CloudWatchログを送信するためのAWSLambdaを作成

下記手順に従い、SAMよりLambdaをデプロイし、トリガーとして対象のCloudWatchログを設定する。
https://docs.newrelic.com/docs/logs/forward-logs/aws-lambda-sending-cloudwatch-logs/

テストしてみる

対象のロググループの[アクション]からログイベントの作成をクリック
image.png

下記、画面が表示されたらNewRelicで検知したい文字列等を入力する。
image.png

・NewRelic側で検知され、PDへ起票されることを確認できたら以上となります。
image.png

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0