Category: AWS Deployments

A technical reference for deploying infrastructure and applications on AWS. Each post in this category covers a specific deployment topic end-to-end — from writing the first line of infrastructure code to running production-grade workloads — using tools like AWS CDK, AWS SAM, AWS CLI, and CloudFormation. Whether you are deploying Lambda functions, ECS clusters, API Gateways, or VPC stacks, every post walks through the deployment lifecycle with real commands, real configuration, and the operational patterns that separate working prototypes from production systems.

  • Deploying a lambda function

    Modern serverless architectures are built on the principle that infrastructure should
    scale automatically, respond to events in milliseconds, and eliminate the operational
    burden of managing servers. AWS Lambda sits at the center of that serverless model.

    A Lambda function is the deployed instance of executable code running inside AWS’s
    managed compute environment. The deployment package defines the function’s behavior,
    while the Lambda function resource represents the actual running compute unit created
    from that definition.

    Core Concept:
    The deployment package is the code. The Lambda function is the deployed, invokable compute unit.


    Understanding What Gets Deployed

    Every Lambda deployment begins with a deployment package. That package may be:

    • A .zip file archive containing function code and dependencies
    • A container image stored in Amazon ECR
    • Generated by AWS CDK using the NodejsFunction or Function construct
    • Produced through AWS SAM using the AWS::Serverless::Function resource type
    Tool What You Write What Lambda Receives
    AWS CDK TypeScript / Python / Java Synthesized CloudFormation + zipped function asset
    AWS SAM Serverless Template (YAML) Expanded CloudFormation + deployment package
    AWS CLI .zip file or S3 URI Directly uploaded deployment package

    Deploying a Lambda Function Using AWS CDK

    AWS CDK allows Lambda infrastructure to be written using real programming languages
    instead of verbose YAML templates. CDK bundles function code, uploads it to S3, and
    deploys it through CloudFormation automatically.

    Step 1: Initialize the CDK Application

    npm install -g aws-cdk
    
    mkdir lambda-demo
    cd lambda-demo
    
    cdk init app --language typescript

    Step 2: Define the Stack

    import * as cdk from 'aws-cdk-lib';
    import * as lambda from 'aws-cdk-lib/aws-lambda';
    import * as apigateway from 'aws-cdk-lib/aws-apigateway';
    import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs';
    import * as path from 'path';
    
    export class LambdaDemoStack extends cdk.Stack {
    
      constructor(scope: cdk.App, id: string) {
        super(scope, id);
    
        const fn = new NodejsFunction(this, 'HelloFunction', {
          runtime: lambda.Runtime.NODEJS_22_X,
          entry: path.join(__dirname, '../lambda/handler.ts'),
          handler: 'handler',
          timeout: cdk.Duration.seconds(30),
          memorySize: 256,
          environment: {
            ENVIRONMENT: 'production'
          }
        });
    
        new apigateway.LambdaRestApi(this, 'HelloApi', {
          handler: fn
        });
      }
    }

    Step 3: Bootstrap the Environment

    cdk bootstrap

    This creates the bootstrap stack:

    CDKToolkit

    Step 4: Synthesize the Template

    cdk synth

    CDK bundles the function code using esbuild, uploads the .zip asset to the bootstrap
    S3 bucket, and converts constructs into a full CloudFormation template.

    Step 5: Deploy the Function

    cdk deploy

    What Happens During Deployment

    1. CDK packages function code into a .zip asset and uploads it to S3
    2. CloudFormation validates the template and resolves the S3 asset location
    3. The Lambda function resource is created with the specified runtime and memory
    4. Lambda provisions an execution environment for the function
    5. Rollback occurs automatically on failure

    Monitoring Function Events

    aws lambda get-function \
      --function-name LambdaDemoStack-HelloFunction

    Rollback Behavior

    If a function update fails midway through a CloudFormation-managed deployment,
    CloudFormation automatically rolls the stack back to its previous stable state.

    For debugging failed deployments without automatic rollback:

    cdk deploy --no-rollback

    Deploying Using the AWS CLI

    Create a Function

    aws lambda create-function \
      --function-name hello-function \
      --runtime python3.12 \
      --handler lambda_function.lambda_handler \
      --role arn:aws:iam::123456789012:role/lambda-execution-role \
      --zip-file fileb://deployment.zip \
      --timeout 30 \
      --memory-size 256

    Update Function Code

    aws lambda update-function-code \
      --function-name hello-function \
      --zip-file fileb://deployment.zip

    Delete a Function

    aws lambda delete-function \
      --function-name hello-function

    Deploying Through CI/CD Pipelines

    Publishing a Version

    aws lambda publish-version \
      --function-name hello-function \
      --description "Release v2 - added input validation"

    Publishing creates an immutable, numbered snapshot of the function’s code and configuration.


    Creating an Alias

    aws lambda create-alias \
      --function-name hello-function \
      --name production \
      --function-version 3 \
      --description "Production traffic"

    Aliases are named pointers to specific published versions, allowing triggers to reference
    a stable function endpoint that can be updated independently of version numbers.


    Canary Deployment

    aws lambda update-alias \
      --function-name hello-function \
      --name production \
      --function-version 3 \
      --routing-config AdditionalVersionWeights={"4"=0.10}

    Weighted alias routing splits live traffic between two published versions, enabling
    gradual rollouts with the ability to roll back by updating the alias pointer.


    Final Thoughts

    Lambda deployment is fundamentally about managing the lifecycle of executable code
    within a managed, event-driven compute environment.

    Whether a function is deployed through:

    • AWS CDK
    • AWS SAM
    • AWS CLI
    • Container Images via ECR
    • CI/CD Pipelines

    the underlying Lambda execution model remains the same.

    Understanding deployment package types, execution role requirements, version
    and alias management, and canary traffic shifting is what separates
    basic Lambda usage from production-grade serverless engineering.

  • Deploying a CloudFormation Stack

    Modern AWS infrastructure is expected to be reproducible, version-controlled,
    reviewable, and deployable through automation. AWS CloudFormation sits at the
    center of that operational model.

    A CloudFormation stack is the deployed instance of an infrastructure template.
    The template defines the desired state of AWS resources, while the stack
    represents the actual running infrastructure created from that definition.

    Core Concept:
    The template is the blueprint. The stack is the deployed infrastructure.


    Understanding What Gets Deployed

    Every deployment begins with a CloudFormation template. That template may be:

    • Written directly in YAML or JSON
    • Generated by AWS CDK
    • Expanded through AWS SAM transforms
    • Produced through automation pipelines
    Tool What You Write What CloudFormation Receives
    AWS CDK TypeScript / Python / Java Synthesized CloudFormation Template
    CloudFormation YAML / JSON Same Template
    AWS SAM Serverless Template Expanded CloudFormation Template

    Deploying Infrastructure Using AWS CDK

    AWS CDK allows infrastructure to be written using real programming languages
    instead of verbose YAML templates.

    Step 1: Initialize the CDK Application

    
    npm install -g aws-cdk
    
    mkdir ecs-demo
    cd ecs-demo
    
    cdk init app --language typescript
    

    Step 2: Define the Stack

    
    import * as cdk from 'aws-cdk-lib';
    import * as ecs from 'aws-cdk-lib/aws-ecs';
    import * as ec2 from 'aws-cdk-lib/aws-ec2';
    import * as ecs_patterns from 'aws-cdk-lib/aws-ecs-patterns';
    
    export class EcsDemoStack extends cdk.Stack {
    
      constructor(scope, id) {
        super(scope, id);
    
        const vpc = new ec2.Vpc(this, 'AppVpc');
    
        const cluster = new ecs.Cluster(this, 'AppCluster', {
          vpc
        });
    
        new ecs_patterns.ApplicationLoadBalancedFargateService(
          this,
          'AppService',
          {
            cluster,
            taskImageOptions: {
              image: ecs.ContainerImage.fromRegistry('nginx')
            }
          }
        );
      }
    }
    

    Step 3: Bootstrap the Environment

    
    cdk bootstrap
    

    This creates the bootstrap stack:

    
    CDKToolkit
    

    Step 4: Synthesize the Template

    
    cdk synth
    

    CDK converts application constructs into a full CloudFormation template.

    Step 5: Deploy the Stack

    
    cdk deploy
    

    What Happens During Deployment

    1. CloudFormation validates the template
    2. Dependencies between resources are resolved
    3. Resources are provisioned in parallel
    4. Stack events stream continuously
    5. Rollback occurs automatically on failure

    Monitoring Stack Events

    
    aws cloudformation describe-stack-events \
      --stack-name EcsDemoStack
    

    Rollback Behavior

    If resource creation fails midway, CloudFormation automatically rolls back
    the deployment.

    For debugging large deployments:

    
    cdk deploy --no-rollback
    

    Deploying Using the AWS CLI

    Create a Stack

    
    aws cloudformation create-stack \
      --stack-name networking-stack \
      --template-body file://vpc.yaml \
      --capabilities CAPABILITY_IAM
    

    Update a Stack

    
    aws cloudformation update-stack \
      --stack-name networking-stack \
      --template-body file://vpc.yaml
    

    Delete a Stack

    
    aws cloudformation delete-stack \
      --stack-name networking-stack
    

    Deploying Through CI/CD Pipelines

    Using Change Sets

    
    aws cloudformation create-change-set \
      --stack-name production-stack \
      --change-set-name v2-update \
      --template-body file://template.yaml
    

    Change sets preview infrastructure modifications before execution.


    Drift Detection

    
    aws cloudformation detect-stack-drift \
      --stack-name production-stack
    

    Drift detection identifies infrastructure modified outside CloudFormation.


    Termination Protection

    
    aws cloudformation update-termination-protection \
      --stack-name production-stack \
      --enable-termination-protection
    

    Termination protection prevents accidental deletion of production
    infrastructure.


    Final Thoughts

    CloudFormation deployment is fundamentally about managing infrastructure as a
    deterministic and reproducible system.

    Whether infrastructure is deployed through:

    • AWS CDK
    • Raw CloudFormation
    • AWS SAM
    • AWS CLI
    • CI/CD Pipelines

    the underlying CloudFormation engine remains the same.

    Understanding dependency resolution, rollback handling, resource
    stabilization, and stack lifecycle management is what separates
    basic AWS usage from production-grade infrastructure engineering.