2 Answers
- Newest
- Most votes
- Most comments
0
I'll provide you with basic code examples for both files that you can upload to your Lambda function.
strands_agent.py:
from strands import Agent from typing import Dict, Any # Define a system prompt for your agent SYSTEM_PROMPT = """You are a helpful assistant that provides clear and concise answers to user questions.""" class StrandsAgent: def __init__(self): # Initialize the agent with the system prompt self.agent = Agent(system_prompt=SYSTEM_PROMPT) def process_message(self, message: str) -> str: # Process the input message and return the agent's response response = self.agent.process(message) return response.text
lambda.py:
from strands_agent import StrandsAgent from typing import Dict, Any # Initialize the agent once outside the handler for better performance agent = StrandsAgent() def handler(event: Dict[str, Any], context): # Extract the input text from the event input_text = event.get("text", "") if not input_text: return { "statusCode": 400, "body": "No input text provided" } # Process the input text with the Strands agent response_text = agent.process_message(input_text) # Return the response return { "statusCode": 200, "body": response_text }
To deploy these files to Lambda:
- Create a virtual environment and install dependencies:
python -m venv venv
source venv/bin/activate
pip install strands
- Create a package directory and copy files:
mkdir package
cd package
cp -r ../venv/lib/python*/site-packages/* .
cp ../lambda.py .
cp ../strands_agent.py .
- Create a zip file:
zip -r strands_agent_lambda.zip .
-
Upload the zip file to your Lambda function.
-
Set the handler to:
lambda.handler
When you invoke this Lambda function with an event like:
{ "text": "What is AWS Lambda?" }
The Lambda will:
- Extract the "text" field from the event
- Pass it to the Strands agent for processing
- Return a response with the agent's answer about AWS Lambda
The output will be a JSON object with:
- statusCode: 200 for success, 400 if no input text is provided
- body: The text response from the Strands agent
You can customize the system prompt in strands_agent.py to make your agent more specialized for specific tasks or domains.
Sources
connect the strands agent to lambda | AWS re:Post
lambda connect to strands | AWS re:Post
