vague memory

うろ覚えを無くしていこうともがき苦しむ人の備忘録

Terraform for_each で map 使用時の index 取得

Terraform v0.12.6 から利用できるようになった for_each で count.index の様な連番付与が行えないか調べました。

for_each の利点無くなるので結果的に使わなかったのですが、何かに使えるかもしれないのでメモ。 定義した変数を基にするのであれば、そこに含めてしまえば良いので出番は無いでしょう。

version

  • >= v0.12.6
$ terraform version
Terraform v0.14.6
+ provider registry.terraform.io/hashicorp/aws v3.28.0

以下、便宜的に resource aws_sns_topic を指定していますが特に意味は無いです。

map

locals {
  # map定義
  servers = {
    "serverA" = { role = "web" }
    "serverB" = { role = "ap" }
    "serverC" = { role = "db" }
  }
  # mapからindex作成
  servers_index = [for k, v in local.servers : k]
}

# resource定義
resource "aws_sns_topic" "sns_topic" {
  for_each = local.servers
  name     = "${each.key}-${each.value.role}-${index(local.servers_index, each.key)}"
}

plan

  # aws_sns_topic.sns_topic["serverA"] will be created
  + resource "aws_sns_topic" "sns_topic" {
      + arn    = (known after apply)
      + id     = (known after apply)
      + name   = "serverA-web-0"
      + policy = (known after apply)
    }

  # aws_sns_topic.sns_topic["serverB"] will be created
  + resource "aws_sns_topic" "sns_topic" {
      + arn    = (known after apply)
      + id     = (known after apply)
      + name   = "serverB-ap-1"
      + policy = (known after apply)
    }

  # aws_sns_topic.sns_topic["serverC"] will be created
  + resource "aws_sns_topic" "sns_topic" {
      + arn    = (known after apply)
      + id     = (known after apply)
      + name   = "serverC-db-2"
      + policy = (known after apply)
    }

list

  • for_each に list は直接渡せないため変換が必要
The given "for_each" argument value is unsuitable: the "for_each" argument
must be a map, or set of strings, and you have provided a value of type tuple.
locals {
  # list定義
  server_list = [
    "serverA", "serverB", "serverC"
  ]
  # listからmapに変換
  server_list_to_map = { for i, j in local.server_list : j => { index = i } }
}

# tosetを使用した場合、key,valueが同値となる
# > each.key and each.value are the same for a set
resource "aws_sns_topic" "sns_topic_list" {
  for_each = toset(local.server_list)
  name     = "${each.key}-${each.value}"
}
# 変換したmapを使用
resource "aws_sns_topic" "sns_topic_list_to_map" {
  for_each = local.server_list_to_map
  name     = "${each.key}-${each.value.index}"
}

plan

  # aws_sns_topic.sns_topic_list["serverA"] will be created
  + resource "aws_sns_topic" "sns_topic_list" {
      + arn    = (known after apply)
      + id     = (known after apply)
      + name   = "serverA-serverA"
      + policy = (known after apply)
    }

  # aws_sns_topic.sns_topic_list["serverB"] will be created
  + resource "aws_sns_topic" "sns_topic_list" {
      + arn    = (known after apply)
      + id     = (known after apply)
      + name   = "serverB-serverB"
      + policy = (known after apply)
    }

  # aws_sns_topic.sns_topic_list["serverC"] will be created
  + resource "aws_sns_topic" "sns_topic_list" {
      + arn    = (known after apply)
      + id     = (known after apply)
      + name   = "serverC-serverC"
      + policy = (known after apply)
    }

  # aws_sns_topic.sns_topic_list_to_map["serverA"] will be created
  + resource "aws_sns_topic" "sns_topic_list_to_map" {
      + arn    = (known after apply)
      + id     = (known after apply)
      + name   = "serverA-0"
      + policy = (known after apply)
    }

  # aws_sns_topic.sns_topic_list_to_map["serverB"] will be created
  + resource "aws_sns_topic" "sns_topic_list_to_map" {
      + arn    = (known after apply)
      + id     = (known after apply)
      + name   = "serverB-1"
      + policy = (known after apply)
    }

  # aws_sns_topic.sns_topic_list_to_map["serverC"] will be created
  + resource "aws_sns_topic" "sns_topic_list_to_map" {
      + arn    = (known after apply)
      + id     = (known after apply)
      + name   = "serverC-2"
      + policy = (known after apply)
    }

Opsgenie Monit Integration

Monit から Opsgenie へアラートを通知します。

f:id:htnosm:20210118075308p:plain

公式ではメールでの連携方法が紹介されていますが、 各監視対象ホストでメール送信を有効化するのが手間だったのでAPIを利用します。

前置き

軽く探索してみた所、下記情報があります。

これらを参考にスクリプトを作成しました。

Monit -> Opsgenie 通知用スクリプト

設定

Opsgenie APIキー取得

REST API のインテグレーションから取得します。

  • [Team] -> 対象のチーム -> [Integrations] -> APIインテグレーション

f:id:htnosm:20210118075313p:plain

スクリプト格納

監視対象ホストにて、任意の場所にスクリプトをダウンロードします。

wget https://raw.githubusercontent.com/htnosm/opsgenie-monit.sh/main/opsgenie-trigger
wget https://raw.githubusercontent.com/htnosm/opsgenie-monit.sh/main/opsgenie-resolve

