0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

AWSの全てのRDSについているタグを全て取得 PowerShell編

Last updated at Posted at 2019-06-21

全てのRDSについて、タグ情報の一覧を作成したい、というときのお話です。

まあAWSのWeb画面で1つずつ見ていくのでも良いですが、RDSが20も30も、さらに多く存在すると、気が滅入りますね。

そこで、AWS CLIを使って実施します。

EC2の場合

ECSの一覧は

aws ec2 describe-instances

のコマンドで、1回で全EC2のタグ情報を拾うことができます。

RDSの場合

RDSの一覧は、

aws rds describe-db-instances

のコマンドで一覧は取得できます。
ただし、そこにはタグ情報は含まれていません!
[2021/7/11更新]
気が付いたら describe-db-instances に TagList が含まれるように変わっていました。
AWS CLIではいつから変わったのかログが無いですが、LambdaのPython(boto3)では日次で取得していたので結果を確認すると、2021/1/8 からTagListが含まれていました。
この記事は不要になりましたが、list-tags-for-resource でも取得は一応可能なので残しておきます。

RDSのタグ情報を取得する場合、個々のRDSに対して

aws rds list-tags-for-resource --resource-name arn:aws:rds:xxx:xxx:db:xxxx

を実行しないと取り出せませんでした。

よって、RDSが20個あると、

  • aws rds describe-db-instances  が1回
  • aws rds list-tags-for-resource が20回

合計21回実行することになります。

PowerShellコード

PowerShellのコードで書くと、以下のような感じです。


$rdsfile = "C:\temp\rds.txt"
$rdsfile_tag = "C:\temp\rds_tag.txt"
$region = "ap-northeast-1"
$accountid = "99876543210x"

aws rds describe-db-instances --region=$region --query "DBInstances[].{DBInstanceIdentifier:DBInstanceIdentifier}" --output text | Sort-Object > $rdsfile

foreach ($li in Get-Content $rdsfile) {
  $out_line = "$li"

  $tags = $(aws rds list-tags-for-resource --resource-name $("arn:aws:rds:${region}:${accountid}:db:" + $($li -split "\t")[0]) --output text)

  foreach ($othertag in ($tags | sls ^TAGLIST)) {
    $t_1,$t_name,$t_val = $othertag -split "\t"
    $out_line += "`t$t_name=$t_val"
  }

  echo "$out_line" >> $rdsfile_tag
}

$rdsfile_tag 変数で指定したファイルが生成されます。
ファイルの形式は、TSV(タブ区切りのテキストファイル)です。

なんでTSVなんだよ。
ということでjsonを出力するならこのようになります。

$rdsfile = "C:\temp\rds.json"
$rdsfile_tag = "C:\temp\rds_tag.json"
$region = "us-east-1"
$accountid = "99876543210x"

aws rds describe-db-instances --region=$region --query "DBInstances[].{DBInstanceIdentifier:DBInstanceIdentifier}" --output json > $rdsfile

$outobj = @()
$rds_instances=$(cat $rdsfile -Raw | ConvertFrom-Json)

foreach( $item in $rds_instances ) {

  $arn = "arn:aws:rds:${region}:${accountid}:db:" + $item.DBInstanceIdentifier
  $rds_tags = $( $(aws rds list-tags-for-resource --resource-name $arn --output json) -join "" | ConvertFrom-Json)

  $rdsobj = [PSCustomObject] @{ DBInstanceIdentifier = $item.DBInstanceIdentifier }
  foreach( $tag in $rds_tags.TagList ) {
    $rdsobj | Add-Member $tag.Key $tag.Value
  }
  $outobj += $rdsobj
}

$outobj | ConvertTo-Json -compress > $rdsfile_tag

全てのRDSと書きましたが、不正確でした。1つのリージョン内の全てのRDSです。

gist

  • リストの1行目にヘッダを付ける
  • 一部の指定したタグは除外する
  • 一部の指定したタグ指定した順で表示する

このあたりを考慮したコードをgistに置いています。
https://gist.github.com/hayayu0/f067ccbbabaa3dc0d9121aa3eadbf322

PowerShell編以外は?

PowerShellで自分の要望は満たせたので、今のところ記事作成予定はありません。じゃあ何で「編」付けたんだよって話ですが。
bash、Python、は探せばいくつかありそうです。

参考にした情報

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?