Study Guide845 words

Mastering Infrastructure as Code (IaC): AWS CloudFormation & SAM

Implement and deploy infrastructure as code (IaC) templates (for example, AWS SAM templates, AWS CloudFormation templates)

Mastering Infrastructure as Code (IaC): AWS CloudFormation & SAM

Infrastructure as Code (IaC) is the practice of managing and provisioning computing infrastructure through machine-readable definition files, rather than physical hardware configuration or interactive configuration tools. For the AWS Certified Developer Associate, mastering CloudFormation and the Serverless Application Model (SAM) is essential for automated, repeatable deployments.

Learning Objectives

  • Explain the core components of a CloudFormation template (Resources, Parameters, Outputs).
  • Differentiate between AWS CloudFormation, AWS SAM, and AWS CDK.
  • Implement serverless resources using the AWS SAM transform.
  • Manage stack lifecycles, including Change Sets and Rollbacks.
  • Configure deployment strategies like Canary and Blue/Green within IaC templates.

Key Terms & Glossary

  • Infrastructure as Code (IaC): The process of managing infrastructure using code and automation.
  • Stack: A single unit of management for AWS resources created from a CloudFormation template.
  • Template: A JSON or YAML file that describes the intended state of your AWS infrastructure.
  • Change Set: A preview of how proposed changes to a stack might impact your running resources.
  • Drift Detection: A feature that identifies if stack resources have been changed outside of CloudFormation management.
  • Intrinsic Function: Built-in functions (e.g., !Ref, !GetAtt) used to assign values to properties that are not available until runtime.

The "Big Idea"

Think of IaC as the "Blueprint for your House." Instead of manually building every room and hoping you remember how you did it next time, you write a detailed architectural plan. If you want to build the same house in a different city (Region) or for a different owner (Environment), you simply give the plan to the contractor (CloudFormation), and they build it exactly the same way every time. This eliminates "manual configuration drift" and ensures that Dev, Test, and Prod environments are identical.

Formula / Concept Box

FeatureAWS CloudFormationAWS SAMAWS CDK
FocusGeneral purpose AWS infrastructureServerless-specific (Lambda, API Gateway, DynamoDB)High-level programming (TS, Python, Java)
SyntaxJSON / YAMLYAML (Extension of CloudFormation)Imperative Code
ComplexityHigh (verbose)Low (abbreviated for serverless)Moderate (requires coding skills)
OutputDeploys directlyTransforms into CloudFormationSynthesizes into CloudFormation

Hierarchical Outline

  1. AWS CloudFormation Fundamentals
    • Template Anatomy: Parameters (inputs), Mappings (lookup tables), Resources (required), and Outputs (export values).
    • Stack Operations: Create, Update, and Delete. Understanding Rollbacks on failure.
    • Advanced Features: Nested Stacks (reusability), StackSets (multi-account/multi-region).
  2. AWS Serverless Application Model (SAM)
    • SAM Transform: The AWS::Serverless-2016-10-31 header.
    • Resource Types: AWS::Serverless::Function, AWS::Serverless::Api, AWS::Serverless::SimpleTable.
    • SAM CLI: Commands for local testing (sam local invoke) and deployment (sam deploy).
  3. Deployment & Management
    • CI/CD Integration: Connecting IaC to CodePipeline.
    • Safety: Using DeletionPolicy (Retain, Snapshot) to prevent accidental data loss.

Visual Anchors

CloudFormation Lifecycle

Loading Diagram...

SAM Transformation Concept

\begin{tikzpicture}[node distance=2cm, auto] \draw[thick, fill=orange!20] (0,0) rectangle (3,2) node[midway] {\begin{tabular}{c} SAM Template \ (20 Lines) \end{tabular}}; \draw[->, ultra thick] (3.5,1) -- (5.5,1) node[midway, above] {Transform}; \draw[thick, fill=blue!20] (6, -1) rectangle (10, 3) node[midway] {\begin{tabular}{c} CloudFormation \ (200 Lines) \end{tabular}}; \node at (5,-2) [text width=8cm, align=center] {\small AWS SAM acts as a shorthand that expands into complex standard CloudFormation resources.}; \end{tikzpicture}

Definition-Example Pairs

  • Intrinsic Function (!Ref): A function that returns the value of a specified parameter or resource.
    • Example: Using !Ref MyS3Bucket to pass the name of a bucket created in the same template to a Lambda environment variable.
  • Pseudo Parameters: Predefined parameters provided by AWS (e.g., AWS::Region, AWS::AccountId).
    • Example: Using !Sub "arn:aws:s3:::my-bucket-${AWS::Region}" to make a resource ARN dynamic across regions.
  • SAM Policy Templates: Pre-defined IAM policies for common serverless patterns.
    • Example: Adding S3ReadPolicy to a Lambda function to grant read access to a specific bucket without writing a full JSON IAM policy.

Worked Examples

Scenario: Deploying a Simple Serverless API

Step 1: The SAM Template (template.yaml)

yaml
AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Resources: MyFunction: Type: AWS::Serverless::Function Properties: Handler: index.handler Runtime: nodejs18.x Events: GetRoot: Type: Api Properties: Path: / Method: get

Step 2: Build & Package Run sam build to process the template and handle dependencies.

Step 3: Deploy Run sam deploy --guided. This uploads the artifacts to S3 and creates the CloudFormation stack.

[!TIP] Always use sam sync during development to quickly push code changes to the cloud without a full stack redeployment.

Checkpoint Questions

  1. Which section of a CloudFormation template is the only mandatory section?
  2. What happens to resources created by CloudFormation if a stack update fails halfway through?
  3. How does AWS SAM identify that a template should be treated as a SAM template instead of standard CloudFormation?
  4. Which CLI command would you use to test an AWS Lambda function locally using a mock event?
View Answers
  1. Resources: All other sections (Parameters, Outputs, etc.) are optional.
  2. Rollback: CloudFormation attempts to return all resources to their previous known stable state.
  3. Transform Header: The presence of Transform: AWS::Serverless-2016-10-31.
  4. sam local invoke "FunctionName" -e event.json

Ready to study AWS Certified Developer - Associate (DVA-C02)?

Practice tests, flashcards, and all study notes — free, no sign-up needed.

Start Studying — Free