Getting Started with Mayfly
View SourceThis guide will walk you through creating your first Elixir Lambda function using Mayfly.
Prerequisites
- Elixir ~> 1.15 installed
- Mix build tool
- AWS Account with Lambda access
- AWS CLI configured (optional, for deployment)
Installation
Add Mayfly to your project's dependencies in mix.exs:
def deps do
[
{:mayfly, github: "bmalum/mayfly"}
]
endThen fetch the dependencies:
mix deps.get
Creating Your First Lambda Function
1. Define Your Handler
Create a new module with a handler function. The handler receives a map (the Lambda event) and returns {:ok, result} or {:error, reason}:
defmodule MyApp.HelloHandler do
@moduledoc """
A simple Lambda function that greets the world.
"""
def handle(event) do
name = Map.get(event, "name", "World")
{:ok, %{
message: "Hello, #{name}!",
timestamp: DateTime.utc_now() |> DateTime.to_iso8601()
}}
end
end2. Build Your Lambda Package
Use the provided Mix task to build a deployment package:
mix lambda.build --zip
This will:
- Build a release in the
lambdaenvironment - Generate a
bootstrapscript - Create a
lambda.zipfile ready for deployment
3. Deploy to AWS Lambda
Using AWS Console
- Go to the AWS Lambda Console
- Click "Create function"
- Choose "Author from scratch"
- Configure:
- Function name:
my-elixir-function - Runtime: Custom runtime on Amazon Linux 2023
- Architecture: x86_64 (or arm64 if you built with Docker)
- Function name:
- Click "Create function"
- In the "Code" section, click "Upload from" → ".zip file"
- Upload your
lambda.zipfile - In "Runtime settings", click "Edit" and set:
- Handler:
Elixir.MyApp.HelloHandler.handle
- Handler:
- Click "Save"
Using AWS CLI
# Create the function
aws lambda create-function \
--function-name my-elixir-function \
--runtime provided.al2023 \
--role arn:aws:iam::YOUR_ACCOUNT_ID:role/lambda-execution-role \
--handler Elixir.MyApp.HelloHandler.handle \
--zip-file fileb://lambda.zip \
--timeout 30 \
--memory-size 512
# Update existing function
aws lambda update-function-code \
--function-name my-elixir-function \
--zip-file fileb://lambda.zip
4. Test Your Function
In the AWS Lambda Console:
- Click the "Test" tab
- Create a new test event:
{ "name": "Elixir Developer" } - Click "Test"
You should see a response like:
{
"message": "Hello, Elixir Developer!",
"timestamp": "2025-11-21T10:30:00Z"
}Handler Patterns
Basic Handler
def handle(event) do
{:ok, %{result: "success"}}
endWith Error Handling
def handle(event) do
case validate(event) do
:ok ->
result = process(event)
{:ok, result}
{:error, reason} ->
{:error, reason}
end
endWith Pattern Matching
def handle(%{"action" => "create"} = event) do
# Handle create action
{:ok, %{status: "created"}}
end
def handle(%{"action" => "delete"} = event) do
# Handle delete action
{:ok, %{status: "deleted"}}
end
def handle(_event) do
{:error, "Unknown action"}
endEnvironment Variables
Configure your handler using the _HANDLER environment variable in Lambda:
_HANDLER=Elixir.MyApp.HelloHandler.handleThe format is: Elixir.ModuleName.function_name
Next Steps
- Learn about Deployment Strategies
- Explore API Gateway Integration
- See Error Handling Best Practices