Skip to content

Instantly share code, notes, and snippets.

@chuckn246
Last active February 5, 2023 14:04
Show Gist options
  • Select an option

  • Save chuckn246/8ad41bfa08a2018d183361755ccf3b71 to your computer and use it in GitHub Desktop.

Select an option

Save chuckn246/8ad41bfa08a2018d183361755ccf3b71 to your computer and use it in GitHub Desktop.
Configure aws-cli and sso
#!/bin/sh
# Description: Configure aws-cli accounts to use SSO
# Author: Chuck Nemeth
# https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html
# https://ben11kehoe.medium.com/you-only-need-to-call-aws-sso-login-once-for-all-your-profiles-41a334e1b37e
# Can fetch ACCOUNT_IDs by running (if aws-cli is already working):
# aws --profile admin organizations list-accounts --query 'Accounts[].[Id,Name]'
# Otherwise, just grab them from the AWS Management Console
sso_url="https://d-SUBDOMAIN.awsapps.com/start"
sso_region="us-east-1"
admin_account="MAIN_ACCOUNT_ID"
admin_iam_role="AdministratorAccess"
admin_region="us-east-1"
dev_account="DEV_ACCOUNT_ID"
dev_iam_role="DeveloperAccess"
dev_region="us-east-1"
prod_account="PROD_ACCOUNT_ID"
prod_iam_role="ProdAccess"
prod_region="us-east-1"
# Directory check
if [ ! -d "$HOME/.aws" ]; then
mkdir -p "$HOME/.aws"
fi
# Create config file
cat << EOF > "$HOME/.aws/config"
[profile login]
sso_start_url = ${sso_url}
sso_region = ${sso_region}
[profile admin]
sso_start_url = ${sso_url}
sso_region = ${sso_region}
sso_account_id = ${admin_account}
sso_role_name = ${admin_iam_role}
region = ${admin_region}
output = json
[profile example-dev]
sso_start_url = ${sso_url}
sso_region = ${sso_region}
sso_account_id = ${dev_account}
sso_role_name = ${dev_iam_role}
region = ${dev_region}
output = json
[profile example-prod]
sso_start_url = ${sso_url}
sso_region = ${sso_region}
sso_account_id = ${prod_account}
sso_role_name = ${prod_iam_role}
region = ${prod_region}
output = json
EOF
# vim: ft=sh ts=2 sts=2 sw=2 sr et
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment