Free tech learning for everyone, from basics to pro

Cloud FundamentalsCloud SecurityBeginner

Compute options: virtual machines, containers and serverless compared

In short: Virtual machines give full control but the most work, containers package apps for portable, efficient hosting, and serverless functions run code on demand with no servers to manage. Each shifts a different share of security work between you and the provider.

Three ways to run code

Every cloud application needs somewhere to run. The three main options differ in how much of the stack you look after. A good mental model: the less you manage, the less you can misconfigure, but the less you can customise.

What you manageVirtual machineOS, runtime, app, dataContainersimage, app, dataServerless functioncode and data only
Figure 1. Moving from VMs to containers to serverless, the provider takes over more of the stack and you manage less.

How they compare

AspectVirtual machinesContainersServerless functions
ExamplesAmazon EC2, Azure VMs, Compute EngineAmazon ECS/EKS, Azure Container Apps/AKS, GKEAWS Lambda, Azure Functions, Cloud Run functions
Start timeMinutesSecondsMilliseconds to seconds
BillingPer second or hour while runningPer node, or per task resourcesPer request and execution time
ScalingScale sets / auto scaling groupsOrchestrator scales pods or tasksAutomatic, per event
You patchOS, runtime, appBase image and app (host too if self-managed)Your code and its libraries
LimitsFewNeeds orchestration skillsTime limits, cold starts, stateless

A container packages an application with its libraries but shares the host's kernel, so it is lighter than a VM. Serverless still runs on servers; you just never see them. The provider runs your function when an event arrives, such as an HTTP request, a file upload or a queue message.

Security responsibilities

  • VMs: you harden and patch the operating system, manage SSH or RDP access, run endpoint protection, and control the network.
  • Containers: scan images for vulnerabilities, use small trusted base images, avoid running as root, and lock down the orchestrator (for example Kubernetes role-based access control).
  • Serverless: give each function its own least-privilege role, validate every input, keep secrets in a vault rather than environment variables in plain text, and watch dependencies.

Identity becomes more important as you move right. A function with an over-powerful role is a tempting target, because a single injection flaw can hand an attacker that role's permissions.

Choosing for a workload

Choosing computeVirtual machinelegacy apps, full controlContainersmicroservices, portabilityServerlessevents, spiky loadManaged K8smany containersContainer servicesimple container hostingBatchbig scheduled jobs
Figure 2. Common compute services and the kinds of workload each suits best.
  1. Does the software need a specific OS or kernel, or a licence tied to a machine? Use a VM.
  2. Is it a modern web service that you want to move between environments? Use containers.
  3. Is it short, event-driven code with unpredictable traffic? Try serverless.
  4. Check run time: long jobs may exceed serverless time limits.
  5. Check your team's skills; a well-run VM beats a badly run Kubernetes cluster.

Tip: many real systems mix all three, for example a container-based API, serverless functions for image resizing, and a VM for a legacy reporting tool.

Practice

Which option leaves you responsible for patching the guest operating system?
Virtual machines. With containers you patch the image; with serverless the provider manages the OS and runtime.
Why is a least-privilege role especially important for a serverless function?
If the function has a flaw, an attacker inherits its role; a narrow role limits what they can reach.
A nightly job runs for three hours. Is a serverless function a good fit?
Usually not, because functions have maximum run times; a container task, batch service or VM is better.

Key takeaways

  • VMs give the most control and the most patching work.
  • Containers share the host kernel and package apps portably.
  • Serverless runs code per event, billed per request and duration.
  • Moving towards serverless shifts more security work to the provider.
  • Least-privilege identity matters at every level.
Tracks your learning on this device only. See your progress on the learning paths.

About the author

Mohamed Fayas

Founder of FM TecHub. CCNA certified, with a BTEC HND in Network Engineering, and BEng (Hons) in Computer Networking and Cloud Security. He writes practical, plain-English guides on networking, security and cloud.

More about the author

Original content written by Mohamed Fayas for FM TecHub. © 2026 FM TecHub. You're welcome to share a link to this page; please don't copy the text elsewhere without permission.

Stay up to date by email

New notes, tools and security news from FM TecHub, explained simply. Free.

We only use your email to send these updates. See our privacy policy.