Lesson 1 of 9 Data Engineering Practices

Defining Data Platforms with Infrastructure as Code and AWS SAM

This lesson explains how infrastructure as code (IaC) defines data platforms in version-controlled templates, so environments are repeatable, reviewable and easy to roll back.

Learning outcomes

After completing this lesson, you will be able to:

  • Distinguish CloudFormation, AWS SAM and the AWS CDK.
  • Describe the SAM CLI commands.
  • Identify the benefits of IaC for data platforms.

IaC tools

Infrastructure as code (IaC) defines buckets, Glue jobs, streams and roles in files, so every environment is built the same way and changes are reviewed like code. AWS CloudFormation is the foundation: templates in JSON or YAML describe resources, parameters supply values that differ per environment, change sets preview what a deployment will change, and StackSets deploy to many accounts and Regions.

The AWS CDK (Cloud Development Kit) writes infrastructure in Python or TypeScript and generates CloudFormation. Its high-level constructs apply sensible defaults, loops and classes remove repetition, and helpers such as bucket.grantRead(role) write least-privilege policies. AWS SAM (Serverless Application Model) extends CloudFormation with short resource types for functions, state machines and tables, simple event source syntax and policy templates, which suits serverless pipelines. The following table summarises the tools.

ToolWhat it does
AWS CloudFormationDescribes resources (buckets, Glue jobs and crawlers, Redshift, Kinesis, IAM, Step Functions) in JSON or YAML; parameters per environment, change sets to preview, StackSets for many accounts and Regions
AWS CDKInfrastructure in Python or TypeScript with high-level constructs; loops and classes reduce repetition; helpers like bucket.grantRead(role) write least-privilege policies
AWS SAMA CloudFormation extension for serverless pipelines: AWS::Serverless::Function, StateMachine, SimpleTable, short event source syntax and policy templates
Sort into groups

CloudFormation, CDK or SAM?

Exam skills 1.4.5 · 1.4.6 · 1.4.8

Decide which IaC tool each statement describes.

Log in to try this activity Interactive activities are free with an account.

The SAM CLI

The SAM command line interface (CLI) supports the whole development cycle. sam build prepares the code and its dependencies, and sam deploy deploys the application through CloudFormation. sam local invoke runs a function on your own computer in a container that imitates Lambda, so you can test it with a sample event before deploying. sam sync, known as SAM Accelerate, quickly pushes code changes to a development stack in the cloud, often without a full deployment, for fast iteration. The following table summarises the commands.

CommandPurpose
sam build, sam deployBuild and deploy
sam local invokeTest functions locally
sam sync (SAM Accelerate)Fast iteration against the cloud

The following diagram shows one infrastructure template deployed to three environments.

One template deployed to three environments. The CloudFormation or CDK template and the Glue job scripts are kept in Git and changed by pull request. A deployment pipeline deploys the same template to development, test and production, with an environment parameter that changes only names and sizes, and rolls back a failed deployment.
The bucket names and worker counts are illustrative.
Match the pairs

SAM CLI commands

Exam skills 1.4.6

Match each task with the SAM CLI command.

Log in to try this activity Interactive activities are free with an account.

Benefits of IaC

Data platforms contain many resources: buckets, Glue jobs and crawlers, Step Functions workflows, Lambda functions, Redshift workgroups, IAM roles and Lake Formation permissions. Created by hand in the console, these drift apart between environments, and nobody can say exactly how production was built. Infrastructure as code (IaC) describes them in templates or code instead.

The benefits follow directly. Development, test and production environments are created from the same definitions, so a pipeline that works in test behaves the same way in production. Infrastructure changes are reviewed like code and deployed automatically through CI/CD pipelines. If a deployment fails, AWS CloudFormation rolls it back to the previous state; drift detection reveals changes made outside the templates; and the templates themselves are a reliable record of what exists and why.

Select all that apply

Why use IaC

Exam skills 1.4.5 · 1.4.8

Identify the benefits of infrastructure as code.

Log in to try this activity Interactive activities are free with an account.
Worked example

The same pipeline in three environments

A team's pipeline (S3 buckets, Glue jobs, a Step Functions state machine, IAM roles) was built by hand in production, and the test environment differs in small ways that cause surprises.

With infrastructure as codeBenefit
One CloudFormation or CDK template with an environment parameterDev, test and prod are identical except for names and sizes
Templates in Git, changed by pull requestEvery change is reviewed and recorded
Deployed by a pipelineNo manual steps; rollback to the previous version
Glue job scripts packaged with the templateCode and infrastructure versioned together

For a pipeline built mostly from Lambda functions and Step Functions, AWS SAM shortens the templates and adds local testing with sam local invoke.

Check yourself

How can infrastructure changes be previewed before they are applied?

Show answer

With CloudFormation change sets.

Exam Tips

Remember for the exam

  • To package and deploy serverless pipelines built on Lambda and Step Functions, use AWS SAM.
  • To define infrastructure in Python or TypeScript, use the AWS CDK.
  • To preview infrastructure changes before applying them, use CloudFormation change sets.
  • To deploy the same stack to many accounts and Regions, use StackSets.
  • To test Lambda functions locally, use sam local invoke.