Skip to content

Instantly share code, notes, and snippets.

@varunchandak
Last active March 15, 2024 05:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save varunchandak/a19a15c69e65b49ed7874ae49d5a3743 to your computer and use it in GitHub Desktop.
Save varunchandak/a19a15c69e65b49ed7874ae49d5a3743 to your computer and use it in GitHub Desktop.
aws-org-reinvite-accounts-to-org

AWS Organization Handshake Management Script

This script is designed to manage AWS Organization handshakes by filtering for open invitations and then canceling and re-inviting the accounts. It automates the process of handling account invitations within an AWS Organization, ensuring that only current and relevant invitations are active.

Features

  • Filter Open Invitations: The script filters for handshakes that are in an 'OPEN' state and of the 'INVITE' action type, indicating pending invitations.
  • Cancel and Re-invite: For each open invitation, the script cancels the existing handshake and immediately sends a new invitation to the account.

How It Works

  1. Set Internal Field Separator (IFS): The script begins by setting the IFS to a comma, which is used to parse the output of AWS CLI commands.
  2. List Open Handshakes: It uses the aws organizations list-handshakes-for-organization command to list all handshakes associated with the organization.
  3. Filter with jq: Utilizes jq to filter out handshakes that are open and are invitations.
  4. Process Each Handshake: For each filtered handshake, the script does the following:
    • Prints the handshake ID and account ID to the console.
    • Cancels the existing handshake using the aws organizations cancel-handshake command.
    • Sends a new invitation to the account using the aws organizations invite-account-to-organization command.

Requirements

  • AWS CLI: The script requires the AWS Command Line Interface (CLI) to be installed and configured with appropriate credentials.
  • jq: This script uses jq for parsing and filtering JSON output from AWS CLI commands.

Usage

To use the script, simply run it from your terminal. Ensure that you have the necessary permissions to list, cancel, and send invitations within your AWS Organization.

bash aws-org-reinvite-accounts-to-org.sh
#!/bin/bash
IFS=','
aws organizations list-handshakes-for-organization \
| jq -r '.Handshakes[]|select(.State == "OPEN" and .Action == "INVITE")|[.Id, (.Parties[]|select(.Type == "ACCOUNT").Id)]|@csv' \
| tr -d '"' \
| while read HANDSHAKE_ID ACCOUNT_ID; do
echo "Handshake ID: $HANDSHAKE_ID and Account ID: $ACCOUNT_ID"
aws organizations cancel-handshake --handshake-id "$HANDSHAKE_ID" | jq '.'
aws organizations invite-account-to-organization --target Id="$ACCOUNT_ID,Type=ACCOUNT" | jq '.'
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment