INFRASTRUCTURE LEARNINGINFRASTRUCTURE LEARNING
インフラ大全インフラ大全IT INFRASTRUCTURE / LEARNING
Infrastructure learning guide
Find a lesson or lab/

Esc to close · Search terms stay on your device

LEARNING日本語

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.

What you will learn

Explain why EC2 or default Lambda may be a candidate, using OS control, duration, state, load and costs, while identifying remaining unknowns.

Prerequisite

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

Move from requirements to candidates, then evaluate the same work
Move from requirements to candidates, then evaluate the same work1: Requirements — Assess required OS/process control. — Consider EC2. 2: Requirements — Assess default Event Function conditions. — Consider Lambda. 3: Consider EC2 — Specification compatibility does not prove optimality. — Evaluate. 4: Consider Lambda — Managed infrastructure does not prove success or lowest cost. — Evaluate1234RequirementsConsider EC2Consider LambdaEvaluateMove from requirements to candidates, then evaluate the same work1: Requirements — Assess required OS/process control. — Consider EC2. 2: Requirements — Assess default Event Function conditions. — Consider Lambda. 3: Consider EC2 — Specification compatibility does not prove optimality. — Evaluate. 4: Consider Lambda — Managed infrastructure does not prove success or lowest cost. — Evaluate1234RequirementsConsider EC2Consider LambdaEvaluate
  1. Requirements → Consider EC2Assess required OS/process control.
  2. Requirements → Consider LambdaAssess default Event Function conditions.
  3. Consider EC2 → EvaluateSpecification compatibility does not prove optimality.
  4. 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.
Write a comparison brief
QuestionWhat 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

Typical EC2 virtual servers versus default Lambda Event Functions
DimensionEC2Default Lambda Event Functions
Unit of executionManage an OS and processes inside instancesInvoke a handler for an event
OS managementCustomer-managed guest-OS updates/configurationAWS manages infrastructure/managed-runtime scope; code, dependencies and access still need design
DurationDesign process/instance operating conditionsUp to 900 seconds per invocation; check configured and dependency timeouts
State/dataChoose EBS/storage and design recovery and multi-instance consistencyDo not depend on memory/tmp as durable storage or guaranteed next-invocation state
Growing demandExplicitly design type, count and optional Auto ScalingReview concurrency growth, quotas and downstream capacity
Cost factorsCompute usage/terms plus EBS, IP and transferRequests and memory/duration plus extras, logs and surrounding resources

Worked examples: short events and long jobs with OS requirements

Fictional requirements: separate candidate reasoning from remaining checks
RequirementReason to consider a candidateBefore deciding
Run a short transformation when a file arrivesConsider default Lambda if each event can complete independentlyInput size, duration, duplicates, storage, permissions and cost
An indivisible 45-minute job requires OS-specific configurationIt does not fit the default 15-minute Lambda condition; consider EC2 or alternativesContinuity, retained data, failures/restart and operations; review other options individually
Steady processing all dayEvaluate both EC2 and Lambda under the real configurationCompare throughput, configuration, applicable discounts and operations, not uptime alone
Strict response latency from the first requestCompare real startup, initialization and readiness approachesMeasure Lambda initialization/preparation options and EC2 startup/insufficient-capacity effects

Choose an option to see the result and explanation.

An indivisible job runs for 45 minutes. Can it be moved unchanged to default Lambda Event Functions without reviewing duration?
Read the answer and reason

Answer: No: review a different design or execution option · Default Lambda in this comparison has a 15-minute maximum invocation. Assess decomposition, other compute models or differently scoped Lambda options against OS and processing requirements.

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.

Questions for either model
QuestionRequired 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

Cost factors for configurations completing the same work
ModelIncludeAvoid
EC2Type, OS, Region, purchase option, runtime, EBS, IPv4, transfer and associated servicesTreating a displayed compute rate as the complete monthly cost
Default LambdaRequests, memory/duration, retries, preparation/storage extras, logs, transfer and integrationsConcluding zero system cost from idle intervals
BothSame input/work volume, failures/retries, latency criterion and operational workComparing 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.

Can you conclude “Lambda must be cheapest because requests are infrequent”?
Read the answer and reason

Answer: No: review duration, memory, surrounding resources and equivalent work · Request count is one factor. Compare execution requirements and the complete configuration; do not claim lowest cost without evidence.

Record the decision and the remaining unknowns

  1. Document input/completion, duration, OS control and storage requirements.
  2. Check the scoped EC2/default-Lambda options; assess another option under explicit conditions.
  3. Compare success, runtime, failures and downstream effects with the same load.
  4. Compare full costs and operations without presenting unmeasured estimates as observations.
  5. Record reasons, exclusions, unknowns and review triggers; design boundaries explicitly if combining models.

Check, then explain in your own words

  1. State the execution models and comparison scope.
  2. Check functionality, duration, state, failures and operational control.
  3. 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.