Skip to content

Instantly share code, notes, and snippets.

@ddepaoli3
Last active June 27, 2023 07:36
Show Gist options
  • Select an option

  • Save ddepaoli3/b13432f00942bb397558ac01cf03d41b to your computer and use it in GitHub Desktop.

Select an option

Save ddepaoli3/b13432f00942bb397558ac01cf03d41b to your computer and use it in GitHub Desktop.
Generate AWS config file with all accounts in a AWS Organization
#!/bin/bash
SSO_START_URL=
SSO_ACCOUNT_ID=
SSO_ROLE_NAME=
cp ~/.aws/config ~/.aws/config.bak.`date +%F`
cat << EOF > ~/.aws/config
[profile root]
sso_start_url=$SSO_START_URL
sso_region=eu-west-1
sso_account_id=$SSO_ACCOUNT_ID
sso_role_name=$SSO_ROLE_NAME
region=eu-west-1
EOF
aws sso login --profile root
aws --profile root organizations list-accounts |jq '.Accounts[]|[.Id, .Name]|@csv'|sed s+'\\"'++g|while read line
do
accountId=`echo $line|cut -d ',' -f1|sed s+'"'++g`
name=`echo $line|cut -d ',' -f2|sed s+'"'++g|tr '[:upper:]' '[:lower:]'|sed s+' '+'-'+g`
cat << EOF >> ~/.aws/config
[profile $name]
sso_start_url=$SSO_START_URL
sso_region=eu-west-1
sso_account_id=$accountId
sso_role_name=$SSO_ROLE_NAME
region=eu-west-1
EOF
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment