Sub topic 7
Writing SCPs: Regions, organization membership, and blocked actions
This chapter assumes you can read IAM policy JSON (Effect, Action, Resource, Condition). It also assumes the SCP policy type is enabled on your root and that you know how policies inherit down the organization tree. The examples use the deny-list strategy. The default FullAWSAccess SCP stays attached, and each new SCP removes one specific capability.
Anatomy of a deny-list SCP
An SCP uses the same JSON grammar as an IAM policy, with a few differences. SCPs do not support a Principal element. They apply to every IAM user and role in the affected accounts. A Deny statement therefore needs only Action, Resource, and optionally Condition. An SCP never grants access. It only limits what identity-based and resource-based policies can allow.
The management account is not restricted by SCPs. Attach and test your policies on member accounts.
Protecting organization membership
The simplest useful SCP blocks a single action. This one stops member-account principals from removing their account from the organization:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "DenyLeavingOrganization",
"Effect": "Deny",
"Action": "organizations:LeaveOrganization",
"Resource": "*"
}
]
}Save it as deny-leave-org.json. You can then add other single-action denials to the same policy. Examples are disabling logging or deleting a security baseline role.
Restricting Regions
Restricting Regions uses the global condition key aws:RequestedRegion. The policy denies a request when the requested Region is not on your approved list.
{
"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"]
}
}
}
]
}NotAction matches every action except those listed. Combined with Deny, the statement reads: deny everything except these global services when the Region is not eu-west-1 or eu-central-1. Global services such as IAM and Route 53 are not tied to a Region. Without the exemption they would fail, because their requests are served from a single Region, often us-east-1.
Creating and attaching the policy
Create the policy from the JSON file, then attach it by target ID. Use a test OU first:
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-ple12345The create-policy output returns the policy ID. Pass it to attach-policy. The target can be the root, an OU, or an account.
Common mistakes
- Putting
Allowstatements in a deny-list SCP and expecting them to grant access. They only widen the ceiling. - Detaching
FullAWSAccesswhile using a deny-list strategy. Every action then becomes implicitly denied. - Forgetting that the Region condition affects service-linked and global calls. Test with a real role in a member account.
- Attaching to the root first. Every account inherits the change at once.
Exercise
A Region-restriction SCP uses "Effect": "Deny" with "Action": "*" and the aws:RequestedRegion condition, with no exemptions. Users report they can no longer manage IAM roles in an approved Region. What is wrong, and how do you fix it?
Show answer
IAM is a global service whose requests are served from a single Region (us-east-1), so aws:RequestedRegion does not match an approved Region and the Deny applies. Replace "Action": "" with "NotAction" listing the global services (iam:, organizations:, sts:, route53:, cloudfront:, support:*), so those calls are exempt from the Region check.