Certopolis

AWS Certified Solutions Architect – Associate

Lambda vs Fargate vs EC2: choosing compute by cost and workload

Lambda suits short, event-driven tasks of up to 15 minutes, and you pay only while code runs. Fargate runs containers without servers, billed by the CPU and memory you give each task. EC2 gives full control and the most pricing options, but you manage the instances. A Compute Savings Plan discounts all three.

AWS Lambda

Lambda is built for short tasks that keep no state between invocations. One invocation runs for up to 900 seconds (15 minutes). Memory ranges from 128 MB to 10,240 MB, and CPU scales with memory. At 1,769 MB a function has the equivalent of one vCPU.

The /tmp ephemeral storage can be set from 512 MB to 10,240 MB, separately from memory. A function that handles large files can write them to /tmp instead of holding them in memory, so you don't pay for memory it only uses as scratch space.

Work that runs longer than 15 minutes, or runs constantly at high load, is usually a better fit for containers or instances.

AWS Fargate

Fargate runs Amazon ECS tasks without provisioning, patching or scaling EC2 instances. You set CPU and memory per task, and each task gets its own isolation boundary.

Fargate Spot runs interruption-tolerant tasks on spare capacity at a discount. Tasks get a two-minute warning before they are stopped. Fargate does not replace reclaimed Spot capacity with On-Demand capacity, so the service waits until Spot capacity is available again.

Amazon EC2, and paying less for any of them

EC2 fits long-running, steady workloads, special instance types, or software that needs OS access. You pay per second On-Demand, or less with Savings Plans, Reserved Instances or Spot. In exchange, you manage the instances.

A Compute Savings Plan covers EC2, Fargate and Lambda usage, so a team can move a workload between them without losing the discount. An EC2 Instance Savings Plan covers EC2 only. Spot Instances are not covered by Savings Plans.

Savings Plans do not reserve capacity. If a workload needs guaranteed EC2 capacity in one Availability Zone, add a Capacity Reservation.

Where the exam catches people

  • Believing Lambda's temporary storage is fixed at 512 MB: /tmp can be configured up to 10,240 MB.
  • Believing large inputs force a Lambda function onto EC2: within the 15-minute limit, more /tmp and right-sized memory is often the cheaper fix.
  • Believing Fargate Spot falls back to regular Fargate on its own: Fargate does not replace Spot capacity with on-demand capacity.
  • Believing only an EC2 discount can apply to containers and functions: a Compute Savings Plan also covers Fargate and Lambda.

Written from Amazon Web Services's documentation

Try 3 questions on this

Pick an answer to see why each option is right or wrong.

  1. A Lambda function downloads 4 GB satellite images from Amazon S3 and processes them. It currently holds each image in memory, so memory is provisioned far above what the computation needs, and the per-millisecond cost reflects that. Which solution meets these requirements?

    Right. In the game that’s Renown for your Design Cost-Optimized Architectures building.

    Not that one: D is right. In the game your Design Cost-Optimized Architectures building would lose a little Renown, and this question would come back later.

  2. A company runs an interruption-tolerant Linux batch service on Amazon ECS entirely on the FARGATE_SPOT capacity provider, with all tasks in subnets of one Availability Zone. Tasks often fail to start or stop early with SpotInterruption errors. The company must keep every task on Fargate Spot pricing, must give the service more Spot capacity to draw on, and must record every interruption for later review. Which combination of steps should a solutions architect take to meet these requirements? (Select TWO.)

    Right. In the game that’s Renown for your Design Cost-Optimized Architectures building.

    Not that one: C,E is right. In the game your Design Cost-Optimized Architectures building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    That is C. A record of every interruption: "When tasks that use the Fargate and Fargate Spot capacity providers are stopped, the task state change event is sent to Amazon EventBridge. The stopped reason describes the cause", and a Spot interruption carries "stopCode": "SpotInterruption". A rule matching those events (E) records each one. C alone leaves no record, and E alone records interruptions without reducing them.

    Written from docs.aws.amazon.com/AmazonECS/latest/developerguide/spot-interruption-errors.html

  3. A company runs a steady compute baseline that moves between Amazon EC2 instance families, AWS Fargate, and AWS Lambda, and it wants a discount on that baseline. During a six-week data center cutover it also needs assured capacity for 40 m6i.xlarge instances in one Availability Zone, released afterward. The discount must also apply to those 40 instances. Which combination of steps should a solutions architect take to meet these requirements? (Select TWO.)

    Right. In the game that’s Renown for your Design Cost-Optimized Architectures building.

    Not that one: B,D is right. In the game your Design Cost-Optimized Architectures building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    Savings Plans "provide savings beyond On-Demand rates in exchange for a commitment of using a specified amount of compute power (measured per hour) for a one or three year period", and a Compute Savings Plan also covers Fargate and Lambda. It holds no capacity: "Savings Plans doesn't provide capacity reservations, but you can allocate On-Demand Capacity Reservation (ODCR) for your needs and your Savings Plans will apply." An immediate reservation has "no term commitment" and can be cancelled "to release the reserved capacity and to stop incurring charges."

    Written from docs.aws.amazon.com/savingsplans/latest/userguide/what-is-savings-plans.html