Copyright (c) 2026 MindMesh Academy. All rights reserved. This content is proprietary and may not be reproduced or distributed without permission.

2.2.1.4. Composing & Deploying IaC Templates (AWS SAM, AWS CloudFormation, AWS CDK)

2.2.1.4. Composing & Deploying IaC Templates (AWS SAM, AWS CloudFormation)

Templates are the source code of your infrastructure. Writing maintainable templates requires the same discipline as writing application code — modularity, parameterization, and version control.

CloudFormation template structure:
AWSTemplateFormatVersion: '2010-09-09'
Parameters:
  Environment:
    Type: String
    AllowedValues: [dev, staging, prod]
Conditions:
  IsProd: !Equals [!Ref Environment, prod]
Resources:
  MyBucket:
    Type: AWS::S3::Bucket
    DeletionPolicy: !If [IsProd, Retain, Delete]
    Properties:
      BucketEncryption:
        ServerSideEncryptionConfiguration:
          - ServerSideEncryptionByDefault:
              SSEAlgorithm: aws:kms
Outputs:
  BucketArn:
    Value: !GetAtt MyBucket.Arn
    Export:
      Name: !Sub "${Environment}-BucketArn"
Key CloudFormation features for the exam:
  • Intrinsic functions: !Ref, !Sub, !GetAtt, !If, !Select, !Split, !Join
  • Cross-stack references: Export/Fn::ImportValue share outputs between stacks
  • Nested stacks: Break large templates into reusable child stacks via AWS::CloudFormation::Stack
  • Dynamic references: {{resolve:ssm:param-name}} pulls values from Parameter Store at deploy time
SAM template shortcuts:
Transform: AWS::Serverless-2016-10-31
Resources:
  MyFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: index.handler
      Runtime: python3.12
      Events:
        Api:
          Type: Api
          Properties:
            Path: /hello
            Method: get

Exam Trap: Fn::ImportValue creates a hard dependency between stacks. You cannot delete or update the exporting stack's output while any other stack imports it. For loose coupling, use SSM Parameter Store to share values instead.

Alvin Varughese
Written byAlvin Varughese•Founder•15 professional certifications