ダウンロードした2つのスクリプトに実行権限を与え、スクリプト内の OPSGENIE_API_KEY を書き換えます。

# 実行権限付与
chmod +x opsgenie-*
# OPSGENIE_API_KEY="1234567890abcdef1234567890abcdef"
↓
OPSGENIE_API_KEY="Opsgenie APIキー"

Monit設定

Failure、Success それぞれにアラート作成、クローズのスクリプトを指定します。

check process crond with pidfile /var/run/crond.pid
    if does not exist for 3 cycles
        then exec "/etc/monit/opsgenie-trigger"
    else if passed for 3 cycles
        then exec "/etc/monit/opsgenie-resolve"

正常に設定できていれば、Monit に連動して Opsgenie のアラート作成とクローズが行えます。

  • Alert created via API

f:id:htnosm:20210118075319p:plain

  • Alert closed via API

f:id:htnosm:20210118075324p:plain

MacOS コマンドラインで「選択したビデオファイルをエンコード」

選択したビデオファイルをエンコード(Encode Selected Video Files)

f:id:htnosm:20201231032200p:plain

上の様にFinderから実行できますが、 コマンドライン実行したかったので調査。 Mac OS X 10.7 Lion から追加された基本機能とのこと。

avconvert

  • オーディオのみ メニューと同等のコマンド例
$ avconvert --preset PresetAppleM4A --source test.mov --output test.m4a

avconvert completed with error:0.

$ file test.*
test.m4a:     ISO Media, Apple iTunes ALAC/AAC-LC (.M4A) Audio
test.mov:     ISO Media, Apple QuickTime movie, Apple QuickTime (.MOV/QT)

オプション

usage: avconvert [--param <value> ...]

    Required parameters:
        --preset (or -p)          <presetName>  Use --help to list all available preset names.
        --source (or -s)          <source media file>
        --output (or -o)          <output movie file>

    Optional parameters:
        --disableFastStart        Disable fast-start movie creation.  Reduces disk accesses if fast-start is not required.
        --disableMetadataFilter   Disable the metadata filter.  Use with caution.  This will allow privacy sensitive source metadata to be preserved in the output file.  Refer to the man page for more details.
        --duration <duration>     Trim the output movie to duration seconds (decimal allowed).  Default is end of file.
        --help (or -h)            Print command usage and list available preset names.
        --multiPass               Perform a higher quality multi-pass encode in the conversion, if supported by the codec.
        --progress (or -prog)     Display progress information.
        --replace                 Overwrite the output file, if it already exists.
        --start <startTime>       Skip the first startTime seconds (decimal allowed) of the source movie.  Default is beginning of file.
        --verbose (or -v)         Print additional information about the conversion.

参考

Amazon EventBridge Replay

EventBridgeのアーカイブ・再生機能がリリースされたのでメモ。

ドキュメントに記載されている内容

  • 再生する対象は期間で指定
    • イベント発生履歴をEventBridgeのコンソール上から確認できない(受信先で確認する必要がある)
  • 同じ順序で再生されるとは限らない
  • アーカイブされたイベントバスへのイベントの再生のみがサポート
    • 現在 とあるので機能拡張されそう

その他

初回のイベント発生時とリプレイ(再生)とで全く同じイベントが再生されるかと思いきや、 実際に実行してみたところ若干異なっていました。

  • "replay-name" が付与される
  • null値のキーが排除される
  • マルチバイトが文字化けする
{
  "id": "1",
  "user": null,  
  "title": "再生TEST",
  "contents": []
}

上記のようなイベントを再生した場合、以下のようになりました。

{
  "id": "1",
  "replay-name": "ReplayTest",
  "title": "??TEST",
  "contents": []
}

Backlog 課題の文字数制限

公式に記載を見つけられなかったのでメモ。

  • 件名は1文字以上255文字以下で入力してください。
  • 詳細は100,000文字以下で入力してください。
  • コメントは50,000文字以下で入力してください。

参考

その他制限等

Datadog AWS Integration リージョン除外設定

Datadog AWSインテグレーション設定にいつの間にか機能追加されていたのでメモ。

※ 2020/07 現時点ではUIからの参照・更新はサポートされていないようです

excluded_regions
An array of AWS regions to exclude from metrics collection

IAM Role 等で明示的に拒否しない限りは、全リージョンを参照していたと思われますが、 設定が可能になった事により不要なリージョンへのAPIリクエスト数削減が見込めそうです。

尚、 Terraform の Datadog Providor では既に機能追加されています。

    excluded_regions = ["us-east-1", "us-west-2"]

Datadog hoge before 関数はエイリアス

Datadog のグラフ作成時に過去情報を参照する関数が用意されています。

  • hour_before : 1時間前
  • day_before : 1日前
  • week_before : 7日前
  • month_before : 28日前(4週間≒1ヶ月)

f:id:htnosm:20200719152120p:plain

これらは期間固定なため、2日前とか任意期間での指定はできないものかと調べたところ、公式ドキュメントにまんまの記載がありました。

timeshift(<METRIC_NAME>{*}, -<TIME_IN_SECOND>)

hoge_before は timeshift 関数のエイリアスのようです。
秒数指定で任意の日時のグラフ表示が可能です。

注意点として、 UI上でサジェストされないため、直入力の必要があります。

f:id:htnosm:20200719152128p:plain