Invocation types & event sources
Lambda is invoked in three ways. Synchronous: the caller waits for the response (API Gateway, direct Invoke) — the caller handles errors/retries. Asynchronous: the caller hands off the event and Lambda queues it, retrying on failure and sending failures to a dead-letter queue or destination (S3 events, SNS, EventBridge). Event source mapping: Lambda polls a stream/queue (SQS, Kinesis, DynamoDB Streams) and invokes with batches. The invocation type determines how errors and retries behave.
- Synchronous: caller waits and handles errors (API Gateway, Invoke)
- Asynchronous: Lambda queues + retries, DLQ/destination on failure (S3, SNS, EventBridge)
- Event source mapping: Lambda polls SQS/Kinesis/DynamoDB Streams in batches
Exam tip: Async invocations retry automatically and support a DLQ/destination; synchronous ones don't — the caller owns retries.
Source: AWS docs — Lambda Developer Guide
Memory, timeout, env vars & layers
You configure a function's memory (which also proportionally allocates CPU — more memory can make CPU-bound code run faster and sometimes cheaper), a timeout (max run time before it's killed), and environment variables (config, optionally encrypted with KMS — not a place for plaintext secrets; use Secrets Manager/Parameter Store). Layers package shared libraries/runtimes separately so multiple functions reuse them and deployment packages stay small.
- Memory setting also scales CPU — tune it for CPU-bound work
- Timeout caps run time; env vars for config (encrypt secrets via KMS/Secrets Manager)
- Layers share libraries across functions and shrink packages
Exam tip: More memory = more CPU. For secrets, use Secrets Manager/Parameter Store, not plaintext env vars.
Source: AWS docs — Lambda Developer Guide
Execution role & VPC access
Every function runs with an EXECUTION ROLE — an IAM role granting exactly what it needs (e.g. write CloudWatch Logs, read a DynamoDB table). By default a function runs in an AWS-managed network with internet access. Attach it to your VPC only when it must reach private resources (an RDS instance, an internal service); then it uses ENIs in your subnets and, to reach the internet or public AWS APIs, needs a NAT gateway or VPC endpoints. VPC attachment can add cold-start and networking considerations.
- Execution role = least-privilege IAM for what the function calls
- Attach to a VPC only to reach private resources (RDS, internal services)
- VPC-attached functions need NAT/endpoints for internet + public AWS APIs
Exam tip: A Lambda reaching a private RDS must be in the VPC; if it also needs S3/DynamoDB, add VPC endpoints (or it goes via NAT).
Source: AWS docs — Lambda Developer Guide
Comparison: Lambda vs EC2/containers
Lambda vs EC2/containers: Lambda is event-driven, scales to zero, requires no server management, and bills per request + duration — ideal for spiky, short, event-driven work. EC2/containers run continuously (you manage capacity), suit long-running, stateful, or steady high-throughput workloads, and can be cheaper at sustained high volume. Choose Lambda for glue, APIs with variable load and event processing; choose containers/EC2 for long-running services, predictable heavy load, or when you need full control of the runtime/network.
- Lambda: event-driven, scales to zero, no servers, per-request billing
- EC2/containers: always-on, stateful/long-running/steady, more control
- Spiky/short/event-driven ⇒ Lambda; long-running/steady/heavy ⇒ containers/EC2
Source: AWS docs — Lambda Developer Guide