Sub topic 3
Organizing accounts into OUs
This chapter assumes you have read the previous chapter on the organization, the management account and the root. It also assumes you can run AWS CLI commands against a management account and read IAM-style JSON policies. By the end you should be able to build and change an OU tree, and predict what each change does to the accounts inside it.
What an OU is
An organizational unit (OU) is a container inside an organization. It holds member accounts and other OUs. An OU has no permissions, resources or billing of its own. It exists to group accounts so that you can attach policies to the group instead of to each account.
The organization is a tree with three kinds of nodes:
- The root is the single top-level parent. It is created with the organization and cannot be deleted or renamed. Its ID looks like
r-examplerootid111. - OUs are interior nodes. Their IDs look like
ou-examplerootid111-exampleouid111, and the first part repeats the ID of the root they belong to. - Accounts are leaves. An account cannot contain anything.
The rules of the hierarchy
These constraints decide what trees you can build.
- Every account has exactly one parent, either the root or one OU. An account cannot be in two OUs.
- An OU also has exactly one parent. The structure is a tree, not a graph.
- OUs can nest up to five levels deep below the root.
- The management account always stays in the root. You cannot move it into an OU.
- OU names must be unique among siblings. Two OUs with the same name under different parents are allowed.
- An OU can be deleted only when it is empty. It must have no accounts and no child OUs.
Why the tree matters: inheritance
A policy attached to the root applies to every member account. A policy attached to an OU applies to every account in that OU and in all of its descendants. A policy attached directly to an account applies only to that account.
In the diagram above, a policy attached to Workloads OU affects prod-app1 and dev-app1. It does not affect log-archive or sandbox-alice. A policy attached to Prod OU affects only prod-app1.
This is the main design pressure on your OU tree. Put accounts that need the same guardrails under the same parent, and attach the policy once.
Building the tree with the CLI
Start by finding the root ID. Every later command needs it.
aws organizations list-roots \
--query 'Roots[0].Id' --output textCreate a top-level OU under the root, then a nested OU under that one.
aws organizations create-organizational-unit \
--parent-id r-examplerootid111 \
--name Workloads
aws organizations create-organizational-unit \
--parent-id ou-examplerootid111-exampleouid111 \
--name ProdEach call returns the new OU's ID. Save it, because the next command that places something under that OU needs it as --parent-id.
Move an account into an OU. You must give the account's current parent as well as the new one, which makes it harder to move the wrong account by accident.
aws organizations move-account \
--account-id 111122223333 \
--source-parent-id r-examplerootid111 \
--destination-parent-id ou-examplerootid111-exampleouid111Reading the tree
The API has no single call that returns the whole tree. You walk it one parent at a time, using these commands.
| Command | What it returns |
|---|---|
| `list-roots` | The root and its ID |
| `list-organizational-units-for-parent` | The child OUs of a root or OU |
| `list-accounts-for-parent` | The accounts directly under a root or OU |
| `list-parents` | The single parent of an account or OU |
| `describe-organizational-unit` | The name, ARN and ID of one OU |
Both listing commands return only direct children. To see everything under Workloads, call them again for each child OU. This script lists the OUs and accounts one level below a parent.
PARENT=ou-examplerootid111-exampleouid111
echo "Child OUs:"
aws organizations list-organizational-units-for-parent \
--parent-id "$PARENT" \
--query 'OrganizationalUnits[].[Id,Name]' --output text
echo "Accounts:"
aws organizations list-accounts-for-parent \
--parent-id "$PARENT" \
--query 'Accounts[].[Id,Name,Status]' --output textTo find where one account sits, run aws organizations list-parents --child-id 111122223333. The result tells you whether the parent is the root or an OU.
Declaring OUs in Terraform
If you manage the organization as code, declare OUs as resources. Nested OUs refer to their parent's id, and Terraform works out the creation order.
resource "aws_organizations_organizational_unit" "workloads" {
name = "Workloads"
parent_id = aws_organizations_organization.org.roots[0].id
}
resource "aws_organizations_organizational_unit" "prod" {
name = "Prod"
parent_id = aws_organizations_organizational_unit.workloads.id
}This assumes aws_organizations_organization.org is declared as in the earlier chapter. When you move an account by changing its parent_id in Terraform, the provider calls MoveAccount. It does not recreate the account.
What happens when you move an account
A move takes effect immediately and has two effects.
- The account stops inheriting the policies of its old ancestors and starts inheriting those of the new ones.
- Any policy attached directly to the account stays attached.
The second point surprises people. If you move an account from Dev OU to Prod OU, a direct attachment follows the account, but everything inherited changes. A stricter new parent can cut off permissions that running workloads depend on, and the effect is visible in the next API call the workload makes. Before moving an account into a stricter OU, review the policies on every ancestor of the destination. Move a test account first when you can.
Common errors
These errors come from the rules above.
| Error | Cause | Fix |
|---|---|---|
| `DuplicateOrganizationalUnitException` | A sibling OU already has that name | Choose a different name, or reuse the existing OU |
| `OrganizationalUnitNotEmptyException` | You tried to delete an OU that still has accounts or child OUs | Move the accounts out and delete child OUs first |
| `ParentNotFoundException` | The parent ID is wrong, or it belongs to a different organization | Check the ID with `list-roots` or `list-organizational-units-for-parent` |
| `SourceParentNotFoundException` | The `--source-parent-id` in `move-account` is not the account's current parent | Run `list-parents` and use its result |
| `ConstraintViolationException` | The tree would be deeper than five levels, or a quota was reached | Flatten the design or request a quota increase |
| `AccessDeniedException` | The caller is not in the management account, or lacks `organizations:*` permissions | Run the command with management account credentials, or from a delegated administrator where one is allowed |
Practical habits
- Name OUs by function, not by team or date. Teams are reorganized often, and renaming an OU is simple but moving many accounts is not.
- Keep the tree shallow. Two or three levels cover most organizations, and every extra level makes inherited policies harder to reason about.
- Tag OUs with
aws organizations tag-resource. You can then record ownership or cost center on the OU itself. - Do not put member workloads in the root. Accounts directly under the root inherit only root-level policies, and they are easy to forget when you attach policies to OUs.
- Treat tree changes as code changes. Make them through Terraform or a reviewed pipeline, so that the tree has a history.
The next chapter takes up how to decide which OUs to create so that the tree stays stable as the organization grows.
Exercise
Your organization has Workloads OU, which contains Prod OU and Dev OU. A policy that denies organizations:LeaveOrganization is attached to Workloads OU, and a second policy is attached directly to the account dev-app1 in Dev OU. You move dev-app1 into Prod OU. Which of the two policies still applies to dev-app1, and why? Then say what you must do before you can delete Dev OU.
Show answer
Both still apply. The policy on Workloads OU applies because Prod OU is also a descendant of Workloads OU, so dev-app1 inherits it from the new path. The policy attached directly to the account stays attached when the account moves. To delete Dev OU you must first make it empty: move every remaining account out of it and delete any child OUs. Otherwise the call fails with OrganizationalUnitNotEmptyException.