Topic lessonTopic: Lambda and API Gateway: inputs, testing and failure investigation
EC2 vs Lambda: compare execution time, operational control and cost factors
Choose between EC2 and Lambda by workload shape, required control and execution conditions. This comparison focuses on typical EC2 virtual servers and default Lambda Event Functions. Neither is universally cheaper or faster: shortlist against requirements and compare completion of the same work.
Explain why EC2 or default Lambda may be a candidate, using OS control, duration, state, load and costs, while identifying remaining unknowns.
Know EC2 instances/AMIs/EBS and Lambda events/handlers/execution roles. Reading needs no AWS account; no real resources or price estimates are created.
Establish the workload before selecting a service
- Requirements → Consider EC2Assess required OS/process control.
- Requirements → Consider LambdaAssess default Event Function conditions.
- Consider EC2 → EvaluateSpecification compatibility does not prove optimality.
- Consider Lambda → EvaluateManaged infrastructure does not prove success or lowest cost.
- Requirements
- Input, completion, duration, OS, state and demand.
- Consider EC2
- Review OS control and long-running processes.
- Consider Lambda
- Review completion within an event-driven invocation.
- Evaluate
- Compare behavior, failures, performance, operations and cost.
| Question | What to record |
|---|---|
| What must complete? | Input, output, success criterion and failure handling |
| When and how much runs? | Average/peak demand, duration, concurrency and latency tolerance |
| What control is required? | OS, libraries, execution format, networking and destinations |
| What must persist? | Durable data, temporary files and duplicate-sensitive effects |
| What is being compared? | Whole-configuration cost and operations; record unmeasured factors explicitly |
Compare the management unit and execution requirements
| Dimension | EC2 | Default Lambda Event Functions |
|---|---|---|
| Unit of execution | Manage an OS and processes inside instances | Invoke a handler for an event |
| OS management | Customer-managed guest-OS updates/configuration | AWS manages infrastructure/managed-runtime scope; code, dependencies and access still need design |
| Duration | Design process/instance operating conditions | Up to 900 seconds per invocation; check configured and dependency timeouts |
| State/data | Choose EBS/storage and design recovery and multi-instance consistency | Do not depend on memory/tmp as durable storage or guaranteed next-invocation state |
| Growing demand | Explicitly design type, count and optional Auto Scaling | Review concurrency growth, quotas and downstream capacity |
| Cost factors | Compute usage/terms plus EBS, IP and transfer | Requests and memory/duration plus extras, logs and surrounding resources |
Worked examples: short events and long jobs with OS requirements
| Requirement | Reason to consider a candidate | Before deciding |
|---|---|---|
| Run a short transformation when a file arrives | Consider default Lambda if each event can complete independently | Input size, duration, duplicates, storage, permissions and cost |
| An indivisible 45-minute job requires OS-specific configuration | It does not fit the default 15-minute Lambda condition; consider EC2 or alternatives | Continuity, retained data, failures/restart and operations; review other options individually |
| Steady processing all day | Evaluate both EC2 and Lambda under the real configuration | Compare throughput, configuration, applicable discounts and operations, not uptime alone |
| Strict response latency from the first request | Compare real startup, initialization and readiness approaches | Measure Lambda initialization/preparation options and EC2 startup/insufficient-capacity effects |
Choose an option to see the result and explanation.
Both choices require failure and data design
With EC2, separate the executing process from persistent storage and review stop/termination behavior. With Lambda, do not rely on reused environments for durable state, and design retries to avoid duplicate effects. Changing the compute model does not automatically implement business-data retention or recovery.
| Question | Required explanation |
|---|---|
| What if processing stops midway? | Establish completed work and define restart/retry and duplicate handling |
| What if access is denied? | Identify caller, action, target and applicable permissions/network conditions |
| What if downstream services are overloaded? | Review DB/API capacity and admission, waiting and failure policies |
| What happens at end of use? | Reconcile compute, storage, configuration and related resources during cleanup |
Neither hourly rate nor “serverless” proves cost or latency
| Model | Include | Avoid |
|---|---|---|
| EC2 | Type, OS, Region, purchase option, runtime, EBS, IPv4, transfer and associated services | Treating a displayed compute rate as the complete monthly cost |
| Default Lambda | Requests, memory/duration, retries, preparation/storage extras, logs, transfer and integrations | Concluding zero system cost from idle intervals |
| Both | Same input/work volume, failures/retries, latency criterion and operational work | Comparing different work or quality requirements |
Lambda initialization and reused-environment processing have different latency conditions. EC2 also has launch, app-readiness and capacity constraints. Measure required conditions rather than concluding that Lambda is always slow or EC2 always responds immediately.
Choose an option to see the result and explanation.
Record the decision and the remaining unknowns
- Document input/completion, duration, OS control and storage requirements.
- Check the scoped EC2/default-Lambda options; assess another option under explicit conditions.
- Compare success, runtime, failures and downstream effects with the same load.
- Compare full costs and operations without presenting unmeasured estimates as observations.
- Record reasons, exclusions, unknowns and review triggers; design boundaries explicitly if combining models.
Check, then explain in your own words
- State the execution models and comparison scope.
- Check functionality, duration, state, failures and operational control.
- Compare performance and cost for configurations completing the same work.
Check your understanding: What should be established first when comparing EC2 and Lambda?
The input, completion criterion and requirements for duration, OS control, state and demand.
Choosing a name first can hide requirements and invalidate cost comparisons. Establish equivalent requirements, shortlist candidates and report unknowns accurately.
Sources and conditions checked
The official references used in this lesson are collected here with their scope, conditions and corresponding sections. Independent expert review and live AWS comparison have not been performed.
- What is Amazon EC2?Checked:
Scope and conditionsEC2 instances are virtual servers. Architecture and operating-system responsibilities depend on the chosen configuration.
Used in this lessonCompare the management unit and execution requirements
- AWS shared responsibility modelChecked:
Scope and conditionsResponsibilities vary by selected service and configuration
Used in this lessonCompare the management unit and execution requirements
- What is AWS Lambda?Checked:
Scope and conditionsCurrent Event Functions/Web Functions/MicroVMs distinction. Teaching is scoped to default Event Functions, not every compute primitive or extension.
Used in this lessonCompare the management unit and execution requirements
- Lambda quotasChecked:
Scope and conditionsDefault Event Function 900-second invocation timeout and account/Region concurrency conditions. Explicitly excludes other Managed Instances/Durable/Web/MicroVM conditions.
Used in this lessonCompare the management unit and execution requirements Worked examples: short events and long jobs with OS requirements
Scope and conditionsDefault environment reuse, cold initialization and transient memory/tmp, with different Managed Instances/Durable lifecycles excluded.
Used in this lessonCompare the management unit and execution requirements Both choices require failure and data design Neither hourly rate nor “serverless” proves cost or latency
- What is Amazon EC2 Auto Scaling?Checked:
Scope and conditionsGroup management, maintaining desired capacity, configured demand scaling and separate request distribution. Not automatic application availability.
Used in this lessonCompare the management unit and execution requirements
- Amazon EC2 On-Demand pricingChecked:
Scope and conditionsCompute assumptions, OS-specific billing granularity, data transfer and additional resource charges; no unit rates embedded.
Used in this lessonCompare the management unit and execution requirements
- AWS Lambda pricingChecked:
Scope and conditionsDefault Event Functions request/memory-duration factors and conditional extras; separate pricing of other compute options. No numeric estimate or always-cheaper claim.
Used in this lessonCompare the management unit and execution requirements
- EC2 instance state changesChecked:
Scope and conditionsEBS-backed stop/start, hibernation and termination differ
Used in this lessonBoth choices require failure and data design
Scope and conditionsData loss and persistence; DeleteOnTermination is evaluated for each EBS volume, not assumed from instance state.
Used in this lessonBoth choices require failure and data design
Scope and conditionsDirect caller handling versus async/source retries and idempotent business effects. No exactly-once guarantee.
Used in this lessonBoth choices require failure and data design
Scope and conditionsNew-instance metric aggregation and capacity/scaling treatment; distinguish grace/defaults/fallbacks and actual application readiness.
Used in this lessonNeither hourly rate nor “serverless” proves cost or latency