Sub topic 7
Writing SCPs: Regions, organization membership, and blocked actions
This chapter assumes you know the following:
- IAM policy JSON (
Effect,Action,NotAction,Resource,Condition). - That SCPs are attached to the root, OUs, or accounts, and that they cap permissions without granting any.
- The deny-list strategy: the default
FullAWSAccesspolicy stays attached, and you addDenystatements on top of it.
Here you write three deny-list SCPs, create them with the AWS CLI, and attach them to a test OU.
The shape of a deny-list SCP
SCPs use the IAM policy grammar with a smaller feature set. For a deny-list SCP you need to know three things:
- Every statement is normally
"Effect": "Deny". TheAllowthat makes a request possible comes fromFullAWSAccessand from IAM policies in the account. "Resource": "*"is the usual value. SCPs support resource ARNs inDenystatements, but most guardrails are easier to reason about at the action and condition level.- The
Principalelement is not used. An SCP applies to every IAM user and role in the member accounts it covers, including the account root user.
An SCP never gives anyone access. If you attach only the three policies in this chapter, nobody gains a permission they lacked before.
Before you write anything
Confirm that the SCP policy type is enabled on the root. If it isn't, attach-policy fails.
aws organizations list-roots
aws organizations enable-policy-type \
--root-id r-examplerootid \
--policy-type SERVICE_CONTROL_POLICYReplace r-examplerootid with the ID returned by list-roots. If the type is already enabled, enable-policy-type returns an error saying so, which is harmless.
Protecting organization membership
The first SCP stops member-account principals from detaching their account from the organization. An account that leaves also leaves every guardrail above it, so this is usually the first SCP an organization deploys.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "DenyLeavingOrganization",
"Effect": "Deny",
"Action": "organizations:LeaveOrganization",
"Resource": "*"
}
]
}Two points about this policy:
- It is a single-action denial with no condition. Nothing in the member account can override it, because an explicit deny always wins over an allow in IAM policies.
- It does not affect the management account. SCPs never restrict the management account's principals. The management account cannot call
LeaveOrganizationanyway, and it removes member accounts by callingRemoveAccountFromOrganization, which this SCP does not block.
Restricting Regions
The second SCP denies any request to a Region outside an approved list. The condition key is aws:RequestedRegion, which holds the Region the request is sent to.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "DenyOutsideApprovedRegions",
"Effect": "Deny",
"NotAction": [
"iam:*",
"organizations:*",
"route53:*",
"cloudfront:*",
"support:*",
"sts:*"
],
"Resource": "*",
"Condition": {
"StringNotEquals": {
"aws:RequestedRegion": ["eu-west-1", "eu-central-1"]
}
}
}
]
}Read the statement from the outside in:
DenywithNotActionapplies to every action except the ones listed.- The
Conditionnarrows that to requests whose Region is noteu-west-1oreu-central-1. - The result: any action outside the exempt list, sent to any other Region, is denied.
Why the NotAction list exists
Some services are global. Their API endpoints are not tied to a Region you choose, and requests to them are typically evaluated as if they were sent to us-east-1. Without the exemption, this SCP would deny IAM and Route 53 calls, because us-east-1 is not on the approved list. The list in this chapter is a starting point. Many organizations also exempt services such as budgets, ce, globalaccelerator, waf, and health. Build your own list by testing in a sandbox OU.
How the evaluation looks
Note that "deny does not apply" is not the same as "allowed". After the SCP step, IAM still needs an Allow.
Side effects to expect
- A user whose CLI profile defaults to
us-east-1getsAccessDeniedonec2:DescribeInstancesuntil they pass--region eu-west-1. - Services that run in
us-east-1on your behalf (some global features of CloudFront, ACM certificates for CloudFront, and similar) can fail if their actions aren't exempt. - Calls to the global STS endpoint appear as
us-east-1. That is whysts:*is in the list. Regional STS endpoints are also fine.
Blocking specific actions, with an exception
The third pattern denies named actions and exempts a trusted role. This is how you protect audit infrastructure such as CloudTrail.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ProtectCloudTrail",
"Effect": "Deny",
"Action": [
"cloudtrail:StopLogging",
"cloudtrail:DeleteTrail",
"cloudtrail:UpdateTrail"
],
"Resource": "*",
"Condition": {
"ArnNotLike": {
"aws:PrincipalArn": "arn:aws:iam::*:role/SecurityAdmin"
}
}
}
]
}The Deny applies to everyone except principals whose ARN matches role/SecurityAdmin in any account. ArnNotLike is the right operator here because the denial triggers when the principal does not match.
Creating and attaching the policies
Save each JSON document to a file, create the policy, then attach it to a target. The target ID can be a root, OU, or account. Start with a test OU.
aws organizations create-policy \
--name DenyLeavingOrganization \
--description "Prevent member accounts from leaving the organization" \
--type SERVICE_CONTROL_POLICY \
--content file://deny-leave-org.json
aws organizations attach-policy \
--policy-id p-examplepolicyid \
--target-id ou-exam-ple12345create-policy returns the real policy ID. Use that value in attach-policy. To update a policy later, run update-policy with --content. The change takes effect for every target the policy is attached to.
Limits and authoring habits
- An SCP document is limited to 5,120 characters. The Region policy with a long exemption list gets close to this, so keep one concern per policy.
- Each root, OU, or account can have only five SCPs attached directly, and
FullAWSAccesscounts toward that number. Inherited policies from parent OUs don't count against the child's limit. - Give every statement a
Sidthat states its purpose. When a denial shows up in a CloudTrail event or an error message, theSidis the quickest way to find the responsible statement. - Use
NotActioncarefully. ADenywithNotActionblocks every action not listed, including ones AWS adds in the future. This is what you want for a Region guardrail, but it is a poor fit for other controls.
Common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| Region SCP without global-service exemptions | IAM, Route 53, or STS calls fail | Add them to `NotAction` |
| Using `StringEquals` where `StringNotEquals` is needed | Denies the approved Regions instead of the others | Re-read the condition as "deny when this is true" |
| Attaching to the root first | Every member account is affected at once | Attach to a test OU, then widen |
| Removing `FullAWSAccess` while using deny statements | All requests are implicitly denied | Keep it attached for a deny-list strategy |
| Exempting a role by name only | Anyone can create a same-named role | Restrict creation of that role name |
Exercise
A Region SCP uses "Effect": "Deny" with a "NotAction" list of iam:, sts: and organizations:*, and a StringNotEquals condition on aws:RequestedRegion with the value eu-west-1. A user in a member account in the affected OU calls s3:ListBucket on a bucket in eu-west-1, and later calls ec2:DescribeInstances in us-east-1. Does the SCP deny each call, and what happens to the first call if the user has no S3 permission in their IAM policy?
Show answer
The s3:ListBucket call is not denied by the SCP, because its Region (eu-west-1) is in the approved list, so the condition is false. The ec2:DescribeInstances call is denied by the SCP, because ec2 is not in the NotAction list and us-east-1 is not approved. The first call is still not automatically allowed. The SCP only declines to deny it. With no S3 permission in the user's IAM policy, the call fails with an implicit deny.
Exercise
Why does the DenyLeavingOrganization SCP not need a Condition, and why does it have no effect on the management account?
Show answer
The action organizations:LeaveOrganization should be blocked for every principal in member accounts, so no exception is required. Because an explicit deny overrides any allow, nothing in the account can undo it. SCPs never apply to principals in the management account, so the policy cannot restrict it. The management account also cannot call LeaveOrganization.