Functions Microservice
How to deploy, configure, and use the Prisme.ai Functions Microservice for running custom NodeJS and Python code in your applications
The Functions Microservice enables you to execute custom code (NodeJS or Python) within your Prisme.ai applications. This powerful capability allows you to extend the platform with custom logic, integrate with external systems, and implement complex business processes.
Overview
The Functions Microservice provides a secure, isolated environment for running custom code within the Prisme.ai platform. It supports:
- NodeJS Functions: JavaScript code execution with access to selected built-in modules
- Python Functions: Python code execution with standard library access
- Shared Execution Context: Functions within the same workspace can call each other
- Memory Management: Configurable resource limits and garbage collection
- Dependency Management: Automatic installation of required packages
The Functions Microservice is designed with security in mind, running code in isolated environments with configurable resource limits to prevent abuse or resource exhaustion.
Installation Prerequisites
Before deploying the Functions Microservice, ensure you have:
NPM Registry Access
The service must have access to a npm registry for installing JavaScript dependencies. You can use the NPM_CONFIG_REGISTRY
environment variable to specify your own registry.
Storage Volume
The service requires a volume for storing code, dependencies, and execution data. We recommend using volumes with high I/O performance as they may experience heavy usage when installing dependencies.
Configuration
Environment Variables
Configure the Functions Microservice with the following environment variables:
Python Configuration
Python Configuration
Variable Name | Description | Default Value |
---|---|---|
PYTHON_FUNCTIONS_RUN_TIMEOUT | Python functions execution timeout in milliseconds | 20000 |
PYTHON_API_URL | Python API URL | http://localhost:8000 |
NodeJS Configuration
NodeJS Configuration
Variable Name | Description | Default Value |
---|---|---|
PORT | HTTP port for the service | 4000 |
FUNCTIONS_STORAGE_FILESYSTEM_DIRPATH | Directory path for function storage | data/functions/ |
FUNCTIONS_RUN_TIMEOUT | Functions execution timeout in milliseconds | 20000 |
FUNCTIONS_WORKERS_MAX_LRU | Maximum number of function workers kept in memory | 500 |
NODE_BUILTIN_MODULES | Allowed Node.js built-in modules | http, https, url, util, zlib, dns, stream, buffer, crypto |
NPM_CONFIG_REGISTRY | NPM registry URL | https://registry.npmjs.org/ |
NODE_WORKER_MAX_OLD_GENERATION_SIZE_MB | NodeJS function worker maxOldGenerationSizeMb | 100 |
INACTIVE_NODE_WORKER_DELETION_TIMEOUT | Inactive period in seconds after which node workers are automatically terminated | 3600 |
UPDATE_SCRIPTS_ON_STARTUP | If set to “yes” or “true”, ensures function scripts are in-sync with corresponding workspace.yaml files on startup | no |
REQUEST_MAX_SIZE | Maximum request body size (format from bodyParser.json) | 1mb |
Resource Considerations
When deploying the Functions Microservice, consider the following resource recommendations:
Memory Allocation
Allocate sufficient memory based on your expected workload:
- Minimum: 1GB
- Recommended: 2GB or more for production environments
- Consider additional memory if functions will process large datasets
CPU Resources
Ensure adequate CPU resources:
- Minimum: 0.5 vCPU
- Recommended: 1 vCPU or more for production environments
- Functions with complex calculations may benefit from additional CPU
Disk Space
Plan for storage requirements:
- Minimum: 1GB
- Recommended: 5GB or more for environments with many dependencies
- Consider storage needs for dependency caching and function code
Network Configuration
Ensure proper network setup:
- Accessible by other microservices (especially API Gateway)
- Access to required external resources (NPM registry, etc.)
- Consider network policies for security
Microservice Testing
After deploying the Functions Microservice, verify its operation with these simple tests:
Create a Test Function
Create a function workspace named test
containing a hello
function:
If successful, the same body should be returned in the response.
Execute the Function
Run the function with a test parameter:
If successful, you should receive a response like:
Verify Resource Limits
Test that resource limits are enforced by creating a function that attempts to use excessive resources:
When executing this function, it should terminate due to resource limits or timeout:
If all tests pass, congratulations! Your Functions Microservice is up and running correctly.
Advanced Features
The Functions Microservice includes several advanced capabilities that enhance its flexibility and power:
Asynchronous Functions
Asynchronous Functions
All functions are executed as asynchronous operations, even if they don’t explicitly use async calls. This means:
- Every function is treated as if it had the
async
keyword - When calling functions from the same workspace, you must use the
await
keyword - Results are always returned as promises
Example:
Shared Execution Context
Shared Execution Context
Shared Memory Cache
Shared Memory Cache
Dependency Management
Dependency Management
Functions can use external dependencies by specifying them in the function definition:
- NodeJS functions can use NPM packages, or those from the internal company registry if configured. Dependencies are automatically installed when the function is created or updated.
- Python functions can use PyPI packages that have been whitelisted by the DevSecOps teams.
Example NodeJS function with dependencies:
Integration with Prisme.ai
The Functions Microservice integrates with other Prisme.ai components:
AI Builder
Within AI Builder, you can create custom automations that execute functions, allowing you to extend workflows with custom logic.
AI Knowledge
Custom functions can be used as tools in AI Knowledge agents, enabling them to perform specialized operations like data transformation or external API calls.
AI Store
Functions can power features in agents published to the AI Store, adding custom capabilities to shared agents.
Custom Code App
The Custom Code app provides a user-friendly interface for managing, testing, and monitoring functions.
Security Considerations
When deploying and using the Functions Microservice, keep these security considerations in mind:
Code Isolation
Functions run in isolated environments to prevent interference between different workspaces and functions.
Resource Limits
Configure appropriate resource limits to prevent denial-of-service attacks or resource exhaustion.
Access Control
Implement proper authentication and authorization to control who can create and execute functions.
Dependency Scanning
Consider scanning dependencies for vulnerabilities before allowing their installation.
Function code is executed in a restricted environment, but it still has access to specified Node.js built-in modules and network resources. Be careful when allowing users to create functions, and consider implementing additional security controls for sensitive environments.
Troubleshooting
Function Creation Fails
Function Creation Fails
Possible causes:
- Invalid function code syntax
- Unavailable or incompatible dependencies
- Insufficient disk space for dependencies
- Network issues preventing access to the NPM registry
Resolution steps:
- Check the service logs for specific error messages
- Verify NPM registry access from within the container
- Ensure sufficient disk space in the function storage volume
- Validate function syntax before submission
Function Execution Timeouts
Function Execution Timeouts
Possible causes:
- Function code contains infinite loops or excessive processing
- Timeout setting is too low for the operation
- External service calls that take too long
Resolution steps:
- Review function code for inefficiencies or infinite loops
- Adjust the
FUNCTIONS_RUN_TIMEOUT
orPYTHON_FUNCTIONS_RUN_TIMEOUT
setting - Implement better error handling for external service calls
- Consider breaking complex operations into smaller, chainable functions
Memory-Related Errors
Memory-Related Errors