Sub topic 12
Assigning across many accounts and OUs
This chapter assumes you know IAM roles and policies, the AWS Organizations hierarchy (management account, member accounts, OUs), and how to run AWS CLI commands. It also assumes you have an IAM Identity Center organization instance with at least one group and one permission set. Earlier chapters covered how an account assignment ties a principal, a permission set, and an account together. This chapter covers what changes when you have dozens or hundreds of accounts.
The core constraint: assignments target accounts, not OUs
An account assignment in IAM Identity Center has exactly one target, and that target is a single AWS account. The API has no "assign to this OU" option. Console workflows that let you select many accounts are only a convenience. Each selected account becomes its own assignment.
This has three practical consequences:
- Moving an account into an OU does not grant or revoke any access. The assignment follows the account, not its position in the tree.
- A new account has no assignments until you create them. Something must run after account creation, or new accounts stay inaccessible.
- The number of assignments is groups × permission sets × accounts, in the worst case. The count is what you must keep under control.
Enumerating accounts
Pick the way you enumerate accounts deliberately, because it decides who gets access.
| Source | Command | Notes |
|---|---|---|
| Direct children of an OU | `aws organizations list-accounts-for-parent` | Does not descend into child OUs. |
| Every account | `aws organizations list-accounts` | Includes suspended accounts, so filter on `Status`. |
| Whole subtree | Recursive walk with `list-organizational-units-for-parent` | You write the recursion yourself. |
list-accounts-for-parent returns only accounts that sit directly under the parent ID you give it. If your OU has nested OUs, the accounts in them are silently skipped. This is a common source of "the auditors can't see the sandbox accounts" tickets.
Assigning a permission set to every account in an OU
The following script uses an existing group and permission set. It looks up the group by display name, then creates one assignment per active account directly under the OU.
INSTANCE_ARN=$(aws sso-admin list-instances --query 'Instances[0].InstanceArn' --output text)
IDENTITY_STORE_ID=$(aws sso-admin list-instances --query 'Instances[0].IdentityStoreId' --output text)
PS_ARN=$(aws sso-admin list-permission-sets \
--instance-arn "$INSTANCE_ARN" \
--query 'PermissionSets[]' --output text | tr '\t' '\n' | while read -r ARN; do
NAME=$(aws sso-admin describe-permission-set \
--instance-arn "$INSTANCE_ARN" --permission-set-arn "$ARN" \
--query 'PermissionSet.Name' --output text)
[ "$NAME" = "ReadOnlyAuditor" ] && echo "$ARN"
done)
GROUP_ID=$(aws identitystore get-group-id \
--identity-store-id "$IDENTITY_STORE_ID" \
--alternate-identifier '{"UniqueAttribute":{"AttributePath":"DisplayName","AttributeValue":"Auditors"}}' \
--query 'GroupId' --output text)
for ACCOUNT_ID in $(aws organizations list-accounts-for-parent \
--parent-id ou-xxxx-xxxxxxxx \
--query 'Accounts[?Status==`ACTIVE`].Id' --output text); do
aws sso-admin create-account-assignment \
--instance-arn "$INSTANCE_ARN" \
--target-id "$ACCOUNT_ID" \
--target-type AWS_ACCOUNT \
--permission-set-arn "$PS_ARN" \
--principal-type GROUP \
--principal-id "$GROUP_ID"
doneReplace ou-xxxx-xxxxxxxx with your OU ID. The first block resolves the permission set ARN by name, because the API identifies permission sets by ARN and offers no lookup by name.
Assignments are asynchronous
create-account-assignment does not finish the work before it returns. It returns an AccountAssignmentCreationStatus object with a request ID and a status of IN_PROGRESS. IAM Identity Center then creates the role in the target account in the background.
You can check progress with describe-account-assignment-creation-status, passing the request ID. The possible states are IN_PROGRESS, FAILED, and SUCCEEDED. A failed request includes a reason.
Two limits matter when you loop over many accounts:
- Identity Center throttles write calls. A loop over hundreds of accounts can hit
ThrottlingException, so add retries with backoff. The AWS CLI retries some throttling errors itself, but a long loop should still check for failures. - Only a limited number of assignment requests can be in progress at once for an instance, and Identity Center may reject new ones with a
ConflictException. Sequential calls with a short pause avoid most of these errors.
Creating the same assignment twice is not harmful. Identity Center treats a repeat as already satisfied, which makes scripts safe to re-run after a partial failure.
Auditing what is already assigned
Before you change anything, list the current state. Two calls cover most needs:
aws sso-admin list-account-assignmentslists the principals and permission sets assigned to one account. It requires--account-idand--permission-set-arn.aws sso-admin list-account-assignments-for-principallists every account and permission set assigned to one user or group. It is available on the same instance, and it is the better starting point for "what can this group reach?"
For the reverse view, "which accounts use this permission set?", use list-accounts-for-provisioned-permission-set.
Keeping the number of assignments low
Each design choice below reduces assignment count, and a lower count means a smaller audit surface.
- Assign groups, never users. One group assignment covers every member, including future members.
- Use few, broad permission sets. Ten well-named permission sets assigned to many accounts are easier to reason about than a permission set per account.
- Split by account purpose, not by team. Put accounts of the same type (production, sandbox, logging) under the same OU, then assign per type.
- Prefer fewer account-wide admin assignments. Give broad write access to a small number of accounts and read-only access elsewhere.
Handling new and moved accounts
Because assignments belong to accounts, automation must cover account creation. A common pattern is an EventBridge rule on the Organizations CreateAccountResult event, or on MoveAccount, that starts a Lambda function. The function reads the account's parent OU, looks up an OU-to-assignment mapping you maintain, and creates the assignments.
Keep this mapping as data, for example a table of OU ID, group ID, and permission set ARN. The function then contains no policy decisions.
Moving an account out of an OU does not remove its old assignments. If your design says "OU membership implies access", your automation must also delete assignments that no longer match. Use delete-account-assignment for that, which is asynchronous in the same way as creation.
Removing assignments
Deleting an assignment removes the principal's access through that permission set. If no other assignment in the account uses the permission set, Identity Center also removes the corresponding role from the account. Active sessions are not cut off immediately. They remain valid until they expire, which is why session duration (covered in an earlier chapter) is also a revocation delay.
Failure modes to expect
| Symptom | Likely cause |
|---|---|
| Loop finishes, but some accounts are missing | Accounts were in a child OU, or were `SUSPENDED` and filtered out. |
| `ThrottlingException` partway through | Too many write calls too fast. Add backoff and re-run. |
| `ConflictException` on create | Another request for the same instance is still in progress. Retry later. |
| Status `FAILED` with an access error | The caller lacks permission in the management or delegated administrator account. |
| New account has no access | No automation reacted to account creation. |
Exercise
A script assigns the Auditors group to every account returned by list-accounts-for-parent for the OU Workloads. Auditors report that they cannot sign in to accounts in Workloads/Sandbox, a child OU. Why, and how do you fix it?
Show answer
list-accounts-for-parent returns only accounts directly under the given parent, so accounts in the child OU Sandbox were never enumerated and received no assignments. Run the same loop with the ID of the Sandbox OU, or write a recursive walk with list-organizational-units-for-parent so that every nested OU is covered. Then check progress with describe-account-assignment-creation-status.
Exercise
You move an account from the Production OU to the Sandbox OU. Does the Auditors group's access change, and what must you do to make access match the new OU?
Show answer
Access does not change, because assignments target the account and not the OU. The old assignments stay in place. Create the assignments that the Sandbox mapping requires, and delete the ones that only applied to Production with delete-account-assignment.