Operational Excellence and Automation · Topic 1 of 7

Infrastructure and Policy as Code

Choose CloudFormation or CDK, enforce rules with Hooks, aspects, Config rules and SCPs, keep secrets out of templates and deploy with StackSets.

  • 5 min
In a nutshell
  • CDK synthesizes to CloudFormation, so all stack features still apply.
  • Hooks and aspects are proactive, SCPs are preventive guardrails, and Config rules are detective with remediation.
  • SCPs never grant access and do not apply to the management account.

What it is

Infrastructure as code (IaC) describes accounts, networks and services in files that a tool deploys the same way every time. Policy as code describes the rules those files must follow, and checks them automatically instead of in a review meeting.

At professional level the questions are about scale. You must choose the right authoring style, block non-compliant resources before they exist, keep secrets out of source control and roll the same baseline into hundreds of accounts.

AuthorCloudFormation or CDKvalidatePolicy checksHooks, aspects, GuarddeployStackSetsmany accounts and RegionswatchConfig rulesdrift and remediation

How it works

Declarative and imperative IaC

AWS CloudFormation is declarative: a YAML or JSON template states the end result and the service works out the order. The AWS CDK is imperative in style: you write TypeScript, Python, Java, C# or Go, use loops and classes, and the CDK synthesizes a CloudFormation template.

ApproachStrengthChoose it when
CloudFormation templatesSimple, readable, no build stepSmall teams, standard patterns, Service Catalog products
AWS CDKReusable constructs, real code, unit testsDevelopers own infrastructure, many similar stacks
AWS SAMShort syntax for serverlessLambda, API Gateway and event sources

Both end up as CloudFormation stacks, so change sets, rollback, drift detection and StackSets apply to both. CDK L2 and L3 constructs bake in sensible defaults, which cuts the code a team must write and review.

Policy as code: prevent, enforce, detect

Strong designs layer three kinds of control, from earliest to latest.

ControlWhen it actsExample use
CDK aspectsAt synthesis, in the developer buildAdd tags to every construct, fail on unencrypted buckets (cdk-nag uses aspects)
CloudFormation HooksBefore CloudFormation creates, updates or deletes a resourceBlock a public S3 bucket in any stack in the account
SCPsAt every API call in member accountsDeny leaving approved Regions or disabling CloudTrail
AWS Config rulesAfter the change, continuouslyFlag drift and run automatic remediation

CloudFormation Hooks can run Guard rules or a Lambda function against resources, stacks or change sets. A hook in fail mode stops the operation, and warn mode only reports, which is a safe way to roll out a new rule. AWS Control Tower proactive controls are implemented as CloudFormation Hooks.

SCPs set the maximum permissions for member accounts. They never grant permissions, and they do not affect the management account, so keep workloads out of it.

AWS Config rules are mainly detective. A remediation action runs a Systems Manager Automation runbook, either manually or automatically, to fix a non-compliant resource. Deploy rules and conformance packs across the organization from a delegated administrator account.

CloudFormation drift detection reports resources changed outside the stack. It does not fix them, so pair it with Config remediation or a redeploy from the pipeline.

Secrets in infrastructure code

Never put a password in a template, a CDK app or a parameter default. Store it once and reference it.

StoreUse it forKey feature
Secrets ManagerDatabase credentials, API keysBuilt-in rotation with Lambda, cross-Region replication, resource policies
Parameter Store SecureStringConfiguration values and simple secretsKMS encryption, hierarchy, no built-in rotation

CloudFormation dynamic references such as {{resolve:secretsmanager:prod/db:SecretString:password}} and {{resolve:ssm-secure:...}} fetch the value at deploy time, so it never appears in the template. For Amazon RDS and Aurora, the ManageMasterUserPassword property lets RDS create and rotate the master password in Secrets Manager with no secret in code at all.

In the CDK, reference a secret with Secret.fromSecretNameV2 or generate one, and let the construct pass only the ARN to the database.

StackSets for many accounts and Regions

CloudFormation StackSets deploys one template as stack instances in many accounts and Regions.

Delegated adminStackSetservice managedTarget OUsOrganizationsauto deployNew accountbaseline stacks
  • Service-managed permissions use AWS Organizations, target OUs and support automatic deployment to accounts that join an OU later. They can be run from a delegated administrator account.
  • Self-managed permissions need an administration role and execution roles that you create, for accounts outside an organization.
  • Operation preferences set maximum concurrent accounts, failure tolerance and Region order, so a bad template stops early instead of hitting every account.

Service-managed StackSets do not deploy to the management account, even if it sits in a target OU.

How the exam asks about this

  • "Block any stack that creates an unencrypted EBS volume, in every account, before resources exist" -> a CloudFormation Hook (or Control Tower proactive control), not only a Config rule.
  • "New accounts must receive the logging and IAM baseline with no manual step" -> StackSets with service-managed permissions and automatic deployment to the OU.
  • "Developers want loops and unit tests for 40 similar microservice stacks" -> the AWS CDK with shared constructs and aspects.

What to remember for the exam

  • CDK synthesizes to CloudFormation, so all stack features still apply.
  • Hooks and aspects are proactive, SCPs are preventive guardrails, and Config rules are detective with remediation.
  • SCPs never grant access and do not apply to the management account.
  • Dynamic references keep secrets out of templates, and Secrets Manager adds rotation.
  • StackSets with service-managed permissions auto deploy to new accounts in target OUs.
  • Use failure tolerance and concurrency settings to limit the blast radius of a StackSet update.
Watch out

Drift detection only reports manual changes. If the question asks for drift to be corrected automatically, add Config remediation or a pipeline redeploy.

Common mistakes

Relying on Config rules alone for prevention. Config evaluates after the resource exists, so a public bucket is briefly live. Use a Hook or an SCP when the requirement says prevent.

Choosing Parameter Store when rotation is required. SecureString parameters do not rotate themselves. Secrets Manager is the answer for automatic rotation of credentials.

Running a script per account instead of StackSets. A loop of CLI calls misses new accounts and has no failure tolerance. StackSets with automatic deployment handles both.

Check yourself

  • Which control blocks a non-compliant resource before CloudFormation creates it, in every stack?

    A CloudFormation Hook in fail mode, deployed across the accounts. It evaluates the resource before the create or update runs.

  • How do you keep a database password out of a template and still rotate it?

    Store it in Secrets Manager with rotation enabled and use a dynamic reference, or use ManageMasterUserPassword for RDS.

  • What makes a StackSet reach accounts created next month without manual work?

    Service-managed permissions with automatic deployment enabled on the target OU.

Topic 1 of 7 in Operational Excellence and Automation

Test yourself on Operational Excellence and Automation

The full AWS SAP-C03 question bank has 850 questions, with timed practice exams and an explanation for every answer.

This study guide is independent and is not affiliated with, endorsed by, or sponsored by AWS. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.