AWS Lambda is an awesome offering. You provide the code, AWS handles the infrastructure and execution for you.

But where is your code actually executed? How does AWS do it?

I recently tried to find it out for myself, and this thread is the result of what I learned!

🧵⏬
1️⃣ Thinking about Lambda

We know that we can write a script that looks like below, and then upload it to AWS while they take care of everything else.

And from simply looking at the code, it doesn't look like the usual API method we'd implement in Express.
We're exporting a function, so something else must take our code, import it, and then handle everything else. 🤔

Conclusion number one:
Something must run our code.
We also know the infamous cold start issues which became better over time but are still there. Sometimes the environment is shut down and then started again.

Conclusion number two:
Whatever runs our code can be shut down and started again.
Have you ever noticed that it's actually impossible to access anything on the host system? If not, try to, and you'll see that the environment will prevent it.

Conclusion number three:
The environment is pretty secure.

2️⃣ Thinking about the technology Lambda is based on

There are a few ways of how AWS could have implemented Lambda (taking into account its initial release year of 2014):

- Containerization
- Virtualization
- Something running on bare metal
We can quickly rule out "Something running on bare metal". AWS already had EC2 at that time and some good knowledge of virtualization.

It would not have made a lot of sense for AWS to step back from virtualization and not make use of their existing infrastructure.
They basically had everything in place to provision a virtual machine on-the-fly.

What about Containers then?
They can be spun up quickly and disposed of again.

AWS could have taken the code, wrap it with something and then put it inside a container.
This would have been a great idea, but also something completely new for AWS at that time.

Additionally, it would not explain the (old) cold start issues, because containers are usually pretty fast to spin up.
What about virtualization then?
It would make a lot of sense.

At the time of starting Lambda, AWS already had EC2 and all the infrastructure to provision a virtual machine on-the-fly. It would also explain why a lambda function being cold-started could sometimes take ...
... so long until it finally served a request. But how did they manage to reduce the cold start time up to today?

Before we dive deeper, I'll give you the answer:
Lambda has, since its release, been based on virtualization technology. No fancy containers, nothing self-developed.
It simply made the most sense for AWS to do it exactly this way. They had all the knowledge, as you've read above, and they had the infrastructure for provision. All they had to add was something to wrap user functions, and something to call them, as well as some ...
... supporting services which could handle eventing.

And now that we know that it's virtualization, we can take a look at what's exactly used nowadays!

3️⃣ Enter Firecracker

Firecracker is a virtualization technology, or better, a virtual machine monitor (VMM) developed at Amazon (now open-sourced) and written in Rust (❤️).

It's the engine powering all your Lambda functions.

firecracker-microvm.github.io
What Firecracker basically does is creating and managing a multitude of Linux Kernel-based Virtual Machines (KVMs), which are microVMs that are faster and more secure than traditional VMs.

The interesting thing about those microVMs is, that they are actually on par with...
...containers regarding memory footprint and start-up time, all while offering even more security due to the advanced features KVM offers.

You can read more about KVM at the link below.

en.wikipedia.org/wiki/Kernel-ba…
Firecracker comes with a REST API that is used to create VMs, delete them, manage them, etc.

Whenever you create a new lambda function and upload your code, the Firecracker REST-API is called under the hood to create a microVM with your function's CPU and memory settings.
AWS keeps base images that contain language/runtime specific bootstrap code.
This is the code that actually invokes your handler, passes it the request, and takes the response to return it to the caller.

This is also the code where various metrics are measured...
...that are then used to calculate your bill.

You can imagine the code as containing an infinite loop, waiting for requests, passing them to your function, returning the response, and gathering execution metrics.
After Firecracker created a new microVM, including your language-specific runtime, your code is then put into its /var/runtime/bin folder. This is the place where the bootstrap code resides, too.

Now your function is basically able to run and accept requests.
AWS will, after a while, however, shut the VM down to save resources on their side.

This is, once again, a call to the Firecracker API.

Incoming requests, e.g. through API Gateway, lead to Firecracker being tasked to start the VM again, such that it can process the request.
And that's it so far for Firecracker and its job within AWS Lambda.

4️⃣ Surrounding infrastructure and supporting services

There are of course a lot of surrounding systems and services which do their work to make AWS Lambda what it is.

There are services and systems around Firecracker that make all those requests to its API.
There are services routing the requests. There are services that decide when to call Firecracker to shut a certain VM down or pause it, and when to spin it up again.

And there are certainly a lot more services, like Queues, scheduling asynchronous messages, and much more.

5️⃣ A disclaimer and conclusion

What I wrote here is only based on what I was able to find out from the outside (not working at Amazon and not personally knowing anyone working at AWS).

If an Amazon engineer comes across this, they might laugh out loud, and ...
think that I got so many things wrong. So take what you read here with a bit of healthy scepticism!

I, however, tried my very best to satisfy my own curiosity and find out as much as I could about Lambda's runtime environment, as it interested me a lot.
Especially the improvements of cold-starts within recent months and years, made me initially guess that AWS made the switch from virtualization to containerization.

But as you see, I was wrong and stand corrected. I would not have guessed that using KVM was an ...
... appropriate answer to tackle those issues.

It's pretty interesting to see what way AWS has gone to make Lambda what it is today, and even more interesting to see Firecracker and how it solves a lot of problems serverless functions cause for service providers.
And that's it. This thread's over.

I hope you learned something and find it at least a little interesting!

🙋🏻‍♂️🙋🏻‍♂️🙋🏻‍♂️

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Oliver Jumpertz

Oliver Jumpertz Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @oliverjumpertz

19 Dec
AWS Lambda recently added container support.

We're going to look into what this offer actually includes and how we could build a containerized Lambda function ourselves. 👨🏻‍🏫

Start your IDEs and open your AWS consoles because we're going in!

🧵⏬
1️⃣ AWS Lambda Previous To Container Image Support

Until recently, AWS Lambda only allowed you to write some code and upload a zip-archive containing all files necessary to run your function.

Some frameworks eased up this process, but that's how it went.
Lambda also imposed some pretty strict limits, especially the 50MB default deployment size limit.

You could circumvent this limit, technically, by pulling more deployment dependencies from S3 or by issuing an AWS Service Limits support request. But this wasn't the most...
Read 32 tweets
17 Dec
Five Node.js web frameworks you could use to build your next API!

🧵⏬ Five Node.js web frameworks you could use to build your next
1️⃣ express

This is the classic and by far the most popular Node.js web framework.
It's fast, has a minimalistic approach, and doesn't get in your way.

You'll find the most tutorials for express, but it's still relatively easy to learn.

github.com/expressjs/expr…
2️⃣ koa

koa is quite similar to express, but tries to give you more freedom and less complexity.

It has a middleware stack that flows in a stack-like manner, allowing you to perform actions downstream then filter and manipulate the response upstream.

github.com/koajs/koa
Read 6 tweets
17 Dec
Here are three free ebooks to help you with leveling up your JavaScript skills!

🧵⏬
1⃣ Eloquent JavaScript

Eloquent JavaScript is pretty good at giving you a lot of examples of how to not use JavaScript.

If you try to improve your overall skills and learn how to actually use JS, I can recommend this one!

⛓️ eloquentjavascript.net
2⃣ You Don't Know JS

This is the first (complete) installment of the series, with a second one on the way.

If you're looking for a great book to dive into JS, this one's definitely for you.

⛓️ github.com/getify/You-Don…

⛓️ github.com/getify/You-Don…
Read 4 tweets
14 Dec
What actually is a Pod in Kubernetes?

If you ever read at least a little about Kubernetes or followed a simple tutorial, you must have come across the term "Pod".

In case you're still wondering what it is, this thread is for you!

🧵⏬
1️⃣ What is it?

A Pod is the smallest deployable unit in Kubernetes.

It's a group of one or more containers that form a logical local host. They share their storage, their network, are always co-located and co-scheduled.
The most common use case is having a Pod with exactly one container. Having multiple containers within a Pod is usually a pretty advanced use-case.

So, naively spoken, a Pod is often only a wrapper around one container.
Read 20 tweets
12 Dec
Kubernetes vs Serverless offerings

Why would you need Kubernetes when there are offerings like Vercel, Netlify, or AWS Lambda/Amplify that basically manage everything for you and offer even more?

Well, let's try to look at both approaches and draw our own conclusions!

🧵⏬
1️⃣ A quick look at Kubernetes

Kubernetes is a container orchestrator and thus needs containers to begin with. It's a paradigm shift to more traditional software development, where components are developed, and then deployed to bare metal machines or VMs.
There are additional steps now: Making sure your application is suited to be containerized (12-factor apps, I look at you: 12factor.net), containerizing the application, following some pretty well-proven standards, and then pushing the image to a registry.
Read 35 tweets
11 Dec
A gentle introduction to Kubernetes (short: K8s)

You might have heard the name Kubernetes already and can't really imagine what it is, or you've already heard about it but still need a little more info.

No matter what your background is, this thread is for you!

🧵⏬
1️⃣ What is Kubernetes?

Kubernetes is an open source container orchestration platform. It basically handles everything related to deploying, managing and scaling containerized applications.

Its name originates from Greek, meaning something like pilot.
You might have already worked with Docker and created a few containers, started them, and then used it to deploy a database to test your app locally.

And now imagine that you want to take your app, containerize it, and deploy it to a server.
Read 25 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!