Part 2: AWS Lambda Fundamentals

Understanding Lambda's Execution Model

After migrating several applications to Lambda, I've developed a deep understanding of how it works under the hood. This knowledge has been crucial for optimizing performance and avoiding common pitfalls.

The Lambda Lifecycle

Every Lambda function follows a specific lifecycle. Understanding this has helped me optimize my functions significantly:

spinner

Python Runtime in Lambda

Lambda supports multiple Python versions. I primarily use Python 3.12 for new projects, but here's what you need to know:

Available Python Runtimes

  • python3.12 (Recommended)

  • python3.11

  • python3.10

  • python3.9

Runtime Environment

Each Lambda execution environment includes:

  • Amazon Linux 2023 (for Python 3.12)

  • AWS SDK for Python (boto3)

  • 512 MB to 10,240 MB memory

  • Ephemeral storage (/tmp): 512 MB to 10 GB

The Handler Function

The handler is your function's entry point. Here's the anatomy of a Lambda handler based on my standard template:

Basic Handler Structure

The Event Object

The event parameter contains data from the invoking service. Here's what I typically see from different sources:

API Gateway Event Example

S3 Event Example

The Context Object

The context parameter provides runtime information. Here's how I use it:

Context Object Properties

Property
Description
Example

function_name

Function name

my-lambda-function

function_version

Version

$LATEST or 1

invoked_function_arn

Full ARN

arn:aws:lambda:us-east-1:123456789012:function:my-func

memory_limit_in_mb

Allocated memory

256

aws_request_id

Unique request ID

abc123-def456

log_group_name

CloudWatch log group

/aws/lambda/my-function

log_stream_name

CloudWatch log stream

2024/01/24/[$LATEST]abc123

Handler Invocation Patterns

Here's how I structure handlers for different use cases:

Pattern 1: API Response Handler

Pattern 2: Event Processing Handler

Lambda Execution Flow

Here's the complete request flow with error handling:

spinner

Memory and Performance

One critical learning: Lambda memory directly affects CPU allocation. Here's what I've observed:

Memory-to-CPU Ratio

Memory (MB)
vCPU
Approx. CPU Power

128

0.08

Very Low

512

0.33

Low

1,024

0.67

Medium

1,792

1.00

1 full vCPU

3,584

2.00

2 full vCPUs

10,240

6.00

6 full vCPUs

Performance Testing from My Projects

I ran the same data processing function with different memory settings:

Key Insight: 512-1024 MB often provides the best cost-performance ratio. More memory can actually reduce costs by completing faster!

Environment Variables

I use environment variables for configuration in every Lambda function:

Setting Environment Variables

Best Practices for Environment Variables

  1. Never hardcode secrets - Use AWS Secrets Manager or Parameter Store

  2. Use descriptive names - DYNAMODB_TABLE not TABLE

  3. Provide defaults - Use .get() with fallback values

  4. Document required variables - In function description or README

Initialization Optimization

One of my biggest performance wins came from optimizing initialization:

❌ Bad Practice (Reinitialize Every Invocation)

βœ… Good Practice (Initialize Once, Reuse)

Initialization Flow

spinner

Logging Best Practices

Effective logging has saved me countless hours of debugging:

Structured Logging

Key Takeaways

From my experience with Lambda fundamentals:

  1. Initialize Outside the Handler: Reuse connections and resources

  2. Right-Size Memory: Test to find the optimal memory setting

  3. Use Structured Logging: Makes debugging and monitoring easier

  4. Handle Errors Gracefully: Always implement proper error handling

  5. Understand the Event Structure: Each trigger source has a different format

  6. Monitor Context: Use remaining time for graceful timeouts

What's Next?

In Part 3: Building Your First Lambda Function, we'll:

  • Set up the AWS CLI and SAM CLI

  • Create and deploy a real Python Lambda function

  • Test locally and in AWS

  • Implement environment-specific configurations

  • Add error handling and logging

Get ready to write actual code!


This article draws from my experience optimizing dozens of Lambda functions in production environments.

Last updated