tjinjin's blog

インフラ要素多めの個人メモ

grafana on dockerで起動時にdatasourceを追加する

About

以前API経由でgrafanaにdatasourceを追加したことがあったんですが、いつのまにか起動時にファイルとして渡せるようになってたので試しました。

github.com

今日時点(2018/02/12)正式リリースはされてないようなので、masterのイメージを使ってみます。

構成

$ tree
.
├── Dockerfile
├── conf
│   └── provisioning
│       └── datasources
│           └── datasource.yaml
├── create_datasource.rb
└── docker-compose.yml

Dockerfile

/etc/grafana/provisioning/datasourcesにファイルを入れると起動時に自動で読み込んでくれます。

FROM grafana/grafana:master
COPY ./conf/provisioning/datasources/datasource.yaml /etc/grafana/provisioning/datasources/

datasource.yaml

datasources:
  - name: test
    type: cloudwatch
    access: proxy
    url: "http://localhost:5000"
    json_data:
      timeField: "@timestamp"
      authType: arn
      defaultRegion: ap-northeast-1
      assumeRoleArn: arn:aws:iam::01234567890:role/hoge
  - name: test2
    type: cloudwatch
    access: proxy
    url: "http://localhost:5000"
    json_data:
      timeField: "@timestamp"
      authType: arn
      defaultRegion: us-west-2
      assumeRoleArn: arn:aws:iam::01234567890:role/hoge

入力できるパラメータは下記のドキュメントに書いてあります。

docs.grafana.org

docker-compose.yml

version: '3'

services:
  grafana:
    build: .
    ports:
      - "3000:3000"
    volumes:
      #  ローカルにあるcredentialsを使うため
      - ~/.aws/credentials:/usr/share/grafana/.aws/credentials:ro
    environment:
      - GF_SECURITY_ADMIN_PASSWORD=foobar
      - GF_USERS_ALLOW_SIGN_UP=false

あとは起動すれば起動時にdatasourceが追加された状態になります。