Wondering how AWS Lambda works under the hood? You’re in the right place! To begin with, in this blog, we’ll explore Firecracker, which is the revolutionary technology powering the AWS Lambda Service. More specifically, we’ll delve into what Firecracker is and gain a deeper understanding of its core features. In addition to that, we’ll learn about MicroVMs (Micro Virtual Machines) and uncover how they contribute to the efficiency of serverless computing. Following this, we will explore how to use the Go Firecracker SDK, breaking it down into simple, actionable steps. Step by step, we’ll guide you through launching a Firecracker Virtual Machine using Go. Furthermore, we’ll show you how to set up Firecracker on your machine, ensuring you’re equipped to take full advantage of this powerful technology. With that said, are you ready to dive deep into the technology that powers AWS Lambda? If so, let’s get this fire-crackin’!
Note: Firecracker only works with Linux Machines
What is Firecracker and a Micro Virtual Machine?
Understanding Firecracker
Originally developed by AWS, Firecracker is an innovative technology. Through its thoughtful design, it prioritizes security, speed, and efficiency. As a result, it has enabled the creation and management of lightweight micro virtual machines (MicroVMs). In turn, these MicroVMs are ideal for serverless computing workloads, such as AWS Lambda, primarily because of their minimal overhead and extremely fast startup times. Moreover, this combination of features ensures optimal performance for modern, scalable applications.
MicroVMs Explained
A MicroVM is a lightweight virtual machine that combines the isolation benefits of traditional Virtual Machines with the speed and resource efficiency of containers. In contrast to conventional Virtual Machines, which can be bulky and slow to start, MicroVMs leverage technologies like the KVM or Kernel-Based Virtual Machine. Notably, this approach provides near-native performance while maintaining strong security. Furthermore, it strikes an excellent balance between efficiency and reliability, making it an ideal choice for modern workloads.
Recap for MicroVMs
- Minimal Footprint: Consume fewer resources which allows for higher density of workloads on the same hardware.
- Fast Boot Times: It can be launched in milliseconds making it perfect for bursty and scalable workloads.
- Strong Isolation: Each MicroVM created is in its isolated environment since it ensures security and stability across different workloads.
- Flexible Configuration: Highly configurable to match the specific requirements of diverse applications.
Setting Up Firecracker, Go, and the Firecracker Go SDK
To provide a hands-on experience, we will now learn how to set up Firecracker and the Go Firecracker SDK. This section ensures you are fully equipped to get started!
Prerequisites
Before we begin, ensure you have the following:
- Linux Machine: Firecracker only works on Linux Environments.
- Root Privileges: Necessary for setting up network interfaces and running firecracker.
- Basic Knowledge of Go: Familiarity with the Go programming language will be beneficial.
Step 1: Install Required Dependencies
Ensure your system meets the necessary prerequisites for Firecracker. For instance, if you’re running Ubuntu:
- Update Your System:
sudo apt-get update
sudo apt-get upgrade -y
- Install Essential Tools:
sudo apt-get install -y git wget build-essential iproute2 qemu-utls
- Load KVM Modules
sudo modprobe kvm
sudo modprobe kvm_intel
For Intel CPUssudo modprobe kvm_amd
For AMD CPUs
- Verify that KVM is Enabled:
ls /dev/kvm
It should return “dev/kvm” which means KVM is successfully loaded.
If everything is correct, it should return “dev/kvm,” indicating KVM is successfully loaded.
Step 2: Install Firecracker
First, visit the Firecracker Releases Page to find the latest version.
- Next, download the latest Firecracker Binary:
-
curl -LO https://github.com/firecracker-microvm/firecracker/releases/download/v1.10.1/firecracker-v1.10.1-x86_64.tgz
tar -xzvf firecracker-v1.10.1-x86_64.tgz
chmod +x release-v1.10.1-x86_64/firecracker-v1.10.1-x86_64
sudo mv release-v1.10.1-x86_64/firecracker-v1.10.1-x86_64 /usr/local/bin/firecracker
- Finally, verify the Installation
firecracker --version
Should return 1.10.1
Step 3: Set Up Kernel and Root Filesystem
For simplicity, this guide uses pre-built images provided by the Firecracker project. However, if desired, you can build your own kernel and root filesystem.
- Download the Kernel Image
wget https://s3.amazonaws.com/spec.ccfc.min/img/hello/kernel/hello-vmlinux.bin -O vmlinux.bin
- Create a Root Filesystem using this Bash Script
chmod +x filename.sh
Step 4: Set Up Networking
Firecracker requires a TAP network interface to provide networking capabilities to the VM.
- Create a TAP Device
sudo ip tuntap add dev fc-88-tap0 mode tap
sudo ip addr add 169.254.0.21/30 dev fc-88-tap0
sudo ip link set dev fc-88-tap0 up
sudo sysctl -w net.ipv4.conf.fc-88-tap0.proxy_arp=1
sudo sysctl -w net.ipv6.conf.fc-88-tap0.disable_ipv6=1
- Explanation:
- TAP Device:
fc-88-tap0
is the TAP device that will bridge the host and the VM’s network. - IP Addressing : Assign IP addresses in the link-local range to avoid conflicts
- Proxy ARP: Enables the host to respond to ARP requests on behalf of the VM.
- Disable IPv6: Simplifies networking by focusing on IPv4.
- TAP Device:
- Ensure the TAP Device Exists:
ip addr show fc-88-tap0
You should see the TAP device with the assigned IP address.
Step 5: Install Go
Visit the official Go Downloads Page for the Latest Go Version.
- Download and Install Go:
curl -O https://go.dev/dl/go1.23.4.linux-amd64.tar.gz
rm -rf /usr/local/go && tar -C /usr/local -xzf go1.23.4.linux-amd64.tar.gz
You may need to run this as root or through sudoexport PATH=$PATH:/usr/local/go/bin
source ~/.profile or source ~/.bashrc or ~/.zsrhc
go version
Should output “go version go1.23.4 linux/amd64”
Let’s Try!
First: Initialize a New Go Project
mkdir firecracker-demo
cd firecracker-demo
go mod init firecracker-demo
Second: Fetch the Firecracker Go SDK
go get github.com/firecracker-microvm/firecracker-go-sdk
Third: Create a Go File and Paste the Code Below
touch main.go