Upgrade to Pro — share decks privately, control downloads, hide ads and more …

DatadogをTerraform で触ってみた

mominosin
January 22, 2019

DatadogをTerraform で触ってみた

Datadogはじめました!#datadogJP

mominosin

January 22, 2019
Tweet

More Decks by mominosin

Other Decks in Technology

Transcript

  1. Terraformの初期設定 API keyとApplication Keyを準備 variable "datadog_api_key" { default = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

    } variable "datadog_app_key" { default = "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy" } provider "datadog" { api_key = "${var.datadog_api_key}" app_key = "${var.datadog_app_key}" }
  2. すでにあるやつ取り込んでみよう サンプルをコピペした設定ファイルを準備(fooのところは書き換えたほうが良い) terraform import でmonitor idを指定し取り込む ❯ terraform import datadog_monitor.foo

    7508938 datadog_monitor.foo: Importing from ID "7508938"... datadog_monitor.foo: Import complete! Imported datadog_monitor (ID: 7508938) datadog_monitor.foo: Refreshing state... (ID: 7508938) Import successful! The resources that were imported are shown above. These resources are now in your Terraform state and will henceforth be managed by Terraform. ❯ head -n 5 datadog_monitor.tf resource "datadog_monitor" "foo" { name = "Name for monitor foo" type = "metric alert" message = "Monitor triggered. Notify: @hipchat-channel" escalation_message = "Escalation message @pagerduty"
  3. 差分を確認 terraorm planで取り込んだ値と設定ファイルの値の差分を比較 ❯ terraform plan |landscape ~ datadog_monitor.foo 取り込んだ値

    設定ファイルの値 escalation_message: "" => "Escalation message @pagerduty" message: -{{#is_alert}} -Storage Space: {{value}} % -@slack-alert -{{/is_alert}} +Monitor triggered. Notify: @hipchat-channel name: "RDS Free Storage Space is very low {{dbinstanceidentifier.name}}" => query: "avg(last_5m):100 - avg:aws.rds.filesystem.usedPercent{env:prd} by { silenced.%: "0" => "1" silenced.*: "" => "0" tags.#: "0" => "2" tags.0: "" => "foo:bar" tags.1: "" => "baz" thresholds.%: "4" => "5" thresholds.critical: "10.0" => "4" thresholds.critical_recovery: "20.0" => "3" thresholds.ok: "" => "0"
  4. Terraform v0.11以下はべた書き 「resources」の括りのみのループ datadogのproviderでは大きい括り - Timeboard - Screenboard 各グラフべた書きするしかない!! resource

    "datadog_timeboard" "fizz_buzz" { title = "fizz_buzz" description = "created using the Datadog provider in Terraform" read_only = true graph { title = "Photo ALB Response Time" viz = "timeseries" precision = 0 request { q = "avg:aws.applicationelb.target_response_time.average{$Target_Group}" type = "line" style { palette = "orange" type = "solid" width = "normal" } } request { q = "week_before(avg:aws.applicationelb.target_response_time.average{$Target_Group})" type = "line" style { palette = "orange" type = "dotted" width = "normal" } } request { q = "avg:aws.applicationelb.target_response_time.average{targetgroup/fizzbuz/xxxxxxxxxxxxxxx}" type = "line" style { palette = "cool" type = "solid" width = "normal" } } request { q = "week_before(avg:aws.applicationelb.target_response_time.average{targetgroup:targetgroup/production-misokatsu/24d81e 06963f1ee3})" type = "line" style { palette = "cool" type = "dotted" width = "normal" } } 切れてますが5倍以上長い→
  5. Terraform v0.12待ち まだPreview版 - 去年の夏には…との噂だった for_each使える テンプレート機能とかもつく variable "graph_list" {

    default = { ALB_Response_time_01 = "avg:aws.applicationelb.target_response_time.average{*}" ALB_Response_time_02 = "avg:aws.applicationelb.target_response_time.average{*}" ALB_Response_time_03 = "avg:aws.applicationelb.target_response_time.average{*}" ALB_Response_time_04 = "avg:aws.applicationelb.target_response_time.average{*}" } } resource "datadog_timeboard" "sample_12" { title = "tf12_test_v12" description = "created using the Datadog provider in Terraform" dynamic "graph" { for_each = var.graph_list content { title = graph.key viz = "timeseries" precision = 0 request { q = graph.value type = "line" style = { palette = "orange" type = "solid" width = "normal" } } } }