AWS Lambda offers a powerful serverless experience, allowing developers to run code without provisioning or managing servers. However, if you want to run Lambda-like functions on your own Linux server, AWS Firecracker provides a lightweight, open-source solution. Firecracker runs microVMs with minimal overhead, making it an excellent choice for creating a Lambda-like environment on your own infrastructure.
This guide will show you how to set up a Lambda-like environment on Ubuntu Linux using Firecracker, so you can run functions in a secure, isolated environment on any host.
curl -LOJ https://github.com/firecracker-microvm/firecracker/releases/download/v0.24.0/firecracker-v0.24.0
chmod +x firecracker-v0.24.0
sudo mv firecracker-v0.24.0 /usr/local/bin/firecracker
sudo apt update
sudo apt install -y qemu-utils
Firecracker requires a kernel image and a root filesystem to start a microVM.
curl -fsSL -o vmlinux.bin https://s3.amazonaws.com/spec.ccfc.min/img/hello/kernel/hello-vmlinux.bin
curl -fsSL -o rootfs.ext4 https://s3.amazonaws.com/spec.ccfc.min/img/hello/fsfiles/hello-rootfs.ext4
Firecracker needs a JSON configuration file for each microVM instance. Create a config.json
file:
{
"boot-source": {
"kernel_image_path": "./vmlinux.bin",
"boot_args": "console=ttyS0 reboot=k panic=1 pci=off"
},
"drives": [
{
"drive_id": "rootfs",
"path_on_host": "./rootfs.ext4",
"is_root_device": true,
"is_read_only": false
}
],
"network-interfaces": [
{
"iface_id": "eth0",
"host_dev_name": "tap0",
"allow_mmds_requests": true
}
],
"machine-config": {
"vcpu_count": 1,
"mem_size_mib": 256,
"ht_enabled": false
}
}
Note: Update
path_on_host
with the correct path if needed.
Create a simple Python function that acts like a Lambda function. For this example, it will read input and return a greeting.
mkdir ~/my_lambda_function
cd ~/my_lambda_function
lambda_function.py
with the following content:def lambda_handler(event, context):
return {
'statusCode': 200,
'body': f"Hello, {event.get('name', 'world')}!"
}
python3 -c 'from lambda_function import lambda_handler; print(lambda_handler({"name": "Ubuntu"}, None))'
mkdir rootfs
sudo debootstrap --arch=amd64 focal rootfs
sudo chroot rootfs apt install -y python3
cp lambda_function.py rootfs/home/ubuntu/
sudo dd if=/dev/zero of=rootfs.ext4 bs=1M count=64
sudo mkfs.ext4 rootfs.ext4
sudo mount rootfs.ext4 /mnt
sudo cp -a rootfs/* /mnt/
sudo umount /mnt
sudo ip tuntap add tap0 mode tap
sudo ip link set tap0 up
firecracker --config-file config.json
Test the function by running:
python3 /home/ubuntu/lambda_function.py
To trigger the function on a schedule or in response to events, you could use systemd timers or a cron job to start and stop Firecracker instances.
[Unit]
Description=Run Lambda-like function on Firecracker
[Service]
ExecStart=/usr/local/bin/firecracker --config-file /path/to/config.json
[Timer]
OnUnitActiveSec=1min
Unit=my_lambda.service
Enable and start the timer to automate function execution.
By following these steps, you can run Lambda-like functions on your own Ubuntu server with AWS Firecracker. This setup combines the flexibility of serverless architecture with the control of self-managed infrastructure, allowing you to trigger isolated functions without the complexity and cost of cloud-based services.
With Firecracker, you can achieve near-Lambda performance in any environment, making it an ideal solution for development, testing, and efficient workload management outside of AWS.