teluzia.Cloud Engineer · Day 64
CHAPTER 10 · SERVERLESS AND MESSAGING

Lambda fundamentals

4–5 hours including independent practice3 topics
LET’S MAKE THIS SIMPLE

What you’re learning today

A Lambda function is an on-call clerk: the event is the work ticket, the execution role is the badge, and the timeout is the shift limit. Lambda runs a configured handler when an invocation arrives. The event is input data; the context describes the invocation. Synchronous callers wait for a response, while asynchronous callers receive acceptance before execution completes. The function should validate input before performing work and return a contract the caller understands. The Lambda execution role controls what function code may do in AWS. Its trust policy permits the Lambda service to assume it, while attached permissions authorize actions such as writing logs. Permission for another service to invoke the function is separate and may use a function resource policy.

Think of it this way

A Lambda function is an on-call clerk: the event is the work ticket, the execution role is the badge, and the timeout is the shift limit.

TOPIC 01 / 03Step by step

Invocation and handler

Lambda runs a configured handler when an invocation arrives. The event is input data; the context describes the invocation. Synchronous callers wait for a response, while asynchronous callers receive acceptance before execution completes. The function should validate input before performing work and return a contract the caller understands. For example, a Python handler returns {"statusCode": 200, "body": "hello"} for an HTTP integration, but a direct invocation can return ordinary JSON.

WORKED EXAMPLE
A Python handler returns {"statusCode": 200, "body": "hello"} for an HTTP integration, but a direct invocation can return ordinary JSON.

Start with a scheduled report rather than an HTTP request. At 09:00, a scheduling service sends an event containing the report date. Lambda finds the configured handler, prepares an execution environment if necessary, and passes that event to the handler. Initialization runs before the handler's first invocation in that environment; later invocations may reuse initialized clients. Initialization is therefore a performance opportunity, not a promise that memory will survive tomorrow.

An invocation is one attempt to execute the function. A trigger is the service or integration initiating that attempt. The payload is the supplied event data. A synchronous requester waits for the attempt's response; asynchronous acceptance means AWS accepted work, not that the report exists. Distinguish malformed event data from an infrastructure invocation failure. Returning an error object may count as a normal function completion, whereas throwing an exception communicates failure through the relevant invocation mechanism and its retry rules.

Why this matters

Knowing who waits for the result helps you choose response contracts, retries, error handling, and appropriate monitoring for each invocation path.

A common trap

Do not assume every event has the same structure; API Gateway events and SQS batches need different parsing and error handling logic.

Read all topics in this lesson

A complete reference to revisit alongside the guided steps above.

Invocation and handler

Lambda runs a configured handler when an invocation arrives. The event is input data; the context describes the invocation. Synchronous callers wait for a response, while asynchronous callers receive acceptance before execution completes. The function should validate input before performing work and return a contract the caller understands. For example, a Python handler returns {"statusCode": 200, "body": "hello"} for an HTTP integration, but a direct invocation can return ordinary JSON.

WORKED EXAMPLE
A Python handler returns {"statusCode": 200, "body": "hello"} for an HTTP integration, but a direct invocation can return ordinary JSON.

Start with a scheduled report rather than an HTTP request. At 09:00, a scheduling service sends an event containing the report date. Lambda finds the configured handler, prepares an execution environment if necessary, and passes that event to the handler. Initialization runs before the handler's first invocation in that environment; later invocations may reuse initialized clients. Initialization is therefore a performance opportunity, not a promise that memory will survive tomorrow.

An invocation is one attempt to execute the function. A trigger is the service or integration initiating that attempt. The payload is the supplied event data. A synchronous requester waits for the attempt's response; asynchronous acceptance means AWS accepted work, not that the report exists. Distinguish malformed event data from an infrastructure invocation failure. Returning an error object may count as a normal function completion, whereas throwing an exception communicates failure through the relevant invocation mechanism and its retry rules.

Why this matters

Knowing who waits for the result helps you choose response contracts, retries, error handling, and appropriate monitoring for each invocation path.

A common trap

