How to make fivem run on serverless?

How to make fivem run on serverless?
hope

What?

What exactly do you mean by serverless?

:innocent: Amen

serverless is an new way

you can see this

may be it’s a good way to solve money

You don’t

2 Likes

why?Can’t it be realized?

1 Like

Because FiveM servers require state, you can’t run them like serverless with bunch of small services

The closes thing, that you would be able to do, would be to use your server as sort of a proxy, to relay request to your lambda functions. This would result in all the compute happening in the lambda function.

I did an experiment on this, where i created a whitelist script that used a lambda function and dynamodb

Did you succeed?

Yeah it worked out fine
Currently it was checking if a players steam id was whitelisted, and then it would edit his connection state in the dynamodb.

But again this was just a PoC i did, i dont have any api key on my api gateway or anything to protect it

Do you have a Demo for your reference? Or how do you do it?

This is what I did:
Created a Lambda function
Created an API Gateway with an ANY method, where the integration request is LAMBDA_PROXY
Created a DynamoDB table called user_data, which has 2 fields (steamID and connected)

I then have the script on my fivem server called “serverless_test”

Code for the different parts:
serverless_test/client/main.lua:

Citizen.CreateThread(function()
    while true do
        Citizen.Wait(0)
        if NetworkIsSessionStarted() then
            TriggerServerEvent("IsConnected")
            return
        end
    end
end)

serverless_test/server/main.lua:

AddEventHandler('playerConnecting', function(name, setCallback, deferrals)
    local _source = source
    deferrals.defer()
    local _source = source
    deferrals.update('Checking if you are allowed . . .')
    PerformHttpRequest(YOUR API GATEWAY ENDPOINT HERE, function (errorCode, resultData, resultHeaders)
        if resultData then
            deferrals.done()
        end
    end, 'POST', json.encode({data = {method = 'checkWhitelist', steamID = GetPlayerIdentifiers(_source)[1]}}))
end)

AddEventHandler('playerDropped', function(reason)
    local _source = source
    PerformHttpRequest(YOUR API GATEWAY ENDPOINT HERE, function (errorCode, resultData, resultHeaders)
    end, 'POST', json.encode({data = {method = 'connection', steamID = GetPlayerIdentifiers(_source)[1], connected = false}}))
end)

RegisterNetEvent('IsConnected')
AddEventHandler('IsConnected', function()
    local _source = source
    PerformHttpRequest(YOUR API GATEWAY ENDPOINT HERE, function (errorCode, resultData, resultHeaders)
    end, 'POST', json.encode({data = {method = 'connection', steamID = GetPlayerIdentifiers(_source)[1], connected = true}}))
end)

Lambda code:

import json
import boto3
from boto3.dynamodb.conditions import Key

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('user_data')

def get_whitelist(steamID):
    resp = table.query(
        KeyConditionExpression=Key('steamID').eq(steamID)
        )
    if 'Items' in resp:
        return True
    else:
        return False

def set_connection(steamID, connection):
    
    table.update_item(
            Key = {
                'steamID': steamID
            },
            UpdateExpression='set connected = :v',
            ExpressionAttributeValues={
                ':v': connection´´
            },
            ReturnValues='UPDATED_NEW'
        )

def lambda_handler(event, context):
    print(event)
    payload = json.loads(event['body'])['data']
    if payload['method'] == 'checkWhitelist':
        output = get_whitelist(payload['steamID'])
    if payload['method'] == 'connection':
        output = set_connection(payload['steamID'],payload['connected'])
        
    return {
        'statusCode': 200,
        'body': json.dumps(output)
    }