Configuring Lambda functions

Serverless Applications with AWS Lambda

Claudio Canales

Senior DevOps Engineer

Why configuration matters

  • The same code can behave very differently with different settings.
  • Configuration controls performance, cost, and safety.
  • The goal is predictable behavior in dev and prod.

Same code, different settings

Serverless Applications with AWS Lambda

Code vs configuration

Code package

  • Code package
  • Handler logic + dependencies.
  • Changes require a deploy.

Function configuration

  • Function configuration
  • Memory, timeout, environment variables.
  • Execution role + layers.
Serverless Applications with AWS Lambda

The knobs you control

  • Runtime + handler: what code Lambda runs.
  • Memory + timeout: performance, cost, safety.
  • Environment variables: configuration without hardcoding.
  • Execution role: permissions for AWS APIs.
  • Layers: shared dependencies across functions.

Lambda configuration knobs

Serverless Applications with AWS Lambda

Runtime and handler basics

  • Runtime = language environment (for example, Python).
  • Handler = the entry point Lambda calls.
  • Handler format: file.function.
  • Example: lambda_function.lambda_handler.

Handler string breakdown

Serverless Applications with AWS Lambda

Handler string: what can go wrong

# lambda_function.py
def lambda_handler(event, context):
    return {"statusCode": 200}

Import then handler call

Serverless Applications with AWS Lambda

The execution role (IAM)

  • IAM = Identity and Access Management (permissions).
  • The execution role is what your function is allowed to do.
  • It controls which AWS APIs your code can call.

Execution role as a keycard

Serverless Applications with AWS Lambda

What is a Lambda layer?

  • A layer is a ZIP you attach to a function.
  • Lambda makes layer files available at runtime under /opt.
  • Use layers for shared libraries and common utilities.
  • Update the layer once; multiple functions can reuse it.

Layer mounted under /opt

Serverless Applications with AWS Lambda

Where layers show up at runtime

  • Your function code is mounted under /var/task.
  • Your attached layers are mounted under /opt.
  • Lambda also provides a temporary folder at /tmp.

Runtime folders: /var/task, /opt, /tmp

Serverless Applications with AWS Lambda

Memory also affects CPU

  • More memory -> more CPU.
  • Can reduce runtime for CPU-bound tasks.
  • Total cost depends on duration and memory.

Memory increases CPU

Serverless Applications with AWS Lambda

Tune memory with measurements

  • Start with a reasonable default (for example, 256 MB).
  • Test with realistic events.
  • Compare duration and cost across memory sizes.

Memory sweep: measure duration and cost

Serverless Applications with AWS Lambda

Timeouts and safe execution

  • Timeout stops runaway executions.
  • Check remaining time before long work.
  • Fail fast and retry when appropriate.

Timeout as a safety timer

Serverless Applications with AWS Lambda

Walkthrough: checking remaining time

def lambda_handler(event, context):
    time_left = context.get_remaining_time_in_millis()
    if time_left < 500:
        raise TimeoutError("Not enough time left")
    return {"statusCode": 200}

Context countdown guard

Serverless Applications with AWS Lambda

Environment variables

  • Keep config out of code.
  • Separate dev/test/prod settings.
  • Use Secrets Manager for sensitive values.

Environment variables configure behavior

Serverless Applications with AWS Lambda

Walkthrough: reading environment variables

import os

STAGE = os.getenv("STAGE", "dev")
LOG_LEVEL = os.getenv(
    "LOG_LEVEL", "INFO"
)

Defaults make local tests safer

  • Import os to access environment variables.
  • os.getenv returns value or default.
Serverless Applications with AWS Lambda

Common configuration pitfalls

  • Hard-coding stage values inside code.
  • Timeout too low or missing IAM permissions.
  • Secrets in plain environment variables.

Pitfalls and rules of thumb

Serverless Applications with AWS Lambda

Let's practice!

Serverless Applications with AWS Lambda

Preparing Video For Download...