A serverless application that deploys AI-powered agent Lambda functions using AWS CDK, featuring weather forecasting and database query capabilities through MCP (Model Context Protocol) handlers.
This project deploys two Lambda functions:
- Weather Agent Function: Provides weather forecasting capabilities using AI agents
- Query Agent Function: Handles database queries with natural language processing
Both functions are built with Python 3.12, use AWS Bedrock for AI model invocations, and are deployed on ARM64 architecture for better performance and cost efficiency.
- Node.js (v18 or later)
- npm or yarn
- AWS CLI configured with appropriate credentials
- AWS CDK CLI (
npm install -g aws-cdk
) - Python 3.12 (for local development)
- Docker (for building Lambda layers)
strandsagent/
├── AgentLambdaStack.ts # CDK stack definition
├── bin/ # CDK app entry point
│ └── app.ts
├── lambda/ # Lambda function source code
│ ├── query/
│ │ ├── mcp_handler.py
│ │ └── query_agent_handler.py
│ └── weather/
│ ├── mcp_handler.py
│ └── weather_agent_handler.py
├── packaging/ # Deployment packages
│ ├── dependencies.zip # Lambda layer with dependencies
│ ├── query_app.zip # Query function package
│ └── weather_app.zip # Weather function package
├── cdk.json # CDK configuration
├── package.json # Node.js dependencies
└── requirements.txt # Python dependencies
Create a .env
file in the project root with the following variables:
# Required for Weather Function
OPENAI_KEY=your-openai-api-key
# Required for Query Function
DB_HOST=your-database-host
DB_PORT=5432
DB_NAME=your-database-name
DB_USER=your-database-user
DB_PASSWORD=your-database-password
- OPENAI_KEY: OpenAI API key for AI model access
- DB_HOST: PostgreSQL database hostname or IP address
- DB_PORT: Database port (default: 5432)
- DB_NAME: Name of the database to connect to
- DB_USER: Database username
- DB_PASSWORD: Database password
git clone <repository-url>
cd strandsagent
npm install
pip install -r requirements.txt
Before deployment, ensure the Lambda deployment packages are built:
# Build dependencies layer
cd packaging
pip install -r ../requirements.txt -t _dependencies/
cd _dependencies && zip -r ../dependencies.zip . && cd ..
# Package Lambda functions (if not already packaged)
python bin/package_for_lambda.py
Ensure your AWS CLI is configured:
aws configure
npx cdk bootstrap
npx cdk synth
npx cdk deploy
The deployment will:
- Create a Lambda layer with all Python dependencies
- Deploy the Weather Agent Lambda function
- Deploy the Query Agent Lambda function
- Set up IAM roles with necessary permissions for Bedrock access
- Configure environment variables for both functions
After successful deployment, you can verify the functions in the AWS Lambda console:
- Weather Agent Function:
WeatherAgentFunction
- Query Agent Function:
QueryAgentFunction
npm test
Add the mcp to settings.json or to claude code
Make a natural language query about weather or about the database. The LLM will decide which agent to use.
npm run watch
- Watch for TypeScript changesnpm run format
- Format code with Prettiernpx cdk diff
- Compare deployed stack with current statenpx cdk destroy
- Remove the stack from AWS
Both Lambda functions are granted the following permissions:
bedrock:InvokeModel
- Invoke AI modelsbedrock:InvokeModelWithResponseStream
- Stream model responses (if you are using bedrock models)
The Query function additionally requires VPC access if your database is in a private subnet.
Lambda function logs are available in CloudWatch Logs:
/aws/lambda/WeatherAgentFunction
/aws/lambda/QueryAgentFunction
- Lambda functions use ARM64 architecture for cost optimization
- Memory is set to 128MB (adjust based on your needs)
- Timeout settings:
- Weather Function: 30 seconds
- Query Function: 60 seconds
-
Deployment fails with "Environment variable not found"
- Ensure all required environment variables are set in your
.env
file - Source the environment variables before deployment
- Ensure all required environment variables are set in your
-
Lambda function times out
- Increase the timeout value in
AgentLambdaStack.ts
- Check network connectivity for database connections
- Increase the timeout value in
-
Bedrock access denied
- Verify your AWS account has access to Bedrock
- Check the IAM role permissions
-
Database connection fails
- Verify database credentials and network settings
- Ensure Lambda has network access to the database (VPC configuration may be needed)
- Store sensitive environment variables in AWS Secrets Manager or Parameter Store
- Use VPC endpoints for Bedrock access if Lambda is in a VPC
- Regularly rotate database credentials
- Enable CloudWatch Logs encryption
- Use least-privilege IAM policies
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
[Specify your license here]
For issues and questions, please open an issue in the GitHub repository.