|
| 1 | +# cryptocom-agent-client AWS Lambda Example |
| 2 | + |
| 3 | +Deploy [cryptocom-agent-client](https://pypi.org/project/cryptocom-agent-client/) as a serverless AWS Lambda function using Docker. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +This example demonstrates how to: |
| 8 | +- Deploy a cryptocom-agent-client powered AI agent to AWS Lambda |
| 9 | +- Use Docker containers for Lambda deployment |
| 10 | +- Create custom tools (functions) the agent can invoke |
| 11 | +- Query blockchain data through the Crypto.com Developer Platform |
| 12 | + |
| 13 | +## Features |
| 14 | + |
| 15 | +- **AI Agent**: Uses OpenAI GPT-4o-mini with cryptocom-agent-client |
| 16 | +- **Custom Tools**: |
| 17 | + - `get_current_time()` - Returns current local and UTC time |
| 18 | + - `fibonacci(n)` - Calculates the nth Fibonacci number |
| 19 | +- **Blockchain Integration**: Query Cronos blockchain data via Dashboard API |
| 20 | +- **Serverless**: Auto-scaling, pay-per-use AWS Lambda deployment |
| 21 | + |
| 22 | +## Prerequisites |
| 23 | + |
| 24 | +- [AWS CLI](https://aws.amazon.com/cli/) configured with appropriate credentials |
| 25 | +- [Docker Desktop](https://www.docker.com/products/docker-desktop/) running |
| 26 | +- [jq](https://jqlang.github.io/jq/) for JSON processing |
| 27 | +- AWS account with permissions for Lambda, ECR, and IAM |
| 28 | +- [OpenAI API key](https://platform.openai.com/api-keys) |
| 29 | +- [Crypto.com Developer Platform API key](https://developer.crypto.com/) |
| 30 | + |
| 31 | +## Quick Start |
| 32 | + |
| 33 | +### 1. Configure Environment |
| 34 | + |
| 35 | +```bash |
| 36 | +cp .env.example .env |
| 37 | +``` |
| 38 | + |
| 39 | +Edit `.env` with your credentials: |
| 40 | +```bash |
| 41 | +# AWS (use profile or access keys) |
| 42 | +AWS_PROFILE=default |
| 43 | +AWS_DEFAULT_REGION=us-east-1 |
| 44 | + |
| 45 | +# API Keys |
| 46 | +OPENAI_API_KEY=sk-... |
| 47 | +DASHBOARD_API_KEY=your-dashboard-api-key |
| 48 | +``` |
| 49 | + |
| 50 | +### 2. Deploy to AWS |
| 51 | + |
| 52 | +```bash |
| 53 | +./deploy.sh |
| 54 | +``` |
| 55 | + |
| 56 | +This will: |
| 57 | +- Build Docker image (ARM64 optimized) |
| 58 | +- Create ECR repository and push image |
| 59 | +- Create IAM role with basic execution permissions |
| 60 | +- Deploy Lambda function (300s timeout, 1024MB memory) |
| 61 | + |
| 62 | +### 3. Test the Function |
| 63 | + |
| 64 | +```bash |
| 65 | +# Default prompt |
| 66 | +./run.sh |
| 67 | + |
| 68 | +# Custom prompts |
| 69 | +./run.sh "What is the current time?" |
| 70 | +./run.sh "Calculate fibonacci of 10" |
| 71 | +./run.sh "Get the latest block on Cronos" |
| 72 | +``` |
| 73 | + |
| 74 | +### 4. Clean Up |
| 75 | + |
| 76 | +```bash |
| 77 | +./cleanup.sh |
| 78 | +``` |
| 79 | + |
| 80 | +## Local Testing |
| 81 | + |
| 82 | +Test the Lambda function locally before deploying: |
| 83 | + |
| 84 | +```bash |
| 85 | +./test-local.sh "What is the current time?" |
| 86 | +``` |
| 87 | + |
| 88 | +## Project Structure |
| 89 | + |
| 90 | +``` |
| 91 | +. |
| 92 | +├── handler.py # Lambda handler with AI agent |
| 93 | +├── requirements.txt # Python dependencies |
| 94 | +├── Dockerfile # Container configuration |
| 95 | +├── deploy.sh # Deploy to AWS Lambda |
| 96 | +├── run.sh # Invoke the Lambda function |
| 97 | +├── test-local.sh # Test locally with Docker |
| 98 | +├── cleanup.sh # Remove all AWS resources |
| 99 | +└── .env.example # Environment template |
| 100 | +``` |
| 101 | + |
| 102 | +## Customization |
| 103 | + |
| 104 | +### Adding Custom Tools |
| 105 | + |
| 106 | +Edit `handler.py` to add your own tools: |
| 107 | + |
| 108 | +```python |
| 109 | +from crypto_com_agent_client import tool |
| 110 | + |
| 111 | +@tool |
| 112 | +def my_custom_tool(param: str) -> str: |
| 113 | + """ |
| 114 | + Description of what this tool does. |
| 115 | +
|
| 116 | + Args: |
| 117 | + param: Description of parameter |
| 118 | +
|
| 119 | + Returns: |
| 120 | + Description of return value |
| 121 | + """ |
| 122 | + # Your implementation |
| 123 | + return result |
| 124 | +``` |
| 125 | + |
| 126 | +Register the tool in the agent initialization: |
| 127 | + |
| 128 | +```python |
| 129 | +agent = Agent.init( |
| 130 | + llm_config={...}, |
| 131 | + blockchain_config={...}, |
| 132 | + plugins={ |
| 133 | + "tools": [get_current_time, fibonacci, my_custom_tool], |
| 134 | + }, |
| 135 | +) |
| 136 | +``` |
| 137 | + |
| 138 | +### Changing the LLM Model |
| 139 | + |
| 140 | +Modify the `llm_config` in `handler.py`: |
| 141 | + |
| 142 | +```python |
| 143 | +llm_config={ |
| 144 | + "provider": Provider.OpenAI, |
| 145 | + "model": "gpt-4o", # or "gpt-3.5-turbo", etc. |
| 146 | + "provider-api-key": openai_api_key, |
| 147 | +} |
| 148 | +``` |
| 149 | + |
| 150 | +### Using Different Providers |
| 151 | + |
| 152 | +The cryptocom-agent-client supports multiple LLM providers: |
| 153 | + |
| 154 | +```python |
| 155 | +from crypto_com_agent_client.lib.enums.provider_enum import Provider |
| 156 | + |
| 157 | +# AWS Bedrock |
| 158 | +llm_config={ |
| 159 | + "provider": Provider.AWSBedrock, |
| 160 | + "model": "anthropic.claude-3-haiku-20240307-v1:0", |
| 161 | +} |
| 162 | + |
| 163 | +# Google AI |
| 164 | +llm_config={ |
| 165 | + "provider": Provider.GoogleAI, |
| 166 | + "model": "gemini-pro", |
| 167 | + "provider-api-key": google_api_key, |
| 168 | +} |
| 169 | +``` |
| 170 | + |
| 171 | +## Architecture |
| 172 | + |
| 173 | +``` |
| 174 | +┌─────────────┐ ┌─────────────────┐ ┌──────────────┐ |
| 175 | +│ Client │───>│ AWS Lambda │───>│ OpenAI │ |
| 176 | +│ (run.sh) │ │ (handler.py) │ │ GPT-4o │ |
| 177 | +└─────────────┘ └────────┬────────┘ └──────────────┘ |
| 178 | + │ |
| 179 | + v |
| 180 | + ┌─────────────────┐ |
| 181 | + │ Crypto.com │ |
| 182 | + │ Dashboard │ |
| 183 | + │ (Blockchain) │ |
| 184 | + └─────────────────┘ |
| 185 | +``` |
| 186 | + |
| 187 | +## Lambda Configuration |
| 188 | + |
| 189 | +| Setting | Value | |
| 190 | +|---------|-------| |
| 191 | +| Runtime | Python 3.12 (Docker) | |
| 192 | +| Architecture | ARM64 | |
| 193 | +| Timeout | 300 seconds | |
| 194 | +| Memory | 1024 MB | |
| 195 | + |
| 196 | +## AWS Resource Identifiers |
| 197 | + |
| 198 | +### What is ARN? |
| 199 | + |
| 200 | +ARN (Amazon Resource Name) is a unique identifier for any resource in AWS. |
| 201 | + |
| 202 | +**Format:** |
| 203 | +``` |
| 204 | +arn:aws:<service>:<region>:<account-id>:<resource-type>/<resource-name> |
| 205 | +``` |
| 206 | + |
| 207 | +**Example for this Lambda:** |
| 208 | +``` |
| 209 | +arn:aws:lambda:us-east-1:123456789012:function:cryptocom-agent-lambda |
| 210 | + │ │ │ │ |
| 211 | + │ │ │ └─ Function name |
| 212 | + │ │ └─ Your AWS account ID |
| 213 | + │ └─ Region (e.g., us-east-1) |
| 214 | + └─ Service (lambda) |
| 215 | +``` |
| 216 | + |
| 217 | +### Resource Identifiers Used |
| 218 | + |
| 219 | +| Resource | Identifier | |
| 220 | +|----------|------------| |
| 221 | +| Lambda Function | `cryptocom-agent-lambda` | |
| 222 | +| ECR Repository | `cryptocom-agent-lambda` | |
| 223 | +| IAM Role | `lambda-exec-cryptocom-agent` | |
| 224 | +| Docker Image | `cryptocom-agent-lambda:latest` | |
| 225 | + |
| 226 | +When invoking the Lambda in the same account/region, scripts use just the function name. AWS resolves the full ARN automatically from your credentials. |
| 227 | + |
| 228 | +## Security Notes |
| 229 | + |
| 230 | +- API keys are passed at runtime in the request payload, not stored in Lambda environment |
| 231 | +- The IAM role has minimal permissions (AWSLambdaBasicExecutionRole only) |
| 232 | +- Never commit `.env` files with real credentials |
| 233 | + |
| 234 | +## Troubleshooting |
| 235 | + |
| 236 | +### Docker Build Issues |
| 237 | +- Ensure Docker Desktop is running |
| 238 | +- On macOS, the build uses ARM64 for Apple Silicon optimization |
| 239 | + |
| 240 | +### Lambda Timeout |
| 241 | +- Default timeout is 300 seconds |
| 242 | +- First invocation may take longer due to cold start |
| 243 | +- Subsequent invocations use cached agent instance |
| 244 | + |
| 245 | +### ECR Push Failures |
| 246 | +- Verify AWS credentials are configured: `aws sts get-caller-identity` |
| 247 | +- Check ECR permissions in your AWS account |
| 248 | + |
| 249 | +## Related Resources |
| 250 | + |
| 251 | +- [cryptocom-agent-client on PyPI](https://pypi.org/project/cryptocom-agent-client/) |
| 252 | +- [Crypto.com Developer Platform](https://developer.crypto.com/) |
| 253 | +- [AWS Lambda Container Images](https://docs.aws.amazon.com/lambda/latest/dg/images-create.html) |
| 254 | + |
| 255 | +## License |
| 256 | + |
| 257 | +Apache-2.0 |
0 commit comments