Do not assume every event has the same structure; API Gateway events and SQS batches need different parsing and error handling logic.

Execution role and resource access

The Lambda execution role controls what function code may do in AWS. Its trust policy permits the Lambda service to assume it, while attached permissions authorize actions such as writing logs. Permission for another service to invoke the function is separate and may use a function resource policy. For example, a function reading one bucket needs narrowly scoped s3:GetObject permission on that bucket’s objects, not AdministratorAccess on every AWS resource.

WORKED EXAMPLE
A function reading one bucket needs narrowly scoped s3:GetObject permission on that bucket’s objects, not AdministratorAccess on every AWS resource.

Imagine an analyst manually invoking a report function. First, the analyst's IAM identity needs permission to invoke the function. Next, Lambda assumes the function's execution role through its trust relationship. The handler then reads an S3 input and writes a report using that role's permissions. These are two separate authorization decisions: permission to call the function does not determine everything the running code may access.

A trust policy identifies who may assume a role. A permission policy identifies allowed actions and resources after assumption. A resource policy attached to a function can authorize an invoking service or another account where applicable. If invocation succeeds but the S3 read fails, investigate the execution role and any relevant bucket or key policies. Broadening the analyst's permissions will not necessarily fix it. Conversely, granting S3 access to the execution role does not automatically authorize a new service to invoke the function.

Why this matters

Separating invocation permissions from execution permissions makes access failures diagnosable and reduces the damage a compromised handler can cause inside your account.

A common trap

Granting broad administrator permissions to fix AccessDenied hides the actual missing action, resource, or condition and leaves unnecessary long-lived privilege behind.

Timeout, memory, and retry-safe work

A timeout ends an invocation that takes too long. Memory configuration also affects available compute resources, so a larger memory setting can change duration. Reused execution environments improve efficiency but are not durable storage. Repeated invocations require idempotent effects because failures or upstream retry policies can repeat an event. For example, store an order ID before charging; a repeated order event should return the existing result instead of performing a second charge.

WORKED EXAMPLE
Store an order ID before charging; a repeated order event should return the existing result instead of performing a second charge.

Suppose a function produces a receipt and sends it to a customer. The receipt is stored successfully, but the invocation times out before its caller receives confirmation. The caller retries. Unless the application checks a stable receipt identifier, a second email or second payment may follow. A timeout limits the invocation's duration; it does not undo effects already completed outside the process.

Idempotency means repeated requests for the same operation produce the intended effect once or return its existing result. Concurrency means multiple invocations can execute at the same time, potentially reading the same initial state. A database uniqueness constraint or conditional write can resolve that race more reliably than an in-memory set. Store a business identifier and an explicit completion state. Memory sizing affects available compute, so benchmark duration and cost together. Faster execution can reduce billed duration, but more memory is not a universal improvement and cannot substitute for duplicate-safe transactions.

Why this matters

Bounded execution and duplicate protection prevent runaway work, confusing retries, repeated side effects, and unreliable assumptions about the lifetime of process memory.

A common trap

Treating a global variable or temporary file as permanent data fails when an execution environment disappears, scales out, or handles another event.

KEEP THESE WITH YOU

Today’s takeaways

Invocation and handler: Lambda runs a configured handler when an invocation arrives. The event is input data; the context describes the invocation. Synchronous callers wait for a response, while asynchronous callers receive acceptance before execution completes. The function should validate input before performing work and return a contract the caller understands.

Execution role and resource access: The Lambda execution role controls what function code may do in AWS. Its trust policy permits the Lambda service to assume it, while attached permissions authorize actions such as writing logs. Permission for another service to invoke the function is separate and may use a function resource policy.

Timeout, memory, and retry-safe work: A timeout ends an invocation that takes too long. Memory configuration also affects available compute resources, so a larger memory setting can change duration. Reused execution environments improve efficiency but are not durable storage. Repeated invocations require idempotent effects because failures or upstream retry policies can repeat an event.

CLOSE THE LOOP

Ready to complete this day?

Verify the practical steps and pass the knowledge check (at least two thirds correct).

Lab steps verifiedKnowledge check passed