Configuration Overview
Configuration
Section titled “Configuration”Microfn provides flexible configuration options for your serverless functions, allowing you to manage environment variables, secrets, and NPM package dependencies. Proper configuration ensures your functions have access to the resources and credentials they need while maintaining security.
Configuration Areas
Section titled “Configuration Areas”Microfn offers two main configuration areas:
| Configuration Type | Description | Use Cases |
|---|---|---|
| Environment Variables & Secrets | Secure storage for sensitive data | API keys, tokens, credentials |
| NPM Packages | External JavaScript dependencies | Libraries, SDKs, utilities |
Quick Overview
Section titled “Quick Overview”Secrets Management
Section titled “Secrets Management”Store sensitive configuration securely:
import secret from "@microfn/secret";
const apiKey = await secret.getRequired("API_KEY");const dbUrl = await secret.get("DATABASE_URL", "localhost:5432");- Stored encrypted in workspace
- Accessed via
@microfn/secretmodule - Never exposed in logs or responses
- Environment-specific values supported
Package Management
Section titled “Package Management”Add NPM packages to your functions:
// After adding 'lodash' package to workspaceimport _ from "lodash";
export default async function main(input) { const sorted = _.orderBy(input.data, ['date'], ['desc']); return { sorted };}- Add packages through dashboard or API
- Version management supported
- Deployed as Lambda layers
- Automatic dependency resolution
Configuration Methods
Section titled “Configuration Methods”1. Microfn Dashboard
Section titled “1. Microfn Dashboard”The easiest way to manage configuration:
- Navigate to your workspace
- Go to Settings
- Configure secrets or packages
- Changes apply immediately
2. API Configuration
Section titled “2. API Configuration”Programmatically manage configuration:
# Add a secretcurl -X POST https://microfn.dev/api/workspaces/username/workspace/secrets \ -H "Authorization: Bearer TOKEN" \ -d '{"key": "API_KEY", "value": "secret-value"}'
# Add a packagecurl -X POST https://microfn.dev/api/workspaces/username/workspace/packages \ -H "Authorization: Bearer TOKEN" \ -d '{"name": "lodash", "version": "4.17.21"}'3. Infrastructure as Code
Section titled “3. Infrastructure as Code”Manage configuration through code:
export const config = { secrets: [ { key: "API_KEY", description: "Main API key" }, { key: "DATABASE_URL", description: "Database connection" } ], packages: [ { name: "lodash", version: "^4.17.21" }, { name: "axios", version: "^1.0.0" } ]};Environment-Specific Configuration
Section titled “Environment-Specific Configuration”Development vs Production
Section titled “Development vs Production”import secret from "@microfn/secret";
export default async function main(input) { const env = await secret.get("ENVIRONMENT", "development");
const config = { development: { apiUrl: "https://dev.api.example.com", debug: true }, production: { apiUrl: "https://api.example.com", debug: false } };
return config[env] || config.development;}Multi-Environment Secrets
Section titled “Multi-Environment Secrets”Prefix secrets for different environments:
// Secrets configured:// DEV_API_KEY, PROD_API_KEY, STAGING_API_KEY
const env = await secret.get("ENV", "DEV");const apiKey = await secret.getRequired(`${env}_API_KEY`);Configuration Best Practices
Section titled “Configuration Best Practices”Security
Section titled “Security”- Never hardcode secrets in function code
- Use descriptive names for configuration values
- Rotate secrets regularly through the dashboard
- Limit secret access to necessary functions only
- Audit secret usage through execution logs
Package Management
Section titled “Package Management”- Pin package versions for consistency
- Minimize dependencies to reduce cold start time
- Test package compatibility before production
- Update packages regularly for security patches
- Use native modules when available
Organization
Section titled “Organization”// Group related configurationconst dbConfig = { host: await secret.get("DB_HOST"), port: await secret.get("DB_PORT", "5432"), name: await secret.get("DB_NAME"), user: await secret.get("DB_USER"), password: await secret.getRequired("DB_PASSWORD")};
const apiConfig = { key: await secret.getRequired("API_KEY"), url: await secret.get("API_URL"), timeout: parseInt(await secret.get("API_TIMEOUT", "5000"))};Configuration Validation
Section titled “Configuration Validation”Startup Validation
Section titled “Startup Validation”import secret from "@microfn/secret";
async function validateConfiguration() { const required = [ "API_KEY", "DATABASE_URL", "AUTH_TOKEN" ];
const missing = [];
for (const key of required) { if (!await secret.has(key)) { missing.push(key); } }
if (missing.length > 0) { throw new Error(`Missing required secrets: ${missing.join(", ")}`); }}
export default async function main(input) { await validateConfiguration();
// Function logic here return { success: true };}Type-Safe Configuration
Section titled “Type-Safe Configuration”interface Config { apiKey: string; apiUrl: string; timeout: number; retries: number;}
async function loadConfig(): Promise<Config> { return { apiKey: await secret.getRequired("API_KEY"), apiUrl: await secret.get("API_URL", "https://api.example.com"), timeout: parseInt(await secret.get("TIMEOUT", "5000")), retries: parseInt(await secret.get("RETRIES", "3")) };}
export default async function main(input) { const config = await loadConfig();
// Use type-safe config const response = await fetchWithRetry( config.apiUrl, config.apiKey, config.retries );
return response;}Configuration Limits
Section titled “Configuration Limits”| Feature | Free Tier | Pro Tier | Enterprise |
|---|---|---|---|
| Secrets per workspace | 10 | 100 | Unlimited |
| Package dependencies | 5 | 50 | Unlimited |
| Secret size | 1 KB | 10 KB | 100 KB |
| Total package size | 10 MB | 100 MB | 1 GB |
Migration and Backup
Section titled “Migration and Backup”Exporting Configuration
Section titled “Exporting Configuration”import secret from "@microfn/secret";
export default async function exportConfig() { const allSecrets = await secret.getWithPrefix("");
// Mask sensitive values const masked = {}; for (const [key, value] of Object.entries(allSecrets)) { masked[key] = value.substring(0, 3) + "***"; }
return { secrets: Object.keys(masked), packages: await getWorkspacePackages(), exported: new Date() };}Configuration as Code
Section titled “Configuration as Code”// config.json{ "secrets": { "API_KEY": { "description": "Primary API key", "required": true }, "DATABASE_URL": { "description": "Database connection string", "required": true } }, "packages": { "lodash": "^4.17.21", "axios": "^1.0.0", "date-fns": "^2.29.0" }}Troubleshooting
Section titled “Troubleshooting”Common Issues
Section titled “Common Issues”| Issue | Solution |
|---|---|
| Secret not found | Check spelling and SECRET_ prefix |
| Package not available | Ensure package is added and deployed |
| Configuration not updating | Clear cache or redeploy function |
| Size limits exceeded | Remove unused packages or optimize |
Debug Configuration
Section titled “Debug Configuration”export default async function debugConfig() { return { hasApiKey: await secret.has("API_KEY"), environment: await secret.get("ENVIRONMENT", "not set"), secretCount: Object.keys(await secret.getWithPrefix("")).length, packages: process.env.NODE_PATH };}Next Steps
Section titled “Next Steps”Dive deeper into specific configuration areas:
- Environment Variables & Secrets - Secure credential management
- NPM Packages - Managing external dependencies