Skip to content
VirtualLP Virtual Learning Platform

AWS Lambda: Functions, Execution Roles, Cold Starts and Limits

15 min read Topic 12 of 30 1 hands-on lab

AWS Lambda runs your code in response to events without servers to manage. You upload a function; Lambda creates execution environments, scales them with traffic and bills per request and per millisecond.

The handler

// index.mjs  –  handler setting: index.handler
export const handler = async (event, context) => {
  console.log('Request', context.awsRequestId, JSON.stringify(event));
  const name = event.name ?? 'world';
  return { statusCode: 200, body: `Hello, ${name}!` };
};
Piece Meaning
event JSON input – shape depends on the trigger (API Gateway, SQS, S3, test event)
context Request ID, function name, memory, getRemainingTimeInMillis()
return value Response for synchronous callers; thrown errors become failures
Handler setting file.export – e.g. index.handler (a wrong value gives Runtime.HandlerNotFound)

Execution environment lifecycle

sequenceDiagram
    participant T as Trigger
    participant L as Lambda service
    participant E as Execution environment
    T->>L: invoke
    L->>E: create (cold start) – download code, start runtime, run INIT (code outside the handler)
    E-->>L: ready
    L->>E: run handler(event)
    E-->>T: result
    T->>L: invoke again
    L->>E: reuse warm environment – handler only
  • Cold start: first request on a new environment runs the INIT phase (shows as Init Duration in the REPORT log line).
  • Warm start: reuses the environment – variables and SDK clients created outside the handler are reused, so create clients there.
  • Environments are frozen between invocations and eventually recycled; never rely on local state.

Configuration

Setting Notes
Memory 128 MB – 10 GB; CPU scales with memory
Timeout Default 3 s, max 15 min – exceeding it gives Task timed out after N seconds
Environment variables Configuration without code changes (process.env.TABLE_NAME)
Execution role IAM role the function assumes – AWSLambdaBasicExecutionRole only allows logs
Architecture x86_64 or arm64 (Graviton) – cheaper per ms
Ephemeral storage /tmp 512 MB – 10 GB
Layers / container images Share libraries; package up to 10 GB as an image

Invocation models and triggers

Model Examples Errors and retries
Synchronous API Gateway, function URL, SDK Invoke Caller gets the error
Asynchronous S3 events, EventBridge, SNS Lambda retries twice; send failures to a destination / DLQ
Poll-based (event source mapping) SQS, DynamoDB Streams, Kinesis Lambda polls and batches; partial batch failures

Permissions in both directions: the execution role lets the function call other services; a resource-based policy lets other services (API Gateway, S3, Bedrock Agents) invoke the function.

Versions, aliases and concurrency

  • Publish a version = immutable snapshot of code + config; $LATEST is mutable.
  • Alias (prod) points to a version; weighted aliases enable canary releases.
  • Concurrency = environments running at once; reserved concurrency caps or guarantees it, provisioned concurrency keeps environments warm.

Observability and cost

Every invocation writes START, your log lines, END and REPORT (duration, billed duration, memory used) to CloudWatch Logs /aws/lambda/<function>. Metrics: Invocations, Errors, Duration, Throttles.

Cost = requests × price + GB-seconds (memory × duration). The free tier covers 1 M requests and 400,000 GB-s per month.

Key point

Keep handlers small and stateless, initialise SDK clients outside the handler, give the role only the actions it needs, and set the timeout just above the expected duration.

Practise it

Virtual lab · Amazon Web Services · 30 min

Build, test and debug your first Lambda function

Write a Node.js Lambda function that really runs: test it with JSON events, configure environment variables, hit a real timeout, fix it and read the logs in CloudWatch.

Open lab

Quick revision

  1. Handler gets event + context; handler setting is file.export.
  2. Cold start runs INIT; warm starts reuse environments.
  3. Memory sets CPU; default timeout 3 s, max 15 min.
  4. Execution role = outbound permissions; resource policy = who may invoke.
  5. Logs: START / END / REPORT in CloudWatch; pay per request + GB-second.

Practise this topic

Hands-on virtual lab

Apply what you just read in a realistic, guided simulation. Free with a verified account.

Create a free account to track your progress and unlock virtual labs.