Skip to content

Instantly share code, notes, and snippets.

@MasahiroKawahara
Last active July 11, 2024 07:02
Show Gist options
  • Save MasahiroKawahara/6248712087b298de89b050f72c9c950b to your computer and use it in GitHub Desktop.
Save MasahiroKawahara/6248712087b298de89b050f72c9c950b to your computer and use it in GitHub Desktop.
cfn-all-ss-ou : 全ての Service-managed StackSet の展開先OU(パス形式)を表示 ※委任管理者上で実施
[toplevel]
### Organizations
# 組織のルートIDを表示する
org-root = organizations list-roots --query "Roots[0].Id" --output text --no-paginate
# OU構造をパス形式で出力する
# -- depends on org-root
org-ou-paths =
!f () {
function _oupaths(){
local parent_name="$1"
local parent_id="$2"
local prefix="$3"
### OUパスを出力
echo "${prefix}/${parent_name} ${parent_id}"
### 子OUに対して再帰的に _oupath を実行
local child_prefix="${prefix}/${parent_name}"
aws organizations list-organizational-units-for-parent --output text \
--parent-id "${parent_id}" --query "OrganizationalUnits[].[Name,Id]" \
| while read child_name child_id; do
_oupaths "${child_name}" "${child_id}" "${child_prefix}"
done
}
root_id=$(aws org-root)
_oupaths "root" "${root_id}" ""
};f
### CloudFormation
# Service-managed StackSet 一覧を表示 ※委任管理者上で実施
cfn-ls-org-ss = cloudformation list-stack-sets --output text --status ACTIVE --query "Summaries[?PermissionModel=='SERVICE_MANAGED'].[StackSetName]" --call-as DELEGATED_ADMIN
# 全ての Service-managed StackSet の展開先OU(パス形式)を表示 ※委任管理者上で実施
# -- depends on org-ou-paths cfn-ls-org-ss
cfn-all-ss-ou =
!f () {
all_ou_paths=$(aws org-ou-paths)
aws cfn-ls-org-ss \
| while read ss; do
echo "\n## ${ss}\n"
aws cloudformation describe-stack-set --output text \
--stack-set-name "${ss}" --query "StackSet.OrganizationalUnitIds" \
--call-as DELEGATED_ADMIN \
| tr "\t" "\n" \
| while read ou_id; do
echo "${all_ou_paths}" | grep "${ou_id}" --color=never
done
done
};f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment