[小ネタ] RDSのDBパラメータグループの設定内容をCSV形式で出力してみた

RDSの「DBパラメータグループ」の設定内容を表形式で出力したいという要件を、AWS CLIとjqコマンドで解決してみました。
2019.08.19

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

みなさん、こんにちは!
AWS事業本部の青柳@福岡オフィスです。

今回は RDS に関する小ネタをお送りします。

やりたいこと: RDSの「DBパラメータグループ」の設定内容を表形式で出力したい

RDS には、各データベースエンジン (MySQL、PostgreSQL など) に固有のパラメータをまとめて管理する「パラメータグループ」という概念があります。
先日、パラメータグループの設定内容を表形式にして、チーム内やお客様と共有したいという機会がありました。

パラメータグループの設定内容を確認するには、RDS のマネジメントコンソールから「パラメータグループ」を選択すれば、一覧を表示することができます。

表示できるんです。・・・ですが、項目数が非常に多く (MySQL 5.7 用パラメータグループの場合は 400 項目以上)、画面上のページ切り替えも大変です。

項目名を検索して表示・編集したりする場合には全く問題ないのですが、一覧を出力したいという今回の要件ではツラいので、マネジメントコンソールで何とかするというのは諦めました。

AWS CLI + jq コマンドで解決!

マネジメントコンソールがダメなら、次に考えるのは AWS CLI です。

AWS CLI でパラメータグループの内容を出力すると、以下のように JSON 形式で出力されます。(デフォルトオプションの場合)

$ aws rds describe-db-parameters --db-parameter-group-name default.mysql5.7
{
    "Parameters": [
        {
            "ParameterName": "allow-suspicious-udfs",
            "Description": "Controls whether user-defined functions that have only an xxx symbol for the main function can be loaded",
            "Source": "engine-default",
            "ApplyType": "static",
            "DataType": "boolean",
            "AllowedValues": "0,1",
            "IsModifiable": false,
            "ApplyMethod": "pending-reboot"
        },
        {
            "ParameterName": "autocommit",
            "Description": "Sets the autocommit mode",
            "Source": "engine-default",
            "ApplyType": "dynamic",
            "DataType": "boolean",
            "AllowedValues": "0,1",
            "IsModifiable": true,
            "ApplyMethod": "pending-reboot"
        },
        {
...

ググってみたところ、JSON での出力結果を jq コマンドを使用して csv 形式に変換するテクニックが紹介されていました。
また、単に csv 形式にするだけでなくヘッダに項目名を付けたいと思いましたが、それを実現する方法も紹介されていました。

それらの情報を参考にして、パラメータグループの内容を csv 形式で出力するコマンドラインを以下のように記述してみました。

$ aws rds describe-db-parameters --db-parameter-group-name default.mysql5.7 \
    | jq -r '["名前","値","許可された値","変更可能","送信元","適用タイプ","データ型","説明","ApplyMethod","MinimumEngineVersion"], (.Parameters[] | [.ParameterName,.ParameterValue,.AllowedValues,.IsModifiable,.Source,.ApplyType,.DataType,.Description,.ApplyMethod,.MinimumEngineVersion]) | @csv' \
    | iconv -t sjis \
    > parameters.csv

ポイント:

  • ヘッダの項目名は、マネジメントコンソールで表示されている日本語の項目名にしています。
    (対応する項目が無い ApplyMethodMinimumEngineVersion は英語のまま・・・)
  • 最終的に Microsoft Excel に読み込ませることを想定して、文字コードを Shift-JIS に変換しています。
    (Linux 上で使う場合は iconv の処理を外して下さい)

出力された csv ファイルは以下のようになりました。

parameter.csv

"名前","値","許可された値","変更可能","送信元","適用タイプ","データ型","説明","ApplyMethod","MinimumEngineVersion"
"allow-suspicious-udfs",,"0,1",false,"engine-default","static","boolean","Controls whether user-defined functions that have only an xxx symbol for the main function can be loaded","pending-reboot",
"autocommit",,"0,1",true,"engine-default","dynamic","boolean","Sets the autocommit mode","pending-reboot",
"auto_generate_certs",,"0,1",false,"engine-default","static","boolean","Controls whether the server autogenerates SSL key and certificate files in the data directory, if they do not already exist.","pending-reboot",
"auto_increment_increment",,"1-65535",true,"engine-default","dynamic","integer","Intended for use with master-to-master replication, and can be used to control the operation of AUTO_INCREMENT columns","pending-reboot",
"auto_increment_offset",,"1-65535",true,"engine-default","dynamic","integer","Determines the starting point for the AUTO_INCREMENT column value","pending-reboot",
"automatic_sp_privileges",,"0,1",true,"engine-default","dynamic","boolean","When this variable has a value of 1 (the default), the server automatically grants the EXECUTE and ALTER ROUTINE privileges to the creator of a stored routine, if the user cannot already execute and alter or drop the routine.","pending-reboot",
"avoid_temporal_upgrade",,"0,1",true,"engine-default","dynamic","boolean","This variable controls whether ALTER TABLE implicitly upgrades temporal columns found to be in pre-5.6.4 format.","pending-reboot",
"back_log",,"1-65535",true,"engine-default","static","integer","The number of outstanding connection requests MySQL can have","pending-reboot",
"basedir","/rdsdbbin/mysql",,false,"system","static","string","The MySQL installation base directory.","pending-reboot",
"binlog_cache_size","32768","4096-18446744073709547520",true,"system","dynamic","integer","The size of the cache to hold the SQL statements for the binary log during a transaction.","pending-reboot",
...

この csv ファイルを Excel にインポートすると、いい感じで表形式にすることができました!

おわりに

今回のように csv 形式で出力したい場合に限らず、AWS CLI の出力結果を jq コマンド等で加工・変換するテクニックはいろいろな場面で有用だと思います。
情報もたくさん公開されていると思いますので、みなさんも困った時には検索等で調べてみるとよろしいのではないでしょうか。

参考にさせて頂いたサイト等

jqを使ってJSONをCSVに変換するいくつかの例。 - Jupitris on Laboratory

シェル芸で使いたい jqイディオム - Qiita

軽量JSONパーサー『jq』のドキュメント:『jq Manual』をざっくり日本語訳してみました | DevelopersIO