Lesson 4.2: Infrastructure as code for AWS developers
Infrastructure as code (IaC) is not optional in modern AWS development. It defines how environments are created, versioned, replicated, and recovered. The developer’s primary concern is not simply writing templates; it is reasoning about how declarative infrastructure reduces operational risk, ensures environment consistency, and enables safe, auditable deployments.
For developer-focused scenarios such as DVA-C02, questions rarely test syntax. Instead, they evaluate understanding of repeatable provisioning, controlled updates, rollback behavior, and multi-environment consistency.
AWS CloudFormation for declarative infrastructure control
AWS CloudFormation allows you to provision resources by defining a desired state in declarative templates. You specify what resources should exist and how they should be configured; CloudFormation handles creation, updates, and rollback.
Architectural benefits:
- Consistency across regions and accounts: Templates can be reused to create identical environments.
- Transactional updates: When stack updates fail, CloudFormation can automatically roll back to the last known-good state.
- Change sets: Preview modifications before applying them, reducing the risk of accidental disruptions.
Key developer considerations:
- Group related resources into stacks to manage them as single units.
- Use nested stacks for modularity and reuse.
- Understand drift detection: CloudFormation can detect manual changes that diverge from the declared template.
Example CLI to create a stack
aws cloudformation create-stack --stack-name MyAppStack --template-body file://app_stack.yaml --capabilities CAPABILITY_IAM
This command deploys the stack in a controlled, versioned way.
AWS CDK for infrastructure defined in code
AWS Cloud Development Kit (CDK) allows developers to define infrastructure using familiar programming languages such as Python, TypeScript, or Java. The CDK synthesizes the code into CloudFormation templates before deployment.
Advantages for developers:
- Express complex infrastructure patterns as reusable constructs.
- Integrate unit tests and validations at development time, before deployment.
- Programmatic generation allows dynamic resource configuration while retaining CloudFormation’s transactional guarantees.
Developer note: CDK simplifies template authoring but does not bypass CloudFormation. Understanding CloudFormation behavior rollback, change sets, stack dependencies is still essential.
Example CDK snippet (Python):
from aws_cdk import aws_s3 as s3, core
class MyStack(core.Stack):
def __init__(self, scope: core.Construct, id: str, **kwargs):
super().__init__(scope, id, **kwargs)
s3.Bucket(self, "MyAppBucket", versioned=True)
This code defines a versioned S3 bucket in a repeatable, programmatic way.
AWS SAM for serverless-focused infrastructure
AWS Serverless Application Model (SAM) is an extension of CloudFormation that simplifies defining serverless resources. SAM abstracts common Lambda patterns, API Gateway integration, and event sources.
SAM simplifies:
- Lambda function definitions and event mappings
- API Gateway integrations
- Packaging and deployment workflows for serverless apps
Example SAM CLI build and deploy:
sam build
sam deploy --stack-name MyServerlessApp --capabilities CAPABILITY_IAM
This workflow packages Lambda code, creates the necessary IAM roles, and deploys the stack safely.
Lambda aliases and traffic shifting
Lambda aliases allow multiple versions of a function to coexist. They decouple deployment from production traffic, enabling controlled rollouts.
Use cases:
- Gradual traffic shifting (canary deployments)
- Safe rollback to previous versions
- Minimizing production impact from new changes
Example of gradual traffic shift:
aws lambda update-alias --function-name ProcessOrders \
--name PROD --routing-config '{"AdditionalVersionWeights":{"2":0.1}}'
This directs 10% of traffic to version 2, leaving 90% on the stable version 1. If error rates spike, traffic can be instantly reverted to version 1, reducing operational risk.
Developer responsibilities and IaC best practices
For developers, IaC ensures:
- Environments are versioned alongside application code.
- Changes are auditable and reproducible.
- Rollouts are controlled, and rollback is always possible.
Key responsibilities include:
- Ensure templates or constructs are deterministic.
- Integrate testing, validation, and change previews into deployment workflows.
- Plan serverless deployments using aliases, traffic shifting, and automated rollback.
- Monitor for configuration drift and avoid manual environment modifications.
IaC shifts focus from writing scripts to reasoning about operational risk. Developers must understand how declarative infrastructure, version control, and safe deployment patterns collectively reduce operational risk, improve recovery, and ensure repeatable, predictable environments.
My name is Naeem ul Haq. I’ve been working with AWS since its early days and have deep expertise across its evolving ecosystem.