Skip to content

Commit 27b8936

Browse files
Merge pull request #131 from crypto-com/feature/465
[BLCKCHN-465] Package for AWS Lambda #130
2 parents 99ffd80 + 01561cc commit 27b8936

File tree

12 files changed

+988
-0
lines changed

12 files changed

+988
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# AWS Configuration
2+
# Option 1: Use AWS Profile
3+
AWS_PROFILE=default
4+
5+
# Option 2: Use Access Keys
6+
# AWS_ACCESS_KEY_ID=your-access-key
7+
# AWS_SECRET_ACCESS_KEY=your-secret-key
8+
9+
# AWS Region
10+
AWS_DEFAULT_REGION=us-east-1
11+
12+
# OpenAI API Key
13+
# Get from: https://platform.openai.com/api-keys
14+
OPENAI_API_KEY=sk-...
15+
16+
# Crypto.com Developer Platform API Key
17+
# Get from: https://developer.crypto.com/
18+
DASHBOARD_API_KEY=your-dashboard-api-key
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.json
2+
.env
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Official AWS Lambda base image for Python 3.12
2+
FROM public.ecr.aws/lambda/python:3.12
3+
4+
# Copy requirements and install dependencies (if any)
5+
COPY requirements.txt ${LAMBDA_TASK_ROOT}/
6+
RUN pip3 install -r requirements.txt -t ${LAMBDA_TASK_ROOT}
7+
8+
# Copy function code
9+
COPY handler.py ${LAMBDA_TASK_ROOT}/
10+
11+
# Set the handler (file.function)
12+
CMD ["handler.lambda_handler"]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Official AWS Lambda base image for Python 3.12
2+
FROM public.ecr.aws/lambda/python:3.12
3+
4+
# Copy requirements and install dependencies
5+
COPY requirements.txt ${LAMBDA_TASK_ROOT}/
6+
RUN pip3 install -r requirements.txt -t ${LAMBDA_TASK_ROOT}
7+
8+
# Users should set their own CMD when extending this image
9+
# Example: CMD ["your_handler.lambda_handler"]
Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
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
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/bin/bash
2+
3+
# Load environment variables
4+
if [ -f .env ]; then
5+
set -a
6+
source .env
7+
set +a
8+
fi
9+
10+
# Auto-detect AWS Account ID if not set
11+
if [ -z "$AWS_ACCOUNTID" ]; then
12+
AWS_ACCOUNTID=$(aws sts get-caller-identity --query Account --output text)
13+
fi
14+
15+
echo "Cleaning up cryptocom-agent-client Lambda resources..."
16+
echo "AWS Account: $AWS_ACCOUNTID"
17+
echo "AWS Region: $AWS_DEFAULT_REGION"
18+
echo ""
19+
20+
# Configuration
21+
REPO_NAME="cryptocom-agent-lambda"
22+
FUNCTION_NAME="cryptocom-agent-lambda"
23+
ROLE_NAME="lambda-exec-cryptocom-agent"
24+
25+
# Delete Lambda function
26+
echo "Deleting Lambda function..."
27+
aws lambda delete-function \
28+
--function-name ${FUNCTION_NAME} \
29+
--region ${AWS_DEFAULT_REGION} 2>/dev/null && echo " Lambda function deleted" || echo " Lambda function not found"
30+
31+
# Delete ECR repository
32+
echo "Deleting ECR repository..."
33+
aws ecr delete-repository \
34+
--repository-name ${REPO_NAME} \
35+
--region ${AWS_DEFAULT_REGION} \
36+
--force 2>/dev/null && echo " ECR repository deleted" || echo " ECR repository not found"
37+
38+
# Detach policy and delete role
39+
echo "Cleaning up IAM role..."
40+
aws iam detach-role-policy \
41+
--role-name ${ROLE_NAME} \
42+
--policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole 2>/dev/null || true
43+
44+
aws iam delete-role \
45+
--role-name ${ROLE_NAME} 2>/dev/null && echo " IAM role deleted" || echo " IAM role not found"
46+
47+
# Clean up local files
48+
echo "Cleaning up local files..."
49+
rm -f response.json
50+
51+
# Remove local Docker images
52+
echo "Removing Docker images..."
53+
docker rmi cryptocom-agent-lambda:latest 2>/dev/null || true
54+
docker rmi cryptocom-agent-lambda:test 2>/dev/null || true
55+
docker rmi ${AWS_ACCOUNTID}.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com/${REPO_NAME}:latest 2>/dev/null || true
56+
57+
echo ""
58+
echo "Cleanup complete!"

0 commit comments

Comments
 (0)