Function Configuration
Configure your Edge Compute functions with environment variables, secrets, and deployment settings for different environments.
Configuration File (func.toml)
Each function includes a func.toml
configuration file:
[edge_compute]
func_id = "3a62135b-22fb-4902-bd4e-b651761ab777"
func_name = "hello-world"
region = "us-east-1"
[cloud_storage]
bucket_name = "my-bucket"
region = "us-east-1"
[env_vars]
MY_KEY = "VAL"
Environment Variables
Setting Environment Variables
Via Configuration File
[envvars]
DATABASE_URL = "postgres://user:pass@db:5432/prod"
API_BASE_URL = "https://api.example.com"
CACHE_TTL = "3600"
DEBUG = "false"
Accessing Environment Variables
JavaScript/Node.js
export default async function(request) {
const dbUrl = process.env.DATABASE_URL;
const apiUrl = process.env.API_BASE_URL;
const debug = process.env.DEBUG === 'true';
if (debug) {
console.log('Debug mode enabled');
}
return new Response(JSON.stringify({
environment: process.env.NODE_ENV,
version: process.env.API_VERSION
}));
}
Python
import os
async def handler(request):
db_url = os.environ.get("DATABASE_URL")
api_url = os.environ.get("API_BASE_URL")
debug = os.environ.get("DEBUG", "false").lower() == "true"
cache_ttl = int(os.environ.get("CACHE_TTL", "300"))
return {
"database_configured": bool(db_url),
"cache_ttl": cache_ttl,
"debug": debug
}
Go
import (
"os"
"strconv"
)
func handler(w http.ResponseWriter, r *http.Request) {
dbURL := os.Getenv("DATABASE_URL")
debug, _ := strconv.ParseBool(os.Getenv("DEBUG"))
cacheTTL, _ := strconv.Atoi(os.Getenv("CACHE_TTL"))
if cacheTTL == 0 {
cacheTTL = 300 // default value
}
response := map[string]interface{}{
"database_configured": dbURL != "",
"cache_ttl": cacheTTL,
"debug": debug,
}
json.NewEncoder(w).Encode(response)
}
Secrets Management
Storing Secrets
Use the CLI to securely store sensitive data:
# Add secrets
telnyx-edge secrets add DATABASE_PASSWORD "super-secret-password"
telnyx-edge secrets add API_TOKEN "sensitive-api-token"
telnyx-edge secrets add ENCRYPTION_KEY "32-character-key-for-encryption"
# List secrets (values are never shown)
telnyx-edge secrets list
# Remove secrets
telnyx-edge secrets delete OLD_API_TOKEN
Accessing Secrets
Secrets are available as environment variables in your function:
export default async function(request) {
// Access secrets like environment variables
const dbPassword = process.env.DATABASE_PASSWORD;
const apiToken = process.env.API_TOKEN;
const encryptionKey = process.env.ENCRYPTION_KEY;
// Use secrets to connect to services
const dbConnection = createConnection({
host: process.env.DATABASE_HOST,
user: process.env.DATABASE_USER,
password: dbPassword, // From secrets
database: process.env.DATABASE_NAME
});
return new Response('Configuration loaded securely');
}
Best Practices for Secrets
DO:
- Store sensitive data as secrets, not environment variables.
- Use descriptive names for secrets.
- Rotate secrets regularly.
- Use different secrets for different environments.
DON'T:
- Store secrets in your source code.
- Log secret values.
- Share secrets via insecure channels.
- Use the same secrets across all functions.
Runtime Configuration
Memory Settings
Configure memory allocation based on your function's needs:
[edge_compute]
# Memory options: 128MB, 256MB, 512MB, 1GB, 2GB, 3GB
memory = "1GB"
Memory affects:
- Performance: More memory can improve execution speed.
- Cost: Higher memory allocation costs more.
- Concurrency: Available CPU scales with memory.
Timeout Settings
Set maximum execution time:
[edge_compute]
# Timeout range: 1s to 30s (paid), 1s to 10s (free)
timeout = "15s"
Choose timeout based on:
- Function complexity: Simple functions need less time.
- External dependencies: API calls may require longer timeouts.
- Cost optimization: Shorter timeouts prevent runaway costs.
Storage Configuration
Cloud Storage Integration
Configure integrated cloud storage:
[cloud_storage]
bucket_name = "my-function-storage"
region = "us-east-1"
Access in your function:
import { S3Client, GetObjectCommand } from "@aws-sdk/client-s3";
// Pre-configured S3 client for your bucket
const s3Client = new S3Client({
region: process.env.STORAGE_REGION,
// Credentials automatically configured
});
export default async function(request) {
const command = new GetObjectCommand({
Bucket: process.env.STORAGE_BUCKET,
Key: "user-data.json"
});
const response = await s3Client.send(command);
const data = await response.Body.transformToString();
return new Response(data);
}
Configuration Validation
Validating Configuration
// Validate required environment variables
export default async function(request) {
const requiredEnvVars = [
'DATABASE_URL',
'API_TOKEN',
'ENCRYPTION_KEY'
];
const missing = requiredEnvVars.filter(
name => !process.env[name]
);
if (missing.length > 0) {
return new Response(
JSON.stringify({
error: 'Missing required environment variables',
missing: missing
}),
{
status: 500,
headers: { 'Content-Type': 'application/json' }
}
);
}
// Proceed with function logic
return new Response('Configuration is valid');
}
Configuration Schema
Define configuration schema for validation:
// config.schema.ts
interface FunctionConfig {
database: {
url: string;
maxConnections: number;
};
api: {
baseUrl: string;
timeout: number;
};
features: {
debug: boolean;
caching: boolean;
};
}
export function validateConfig(): FunctionConfig {
return {
database: {
url: requireEnv('DATABASE_URL'),
maxConnections: parseInt(process.env.DB_MAX_CONNECTIONS || '10')
},
api: {
baseUrl: requireEnv('API_BASE_URL'),
timeout: parseInt(process.env.API_TIMEOUT || '5000')
},
features: {
debug: process.env.DEBUG === 'true',
caching: process.env.ENABLE_CACHE !== 'false'
}
};
}
function requireEnv(name: string): string {
const value = process.env[name];
if (!value) {
throw new Error(`Required environment variable ${name} is not set`);
}
return value;
}
Configuration Best Practices
Security
- Use secrets for sensitive data.
- Validate configuration at startup.
- Use environment-specific configurations.
- Never log secret values.
Maintainability
- Document all configuration options.
- Use descriptive variable names.
- Group related settings.
- Version your configuration files.