Sub topic 13
Testing and rolling out guardrails safely
This chapter assumes you can write SCPs and RCPs, attach them with the AWS CLI, and read CloudTrail events. It covers how to put a new guardrail into an organization without breaking production workloads, and how to back it out when something goes wrong.
Why guardrails need a staged rollout
An SCP or RCP applies to every account under the target it is attached to, and a deny takes effect for all matching requests. A Region restriction attached to the root can stop a production deployment pipeline that uses an unlisted Region. An S3 RCP that denies non-organization principals can stop a partner's access to a bucket.
AWS Organizations has no log-only or audit mode for SCPs or RCPs. You cannot attach a policy and see what it would have denied. You have to get that evidence another way: by analyzing existing usage before you attach, by testing in an isolated account, and by widening the target gradually.
The rollout path
The usual path moves a policy through targets of increasing size. At each stage you check for unexpected denials before moving on. Detaching the policy is the rollback.
| Stage | Target | Purpose | Move on when |
|---|---|---|---|
| Analyze | None | Find who uses the service, action or Region you plan to block | You know the legitimate users and have a list of exceptions |
| Sandbox | An OU with one or two test accounts | Prove the policy syntax and the intended allows and denies | Every test case behaves as expected |
| Pilot | An OU of low-risk workloads | See real traffic hit the policy | A fixed observation period passes with no unexpected denials |
| Broad | Production OUs, one at a time | Apply the control where it matters | Each OU stays clean through its own observation period |
| Root | The organization root | Cover new accounts automatically | Every OU below it is already running the policy |
Attaching at the root last gives you a useful property. Any account you add later inherits the guardrail without extra work.
Step 1: Analyze existing usage
Before you deny something, find out who uses it. Two sources help.
Last-accessed data. IAM can generate a report of which services principals in an organization entity last used. Run this from the management account (or a delegated administrator that has the permission):
# Start a report for an OU, then fetch it with the returned job ID
aws iam generate-organizations-access-report \
--entity-path o-exampleorgid/r-examplerootid/ou-exam-ple12345
aws iam get-organizations-access-report \
--job-id examplejobid \
--sort-key SERVICE_NAMESPACE_ASCENDINGIf the report shows no principal in the OU has touched a service in the last year, a deny for that service is low risk. If it shows regular use, you need an exception or a conversation with the owner.
CloudTrail history. Each event records the Region (awsRegion) and the service (eventSource). For a Region restriction, group the last few months of events by awsRegion across your accounts, for example in CloudTrail Lake or Athena. Any Region outside your approved list that has recent activity is a workload your policy will break. Check it before you attach anything.
Step 2: Test in a sandbox OU
Create an OU that contains one test account with no production workloads. Attach the policy to the OU, not to the account, so the test matches how the policy will be attached later.
aws organizations create-policy \
--name DenyOutsideApprovedRegions \
--description "Region guardrail, sandbox test" \
--type SERVICE_CONTROL_POLICY \
--content file://deny-regions.json
aws organizations attach-policy \
--policy-id p-examplepolicyid \
--target-id ou-exam-ple12345
aws organizations list-policies-for-target \
--target-id ou-exam-ple12345 \
--filter SERVICE_CONTROL_POLICYThen test with real requests from a role in the test account. Write down each case before you run it: the action, the expected result, and why. A Region policy needs at least four cases:
- A regional action in an approved Region succeeds.
- A regional action in an unapproved Region is denied.
- A global-service action is allowed from any Region, because it is listed in
NotAction. - A principal with full IAM administrator rights is still denied in the unapproved Region, which proves the SCP and not the identity policy is the cause.
A script keeps these cases repeatable:
#!/usr/bin/env bash
# Run with credentials for a role in the sandbox account.
# Case 1: approved Region should succeed
aws ec2 describe-instances --region eu-west-1 > /dev/null \
&& echo "PASS approved Region allowed" \
|| echo "FAIL approved Region denied"
# Case 2: unapproved Region should be denied by the SCP
out=$(aws ec2 describe-instances --region us-east-1 2>&1)
if echo "$out" | grep -qi "service control policy"; then
echo "PASS unapproved Region denied by SCP"
else
echo "FAIL expected an SCP denial, got: $out"
fi
# Case 3: exempted global service works from an unapproved Region
aws sts get-caller-identity --region us-east-1 > /dev/null \
&& echo "PASS sts allowed from any Region" \
|| echo "FAIL sts denied"Check the wording of the error. A denial caused by an SCP says "explicit deny in a service control policy", and one caused by an RCP says "explicit deny in a resource control policy". If the message says only "no identity-based policy allows the action", the SCP is not what stopped the request and your test proves nothing about it. Not every service includes this detail in its error, so confirm in CloudTrail when the message is vague.
For RCPs, test from two sides. Use a principal inside the organization and a principal from outside it (a separate test account), and call the same bucket. The policy should block the outside caller and leave the inside caller alone.
Step 3: Pilot and watch for denials
Move the policy to a pilot OU, then search CloudTrail in those accounts for denials. Denied requests are recorded with an errorCode and an errorMessage. This filter pulls recent denials that mention an SCP:
aws cloudtrail lookup-events \
--lookup-attributes AttributeKey=ReadOnly,AttributeValue=false \
--max-results 50 \
--query 'Events[].CloudTrailEvent' --output json \
| jq -r '.[] | fromjson
| select(.errorMessage != null
and (.errorMessage | test("service control policy"; "i")))
| [.eventTime, .eventName, .userIdentity.arn, .awsRegion] | @tsv'lookup-events covers only recent management events and returns a limited number of results per call, so use it for spot checks. For a full observation period, query the same fields in CloudTrail Lake or in Athena over your trail bucket, and create an alarm on the error message so denials reach someone while the pilot runs. Remember that the event is logged in the account where the request was made, so an organization trail gives you one place to look.
Decide the observation period before you start. It should span your longest normal cycle, which for many teams is a month, because quarterly or month-end jobs rarely run during a one-week pilot.
Rolling back
Detaching is the rollback, and it is fast to run:
aws organizations detach-policy \
--policy-id p-examplepolicyid \
--target-id ou-exam-ple12345Two cautions apply to rollback and to ordinary changes.
- Changes are not guaranteed to take effect at the same instant everywhere. Allow a short delay before you re-run a test, and do not conclude that a detach failed after one immediate retry.
- Do not edit a policy that is already attached to the root as a way to test. Edits to an attached policy apply to every target using it at once. Create a new version of the policy under a new name, attach the new one to the sandbox OU, and swap targets when it passes.
Keep the previous version of a policy until the new one has been clean for a full observation period, so you can reattach it quickly.
Limits and mistakes that stall a rollout
- Attachment limits. Each root, OU, or account can have only a limited number of SCPs attached directly, and each policy has a maximum document size. Check the current quotas before you plan separate small policies per control. Combining many statements into one large policy runs into the size limit, and many tiny policies run into the attachment limit.
- The default allow. When you enable SCPs,
FullAWSAccessis attached to every target. If you detach it from a target without an allow-list SCP in its place, everything below that target is denied. The same applies toRCPFullAWSAccessfor RCPs. Keep these defaults in place when you are testing a deny list. - Untestable targets. The management account is not restricted by SCPs, so it cannot serve as a test account. Put your test principal in a member account.
- Cross-account blind spots. An RCP affects requests from principals in other accounts, including partners and AWS services acting on your behalf. A test with only in-organization callers will pass and still miss these. List third-party integrations before the pilot and add exceptions to the policy conditions.
- Changing several things at once. If you attach a new SCP and a new RCP together and see denials, you cannot tell which one caused them. Roll them out one after the other.
Exercises
Exercise
You are about to attach an SCP that denies all actions outside eu-west-1 and eu-central-1. List the checks you run before attaching it to the sandbox OU and the checks you run before attaching it to the root.
Show answer
Before the sandbox OU, review CloudTrail or CloudTrail Lake for activity by awsRegion across the accounts and note any workloads outside the two Regions, then confirm the global services in NotAction are listed. Test cases are written down in advance. Before the root, the policy has passed the sandbox test cases (approved Region allowed, unapproved Region denied with an SCP error message, global service allowed), has been clean in a pilot OU for a full observation period that covers monthly jobs, and has been attached to the other production OUs one at a time without unexpected denials. A rollback plan (detach the policy and keep the previous version) is documented.
Exercise
A test in the sandbox account is denied in us-east-1, but the error says only that no identity-based policy allows the action. Can you treat this as proof that the Region SCP works? What do you do next?
Show answer
No. That message points to a missing allow in the identity-based policy, not to the SCP. Give the test role administrator permissions so the identity policy cannot be the cause, rerun the request, and look for "explicit deny in a service control policy" in the error or in the CloudTrail event. Also confirm the policy is attached to the sandbox OU with list-policies-for-target and that the test account is a member account, not the management account.