Security Foundations and Governance · Topic 4 of 6

Secure Deployment with IaC

Deploy resources securely and consistently with CloudFormation, StackSets, Service Catalog, policy as code checks, least privilege pipelines and tags.

  • 5 min
In a nutshell
  • Use change sets to preview updates, and stack policies to protect critical resources.
  • CloudFormation should deploy through a service role with least privilege.
  • Retrieve secrets with dynamic references to Secrets Manager or Parameter Store, not template parameters.

What it is

Manual changes in the console are slow, hard to review and easy to get wrong. Infrastructure as code (IaC) describes resources in files that are reviewed, tested and deployed the same way every time, which is the foundation of secure and consistent deployment.

The security goals are that every resource starts in a compliant state, that deployments use only the permissions they need, and that bad changes are caught before they reach production.

Developercommits templatePipelinescan and testApprovalhuman or policyCloudFormationdeploys with a service roleAccountsconsistent resources

How it works

AWS CloudFormation essentials

CloudFormation creates resources from templates, grouped in stacks. These features support safe deployment.

FeatureSecurity value
Change setsPreview what an update will create, replace or delete before executing
Drift detectionFinds resources whose live settings differ from the template
Stack policyBlocks updates to chosen resources, such as a production database
Termination protectionPrevents accidental stack deletion
DeletionPolicy and UpdateReplacePolicyRetain or snapshot data resources when a stack or resource is removed
Service roleCloudFormation acts with a specific role, not the caller's permissions
Dynamic referencesPull values from Secrets Manager or Parameter Store at deploy time

Use dynamic references instead of putting secrets in parameters. A parameter with NoEcho is masked in the console but is still a value you passed, so a secret store is better for real secrets.

Resources:
  DataBucket:
    Type: AWS::S3::Bucket
    DeletionPolicy: Retain
    UpdateReplacePolicy: Retain
    Properties:
      VersioningConfiguration:
        Status: Enabled
      BucketEncryption:
        ServerSideEncryptionConfiguration:
          - ServerSideEncryptionByDefault:
              SSEAlgorithm: aws:kms
              KMSMasterKeyID: !Ref DataKey
            BucketKeyEnabled: true
      PublicAccessBlockConfiguration:
        BlockPublicAcls: true
        BlockPublicPolicy: true
        IgnorePublicAcls: true
        RestrictPublicBuckets: true

This template builds a bucket that is encrypted, versioned, kept on deletion and private (the DataKey resource is defined elsewhere in the template). Putting secure defaults into shared templates means every team inherits them.

Deploying across accounts with StackSets

CloudFormation StackSets deploys one template to many accounts and Regions. With service managed permissions and AWS Organizations, you target OUs, and StackSets can deploy automatically to accounts that join the OU later.

# AWS CLI, in the management or delegated administrator account
aws cloudformation create-stack-set --stack-set-name response-role \
  --template-body file://role.yaml \
  --permission-model SERVICE_MANAGED \
  --auto-deployment Enabled=true,RetainStacksOnAccountRemoval=false \
  --capabilities CAPABILITY_NAMED_IAM

aws cloudformation create-stack-instances --stack-set-name response-role \
  --deployment-targets OrganizationalUnitIds=ou-ab12-cdef3456 \
  --regions us-east-1

Use StackSets for baselines such as security roles, Config rules and logging settings. Self managed permissions need administration and execution roles in each account and suit cases outside an organization.

Checking templates before deployment

  • Linting and policy as code. Tools such as cfn-lint check syntax, and AWS CloudFormation Guard evaluates rules, for example "every bucket must have encryption".
  • CloudFormation Hooks. A Guard or Lambda hook runs during stack operations and can block a non compliant resource. Control Tower proactive controls use hooks.
  • Static analysis in the pipeline. Scan templates, code and container images before release.
  • AWS Config and Security Hub. Detect non compliance after deployment as a second line.

Service Catalog

AWS Service Catalog lets administrators publish approved products (CloudFormation or Terraform based) in portfolios that are shared with accounts or OUs. Users launch products without needing permissions to the underlying services, because a launch constraint runs the deployment with a specified role.

Choose Service Catalog when you want self service with guardrails: developers pick from a vetted list, the template is fixed, and parameters are limited.

Administratorbuilds productPortfolioshared to OUDeveloperlaunches productLaunch rolecreates resources

Least privilege pipelines

  • Give the pipeline a separate role per stage, and limit each to its target accounts.
  • Deploy to accounts through roles that the pipeline assumes, with short sessions.
  • Use OIDC federation or roles for external build tools, with no stored access keys.
  • Keep production deployments in separate accounts, behind a manual approval step.
  • Encrypt and restrict artifact buckets, and log all pipeline actions to CloudTrail.
  • Pin dependencies and verify image signatures before deployment.

Consistency and tagging

Immutable infrastructure means you replace resources instead of changing them in place, so drift cannot build up. Build AMIs with Image Builder, and deploy new instances from them. Enforce tags with tag policies, SCPs that require tags on creation, and Config rules that report missing tags.

Handling drift and emergencies

Drift detection finds manual changes. Decide whether to revert the resource to the template or update the template. Emergency console changes should be rare, logged and followed by a template update.

What to remember for the exam

  • Use change sets to preview updates, and stack policies to protect critical resources.
  • CloudFormation should deploy through a service role with least privilege.
  • Retrieve secrets with dynamic references to Secrets Manager or Parameter Store, not template parameters.
  • StackSets with service managed permissions deploy to OUs and to accounts added later.
  • CloudFormation Guard and Hooks apply policy as code before resources are created.
  • Service Catalog gives self service products, and a launch constraint lets users deploy without direct service permissions.
  • Drift detection finds changes made outside CloudFormation.
  • Pipelines need per stage roles, short lived credentials and approvals before production.
Exam tip

When a question says "developers must create only approved resource types without having permissions for those services", choose Service Catalog with a launch constraint.

Common mistakes

Giving the deployer administrator access. A template then can create anything, so use a scoped service role.

Relying on NoEcho for secrets. It hides values in the console but is not a secret store.

Forgetting DeletionPolicy on data. Deleting the stack can delete the database or bucket.

Fixing production by hand. The next stack update may overwrite the change or fail on drift.

Watch out

Removing a stack instance or deleting a stack can delete resources, including roles that other systems depend on, so check retention settings first.

Check yourself

  • How do you deploy a security baseline to every current and future account in an OU?

    Use a StackSet with service managed permissions and automatic deployment targeting the OU.

  • What does a Service Catalog launch constraint do?

    It makes Service Catalog deploy the product with a defined role, so users do not need permissions for the underlying services.

  • How do you preview what a CloudFormation update will change?

    Create a change set and review the planned additions, replacements and deletions.

Topic 4 of 6 in Security Foundations and Governance

Test yourself on Security Foundations and Governance

The full AWS SCS-C03 question bank has 858 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.