All Articles

Run Your Own Lambda on Your Linux Box with Firecracker: Step-by-Step Guide for Ubuntu

Introduction

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.

Prerequisites

  1. Ubuntu Linux: This guide is designed for Ubuntu 20.04 or higher.
  2. Firecracker: Firecracker must be installed to manage microVMs.
  3. Python 3.x: We’ll use Python to create a simple Lambda-like function.

Step 1: Install Firecracker

  1. Download the Firecracker binary:
   curl -LOJ https://github.com/firecracker-microvm/firecracker/releases/download/v0.24.0/firecracker-v0.24.0
  1. Make the binary executable and move it to a directory in your path:
   chmod +x firecracker-v0.24.0
   sudo mv firecracker-v0.24.0 /usr/local/bin/firecracker
  1. Install additional dependencies:
   sudo apt update
   sudo apt install -y qemu-utils

Step 2: Set Up the MicroVM Environment

Firecracker requires a kernel image and a root filesystem to start a microVM.

  1. Download the kernel image:
   curl -fsSL -o vmlinux.bin https://s3.amazonaws.com/spec.ccfc.min/img/hello/kernel/hello-vmlinux.bin
  1. Create a root filesystem:
   curl -fsSL -o rootfs.ext4 https://s3.amazonaws.com/spec.ccfc.min/img/hello/fsfiles/hello-rootfs.ext4

Step 3: Define a Firecracker Configuration

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.

Step 4: Create a Sample Lambda-like Function

Create a simple Python function that acts like a Lambda function. For this example, it will read input and return a greeting.

  1. Create a directory for the function:
   mkdir ~/my_lambda_function
   cd ~/my_lambda_function
  1. Create lambda_function.py with the following content:
def lambda_handler(event, context):
    return {
        'statusCode': 200,
        'body': f"Hello, {event.get('name', 'world')}!"
    }
  1. Test the function locally:
   python3 -c 'from lambda_function import lambda_handler; print(lambda_handler({"name": "Ubuntu"}, None))'

Step 5: Package the Function into a Root Filesystem

  1. Create a directory for the root filesystem:
   mkdir rootfs
  1. Install Python and copy the function into the root filesystem:
   sudo debootstrap --arch=amd64 focal rootfs
   sudo chroot rootfs apt install -y python3
   cp lambda_function.py rootfs/home/ubuntu/
  1. Package the root filesystem:
   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

Step 6: Launch the Firecracker MicroVM

  1. Create a tap device for network access (if needed):
   sudo ip tuntap add tap0 mode tap
   sudo ip link set tap0 up
  1. Start Firecracker:
   firecracker --config-file config.json
  1. Log in to the MicroVM:

Test the function by running:

   python3 /home/ubuntu/lambda_function.py

Step 7: Automate Execution

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.

  1. Create a systemd service:
   [Unit]
   Description=Run Lambda-like function on Firecracker

   [Service]
   ExecStart=/usr/local/bin/firecracker --config-file /path/to/config.json
  1. Create a timer to start the function periodically (e.g., every minute):
   [Timer]
   OnUnitActiveSec=1min
   Unit=my_lambda.service

Enable and start the timer to automate function execution.

Conclusion

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.

Published Nov 3, 2024

Welcome to Vians Tech