| Duration | 1.5 hours |
| Day | 5 of 5 |
Learning Objectives
By the end of this module, students will be able to:
- Deploy agents to AWS Lambda
- Configure Google Cloud Functions
- Set up Azure Functions
- Handle cold starts appropriately
Topics
1. Serverless Benefits (15 min)
Why Serverless?
| Benefit | Description |
|---|---|
| No server management | Cloud handles infrastructure |
| Auto-scaling | Scales with demand |
| Pay per use | Only pay when called |
| High availability | Built-in redundancy |
When to Use Serverless
| Good For | Not Good For |
|---|---|
| Sporadic traffic | Consistent high load |
| Variable load | Very low latency requirements |
| Simple deployments | Complex state management |
| Cost optimization | Long-running processes |
2. AWS Lambda (30 min)
Handler Code
# handler.py
from signalwire_agents import AgentBase, SwaigFunctionResult
class MyAgent(AgentBase):
def __init__(self):
super().__init__(name="lambda-agent")
self.prompt_add_section("Role", "You are a helpful assistant.")
self.add_language("English", "en-US", "rime.spore")
@AgentBase.tool(description="Get help")
def get_help(self, args: dict, raw_data: dict = None) -> SwaigFunctionResult:
return SwaigFunctionResult("How can I help you?")
# Create agent instance
agent = MyAgent()
# Lambda handler
def lambda_handler(event, context):
return agent.handle_lambda(event, context)
serverless.yml (Serverless Framework)
service: voice-agent
provider:
name: aws
runtime: python3.11
region: us-east-1
environment:
SIGNALWIRE_SPACE_NAME: ${env:SIGNALWIRE_SPACE_NAME}
SIGNALWIRE_PROJECT_ID: ${env:SIGNALWIRE_PROJECT_ID}
SIGNALWIRE_TOKEN: ${env:SIGNALWIRE_TOKEN}
functions:
agent:
handler: handler.lambda_handler
events:
- http:
path: /agent
method: post
- http:
path: /agent/swaig
method: post
timeout: 30
memorySize: 512
plugins:
- serverless-python-requirements
custom:
pythonRequirements:
dockerizePip: true
Deploying
# Install dependencies
npm install -g serverless
npm install serverless-python-requirements
# Deploy
serverless deploy
# Get URL
serverless info
3. Google Cloud Functions (25 min)
Handler Code
# main.py
from signalwire_agents import AgentBase, SwaigFunctionResult
class MyAgent(AgentBase):
def __init__(self):
super().__init__(name="gcf-agent")
self.prompt_add_section("Role", "You are a helpful assistant.")
self.add_language("English", "en-US", "rime.spore")
agent = MyAgent()
def handle_request(request):
"""Google Cloud Function entry point."""
return agent.handle_cloud_function(request)
requirements.txt
signalwire-agents>=1.0.7
Deploying
gcloud functions deploy voice-agent \
--runtime python311 \
--trigger-http \
--allow-unauthenticated \
--entry-point handle_request \
--set-env-vars SIGNALWIRE_SPACE_NAME=your-space,SIGNALWIRE_PROJECT_ID=your-id,SIGNALWIRE_TOKEN=your-token
4. Azure Functions (20 min)
Handler Code
# __init__.py
import azure.functions as func
from signalwire_agents import AgentBase, SwaigFunctionResult
class MyAgent(AgentBase):
def __init__(self):
super().__init__(name="azure-agent")
self.prompt_add_section("Role", "You are a helpful assistant.")
self.add_language("English", "en-US", "rime.spore")
agent = MyAgent()
def main(req: func.HttpRequest) -> func.HttpResponse:
return agent.handle_azure_function(req)
function.json
{
"scriptFile": "__init__.py",
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": ["post"]
},
{
"type": "http",
"direction": "out",
"name": "$return"
}
]
}
5. Cold Start Optimization (10 min)
What is Cold Start?
First request after idle period:
- Container starts
- Runtime loads
- Dependencies import
- Code initializes
Can add 1-5 seconds!
Mitigation Strategies
1. Keep Warm
# serverless.yml
functions:
agent:
events:
- schedule: rate(5 minutes) # Keep warm
2. Minimize Dependencies
# Import only what you need
from signalwire_agents import AgentBase # Good
# from signalwire_agents import * # Bad - loads everything
3. Lazy Loading
class LazyAgent(AgentBase):
_db_client = None
@property
def db(self):
if self._db_client is None:
from database import Client
self._db_client = Client()
return self._db_client
4. Provisioned Concurrency (AWS)
functions:
agent:
provisionedConcurrency: 2 # Keep 2 instances warm
Key Takeaways
- Serverless = no server management - Cloud handles it
- Each platform has handlers - Lambda, GCF, Azure
- Cold starts are real - Plan for them
- Keep dependencies minimal - Faster startup
- Use for variable load - Pay for what you use
Preparation for Lab 2.11
- AWS, GCP, or Azure account
- CLI tools installed
- Environment variables ready
Lab Preview
In Lab 2.11, you will:
- Deploy to AWS Lambda
- Test cold start performance
- Configure environment
- Connect to SignalWire