Course contents · Lesson 5 of 19

Cloud Compute Explained: VMs, Containers, Functions

Core Services · Lesson 5 of 19 · 10 min Engineer

Your team has built a small web shop, plus a script that shrinks each product photo someone uploads. Now you must decide where that code runs in the cloud: on a virtual machine, in a container or as a function. This lesson explains cloud compute, the rented processing power and memory that runs your code, in those three forms: how each works, how it is billed and how to choose.

Virtual machines: a whole computer of your own

As you saw in lesson 1, a virtual machine (VM) is a computer made of software: a hypervisor splits a physical server into several, and each VM runs its own operating system. On AWS, a VM in Amazon EC2 is called an instance.

To launch one, you make four choices. The image, on AWS an Amazon Machine Image (AMI), holds the operating system and software to boot from. The instance type sets the size: how many vCPUs (virtual processors, each a share of a physical processor) and how much memory.

The disk is the root volume the OS boots from, copied from the image. On AWS it is an EBS (Elastic Block Store) volume; block storage comes in the next lesson. The network choice puts the instance in a subnet, a slice of your private network inside one availability zone, with a security group as its firewall. The later VPC lesson covers both.

The diagram follows one instance from choices to states; notice where the meter starts.

Four steps in a row: image (AMI) with the OS and software to boot from, such as Ubuntu 24.04; instance type, such as m6i.large with 2 vCPU and 8 GiB; disk, a root EBS volume copied from the image; network, a subnet in one availability zone plus a security group. Then launch leads to pending, not billed yet, then running, billed per second with a one-minute minimum, then stop leads to stopped, with no instance charge but the disk still billed
The bill starts at running, not at launch, and a stopped instance still pays for its disk.

A new instance is pending while it gets ready on a host, then running. From running, EC2 bills every second, with a one-minute minimum each time it starts, busy or idle, like flat rent. A stopped instance has no compute charge, but its disk is still billed.

Containers: your app packed with what it needs

You met containers in lesson 4 as apps packaged with everything they need. That package is a container image: your app, libraries and settings, built once and run unchanged on a laptop or a cloud server.

The big difference from a VM is the kernel, the core of the operating system that controls the processor, memory and devices. Each VM boots its own kernel. Containers on one host all share the host's kernel, and each runs as an isolated process. The Kubernetes documentation calls this relaxed isolation, and it is what makes containers lightweight.

With no OS to boot, a container starts in seconds, and one host can pack many of them. The costs: isolation is weaker than between VMs, and a Linux container needs a Linux kernel underneath.

The diagram puts all three side by side. Compare what each box carries: a VM brings a whole guest OS, a container only its app and libraries, and a function only your code.

Three stacks on physical servers. Virtual machine: a hypervisor carries two VMs, each with its own guest OS and kernel, libraries and app; it boots its own OS in minutes. Container: a host OS with one shared kernel and a container engine such as Docker carry three containers holding only app and libraries; it starts as a process in seconds. Function: the provider's servers, OS and runtime are managed and hidden; an event calls your function, which runs and stops. A note says the container host is usually itself a VM
Only the VM carries its own operating system; containers share one kernel, and a function brings nothing but code.

In the cloud, a container service starts, restarts and spreads your containers across hosts. AWS has Amazon ECS (Elastic Container Service) and Amazon EKS, the managed Kubernetes from lesson 4. ECS can place containers on EC2 instances that you manage, or on AWS Fargate, which runs the servers for you, pay-as-you-go. Google Cloud Run runs a container and by default can scale down to zero copies when no requests arrive.

Those EC2 hosts are VMs, as the diagram's footnote says: in the cloud, containers usually run on top of VMs, not instead of them.

Functions: code that runs only for the trip

A function is a small piece of code that the provider runs when an event calls it, such as a web request, a new file or a timer, as you saw in lesson 2. You upload only the code; the servers, OS and language runtime stay hidden.

The bill follows each run: AWS Lambda charges per request and for running time, so idle hours cost nothing. Each run is also capped, like a taxi ride: a Lambda function may run for at most 1,800 seconds (30 minutes). Azure Functions and Cloud Run functions follow the same idea. Cold starts and event sources come later in What Is Serverless?

Choosing: what am I running?

Start from the job, not the technology. The flowchart asks two questions, and most paths end in the middle.

Flowchart. Start: what am I running? Does it need its own OS, drivers or a legacy setup? Yes leads to a virtual machine, for full control and legacy apps, such as EC2, Azure VMs or Compute Engine. No leads to: a short job, started by an event? Yes leads to a function, for short event-driven work, such as Lambda, Azure Functions or Cloud Run functions. No, a long-running packaged service, leads to a container, for most modern apps, such as ECS, EKS or Cloud Run
A VM only when you need the OS itself, a function only for short event-driven jobs, and a container for the rest.

Choose a VM for full control: a specific OS version, special drivers, or legacy software, older software built to own a whole server.

Choose a container for most modern apps, such as a web shop or an API (an interface that other programs call) that runs all day. They start fast and pack densely.

Choose a function for short event-driven work, such as the photo-shrinking script. A job that runs for hours, or a busy service that never idles, fits a container better. Many systems mix all three.

Instance families and pricing in brief

A VM, or a host for your containers, needs an instance type. AWS groups types into families by their mix of vCPUs and memory. The name is a code: in c5n.xlarge, c is the family (compute), 5 the generation, n an extra capability (faster networking) and xlarge the size.

The table compares the large size of three families: the same vCPUs, and double the memory on each row.

FamilyExamplevCPUMemorySuits
Compute optimised (c)c6i.large24 GiBCPU-heavy work, such as batch jobs
General purpose (m)m6i.large28 GiBBalanced work, such as web servers
Memory optimised (r)r6i.large216 GiBLarge data held in memory, such as caches

Azure uses other letters: D-series for general purpose, F-series for compute optimised, E-series for memory optimised. You also choose how to pay:

  • On-demand (the default): pay per second while running, with no commitment.
  • Reserved Instances and Savings Plans: commit to one or three years of use for a discount. A Reserved Instance is a billing discount, not a physical machine.
  • Spot Instances: spare EC2 capacity at a steep discount, but EC2 can take it back after a two-minute warning, so use it only for work that can stop and resume.

A common pattern: commit for the load you always run, use on-demand for the rest, and spot for interruptible batch jobs. Cost habits come later in Cloud Cost Basics.

Check yourself

A vendor's reporting tool needs one Windows Server version with a special driver installed, and runs all day. VM, container or function?

A VM: only a VM gives the tool its own operating system to install the driver in, while containers share the host's kernel and functions stop after a short run.

On the same hardware, why does a container start in seconds when a VM takes minutes?

The VM must boot a whole guest OS from its image, while the container starts as a process on a kernel that is already running.

A nightly job runs for three hours on 20 instances and can resume where it stopped. Which pricing model fits, and what is the risk?

Spot: the steep discount suits it, and if EC2 reclaims an instance after its two-minute warning, the job resumes later; a one- or three-year commitment would pay for 21 idle hours a day.