皆さんは普段 AWS Organizations を触っていて、 組織単位(OU)のIDを覚えてますか? 私は覚えていません。
Organizations を使っていると SCPを適用したり CloudFormation StackSet を展開したりと、 何かと「OUに対する操作」が行われます。 その操作をCLIやSDKで実行するとなると、 OU IDをパラメータに指定する必要があります。
その都度 OU ID をマネコンから見に行くのは面倒ですよね。
そこで今回は OU階層(OU ID含む) を出力するCLIエイリアス を作ってみます。
作成したCLIエイリアス
以下設定を .aws/cli/alias
に付与することで、 aws org-ou-tree
を使えるようになります。
[toplevel]
org-ou-tree =
!f () {
function _outree(){
local parent_name="$1"
local parent_id="$2"
local prefix="$3"
### インプット内容を出力
echo "${prefix} ${parent_name}: ${parent_id}"
### 子OUに対して _outree を実行
local child_prefix=" ${prefix}"
aws organizations list-organizational-units-for-parent --output text \
--parent-id "${parent_id}" --query "OrganizationalUnits[].[Name,Id]" \
| while read child_name child_id; do
_outree "${child_name}" "${child_id}" "${child_prefix}"
done
}
root_id=$(aws organizations list-roots --query "Roots[0].Id" --output text)
_outree "root" "${root_id}" "-"
};f
スクリプト解説
使っているコマンドは以下 2つです。
- aws organizations list-roots : 組織の Root をリストする
- aws organizations list-organizational-units-for-parent : 指定した OU(もしくはRoot) の子OUをリストする
_outree
という再帰関数を使っています。以下処理を実行します。
- インプット内容(OU ID や OU名)を出力する
- OU(もしくはRoot)の子OUを取得する
- 子OUが無い場合は、処理を終了する
- 子OUがある場合は、それぞれの子OUに対して
_outree
を実行する
出力例
以下出力例です。
aws org-ou-tree
# - root: r-abcd
# - Security: ou-abcd-9pexample
# - Infrastructure: ou-abcd-1vexample
# - SDLC: ou-abcd-ebexample
# - PRD: ou-abcd-7mexample
# - Workloads: ou-abcd-v9example
# - SDLC: ou-abcd-ebexample
# - PRD: ou-abcd-7mexample
OU階層と OU ID 情報を取得できていますね。 これでわざわざマネコンを確認することも不要になりました。
おわりに
以上、チョット便利になるCLIエイリアスでした。