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
- CDK packages function code into a .zip asset and uploads it to S3
- CloudFormation validates the template and resolves the S3 asset location
- The Lambda function resource is created with the specified runtime and memory
- Lambda provisions an execution environment for the function
- 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.
Leave a Reply