Functions handlers reference
A handler is a routine/function/method that processes specific events. Upon invoking your function, the handler is executed and returns an output.
To learn how to package a function and its dependencies in a zip file, refer to the dedicated documentation.
Handler syntax
For Python functions, the handler must be written in the <folder>/<file>.<function_name>
format.
-
If the
handle
function is defined in a single file namedhandler.py
: the handler name will behandler.handle
. -
If the
handle
function is defined in a file namedhandler.py
in a root folder: the handler will behandler.handle
. -
If the
handle
function is defined in a file namedhandler.py
within a folder (for example,myFunction/handler.py
): the handler will bemyFunction/handler.handle
.
Python handler example
def handle(event, context):
return {
"body": {
"message": 'Hello, world',
},
"statusCode": 200,
}
For Node functions, the handler must be written in the <folder>/<file>.<function_name>
format.
-
If the
handle
function is defined in a single file namedhandler.js
: the handler name will behandler.handle
. -
If the
handle
function is defined in a file namedhandler.js
in a root folder: the handler will behandler.handle
. -
If the
handle
function is defined in a file namedhandler.js
within a folder (for example,myFunction/handler.js
): the handler will bemyFunction/handler.handle
.
Node handler example
export {handle};
function handle (event, context, cb) {
return {
body: process.version,
statusCode: 200,
};
};
For PHP functions, the handler must be written in the <folder>/<file>.<function_name>
format.
-
If the
handle
function is defined in a single file namedhandler.php
: the handler name will behandler.handle
. -
If the
handle
function is defined in a file namedhandler.php
in a root folder: the handler will behandler.handle
. -
If the
handle
function is defined in a file namedhandler.php
within a folder (for example,myFunction/handler.php
): the handler will bemyFunction/handler.handle
.
PHP handler example
<?php
function handle($event, $context) {
return [
"body" => phpversion(),
"statusCode" => 200,
];
}
For Go, the handler must be written in the <folder>/<function_name>
format:
-
If the
Handle
function is defined in ahandler.go
file in a root folder: the handler name will beHandle
. -
If the
Handle
function is defined in ahandler.go
file (for example,myFunction/handler.go
): the handler will bemyFunction/Handle
Go handler example
package myfunc
import (
"encoding/json"
"net/http"
)
// Handle - Handle event
func Handle(w http.ResponseWriter, r *http.Request) {
response := map[string]interface{}{
"message": "We're all good",
}
responseBytes, err := json.Marshal(response)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
// Set the header explicitly depending the returned data
w.Header().Set("Content-Type", "application/json")
// Customize status code.
w.WriteHeader(http.StatusOK)
// Add content to the response
_, _ = w.Write(responseBytes)
}
For Rust, the handler must be written in the <folder>/<function_name>
format:
-
If the
handle
function is defined in ahandler.rs
file in a root folder: the handler name will behandle
. -
If the
handle
function is defined in ahandler.rs
file (for example,myFunction/handler.rs
): the handler will bemyFunction/handle
Rust handler example
use axum::{
body::Body, extract::Request, response::Response,
};
use http::StatusCode;
pub async fn handle(_req: Request<Body>) -> Response<Body> {
Response::builder()
.status(StatusCode::OK)
.header("Content-Type", "text/plain")
.body(Body::from("Hello world!"))
.unwrap()
}