Sub topic 12
Assigning across many accounts and OUs
This chapter assumes you know what a permission set, a group, and an account assignment are. It also assumes you can run AWS CLI commands against the management account or a delegated administrator account.
Assignments target accounts, not OUs
An account assignment links one principal, one permission set, and one account. IAM Identity Center has no "assign to this OU" operation. The TargetType field accepts only AWS_ACCOUNT. To cover an OU, you assign to each account in it.
This affects your design in three ways:
- A new account in an OU gets no access until you create assignments for it.
- Moving an account to another OU does not move its assignments.
- Removing a group from an OU means deleting each assignment individually.
Looping over an OU
The script below finds the group by display name and creates one assignment per active account directly under an OU. It lists only direct children, so accounts in nested OUs are skipped.
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)
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"
donePS_ARN is the ARN of the permission set from the earlier chapter.
Asynchronous requests
create-account-assignment returns right away with a request ID and a status of IN_PROGRESS. The role is created in the target account afterward. If you script many assignments, check each result with describe-account-assignment-creation-status, because a failed request does not stop your loop.
aws sso-admin list-account-assignment-creation-status \
--instance-arn "$INSTANCE_ARN" \
--filter Status=FAILEDCalls that create or delete assignments are rate limited. For hundreds of accounts, add a short delay or a retry with backoff.
Making the loop repeatable
Running the loop twice is a common situation. Treat it as safe by design:
- Run it on a schedule, or on the Organizations
CreateAccountandMoveAccountevents, so new accounts receive their assignments. - List existing assignments with
list-account-assignmentsand skip accounts that already have one. - Keep the OU, group, and permission set pairs in a version-controlled file, and have the script read from it.
- Recurse with
list-organizational-units-for-parentif your OUs are nested.
Choosing an approach
| Approach | Best for | Weakness |
|---|---|---|
| One-off CLI loop | Initial rollout | No drift detection |
| Event-driven script | New accounts | You maintain the code |
| CloudFormation or other IaC | Auditable, reviewed changes | One resource per assignment |
Exercise
A new account is moved into an OU where the Auditors group already has access to every other account. Does the group get access to the new account automatically? Why or why not?
Show answer
No. Assignments are made per account, not per OU, so the new account has no assignment until one is created, for example by rerunning the loop or by an event-driven script.