-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
xRPC com.atproto.identity.resolveHandle (#10)
* Create handle resolution service with SAM deployment This commit introduces the Handle Resolution Service for ATProto, developed using AWS SAM. It includes the SAM template, Lambda functions, Redis and DynamoDB integrations, Route53 configuration, and deployment scripts. Additionally, local development and testing setup are documented in the README file. * Refactor handle resolution service for improved reliability. Updated configurations to optimize performance (e.g., reduced timeout, increased memory). Enhanced Redis and DynamoDB error handling, added logging, and configured retries in boto3 to improve service reliability. Removed unnecessary Route53 DNS records and provisioned concurrency settings in template.yaml. * Add VPC endpoints and optimize DynamoDB data handling. Introduced private route tables and a DynamoDB VPC Gateway Endpoint for improved network control. Updated the app to only retrieve and return the 'did' field from DynamoDB, optimizing resource usage and reducing extraneous data processing.
- Loading branch information
1 parent
a36c915
commit 8badeca
Showing
11 changed files
with
841 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.aws-sam/ | ||
dependencies/python/ | ||
__pycache__/ | ||
*.pyc | ||
.env | ||
.venv/ | ||
.idea/ | ||
.vscode/ | ||
/certs/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
# Handle Resolution Service | ||
|
||
ATProto handle resolution service built with AWS SAM. | ||
|
||
## Prerequisites | ||
|
||
- Python 3.12 | ||
```shell | ||
brew install [email protected] | ||
``` | ||
- AWS SAM CLI | ||
- AWS CLI configured with appropriate credentials | ||
|
||
## Development Setup | ||
|
||
1. Create and activate a virtual environment: | ||
```bash | ||
python3.12 -m venv .venv | ||
source .venv/bin/activate | ||
``` | ||
|
||
2. Install development dependencies: | ||
```bash | ||
# Update pip | ||
python -m pip install --upgrade pip | ||
|
||
# Install development requirements | ||
pip install aws-sam-cli awscli | ||
``` | ||
|
||
## Project Structure | ||
|
||
``` | ||
handle-resolution-service/ | ||
├── template.yaml # SAM template | ||
├── requirements.txt # Python dependencies | ||
├── build.sh # Build script | ||
├── src/ # Lambda function code | ||
└── dependencies/ # Lambda layer dependencies | ||
``` | ||
|
||
## Building and Deployment | ||
|
||
1. Build the project: | ||
```bash | ||
chmod +x build.sh | ||
./build.sh | ||
``` | ||
|
||
2. Deploy (first time): | ||
```bash | ||
sam deploy --guided | ||
``` | ||
|
||
3. Subsequent deployments: | ||
```bash | ||
sam build && sam deploy --no-confirm-changeset | ||
``` | ||
|
||
```shell | ||
sam delete --stack-name handle-resolution-service --no-prompts | ||
``` | ||
|
||
### Production Test | ||
|
||
```shell | ||
curl https://xrpc.arkavo.net/xrpc/com.atproto.identity.resolveHandle?handle=test.bsky.social | ||
``` | ||
|
||
```shell | ||
curl https://q8ku4t7uxj.execute-api.us-east-1.amazonaws.com/Prod/xrpc/com.atproto.identity.resolveHandle?handle=test.bsky.social | ||
``` | ||
|
||
## Local Development | ||
|
||
1. Install local dependencies in your virtual environment: | ||
```bash | ||
pip install -r requirements.txt | ||
``` | ||
|
||
2. Colima | ||
```shell | ||
colima start | ||
export DOCKER_HOST="unix://${HOME}/.colima/docker.sock" | ||
```` | ||
|
||
3. Redis | ||
```shell | ||
docker run -d --name redis -p 6379:6379 redis | ||
``` | ||
|
||
4. DynamoDB | ||
```shell | ||
docker run -d --name dynamodb -p 8000:8000 amazon/dynamodb-local | ||
``` | ||
|
||
```shell | ||
# Create the table | ||
aws dynamodb create-table \ | ||
--table-name dev-handles \ | ||
--attribute-definitions AttributeName=handle,AttributeType=S \ | ||
--key-schema AttributeName=handle,KeyType=HASH \ | ||
--billing-mode PAY_PER_REQUEST \ | ||
--endpoint-url http://localhost:8000 | ||
# Add test data | ||
aws dynamodb put-item \ | ||
--table-name dev-handles \ | ||
--item '{"handle": {"S": "test.arkavo.social"}, "did": {"S": "did:plc:testuser123"}}' \ | ||
--endpoint-url http://localhost:8000 | ||
``` | ||
|
||
|
||
3. Local testing: | ||
```bash | ||
# Test GET request | ||
sam local invoke HandleCheckFunction --env-vars env.json -e events/get-request.json | ||
# Test HEAD request | ||
sam local invoke HandleCheckFunction --env-vars env.json -e events/head-request.json | ||
# Start local API | ||
sam local start-api | ||
``` | ||
|
||
4. Test the API: | ||
```bash | ||
# Test GET endpoint | ||
curl "http://localhost:3000/xrpc/com.atproto.identity.resolveHandle?handle=test.arkavo.social" | ||
# Test HEAD endpoint | ||
curl -I "http://localhost:3000/xrpc/com.atproto.identity.resolveHandle?handle=test.arkavo.social" | ||
``` | ||
|
||
## API Endpoints | ||
|
||
The service provides two endpoints: | ||
|
||
### GET /xrpc/com.atproto.identity.resolveHandle | ||
Resolves a handle to a DID. | ||
|
||
Query Parameters: | ||
- `handle`: The handle to resolve (e.g., `username.arkavo.social`) | ||
|
||
### HEAD /xrpc/com.atproto.identity.resolveHandle | ||
Quick check for handle validity and existence. | ||
|
||
Query Parameters: | ||
- `handle`: The handle to check (e.g., `username.arkavo.social`) | ||
|
||
## Contributing | ||
|
||
1. Create a new branch for your feature | ||
2. Make changes in your branch | ||
3. Test locally using SAM CLI | ||
4. Submit a pull request | ||
|
||
## Common Issues | ||
|
||
### Virtual Environment Issues | ||
|
||
If you see "command not found" after activating the virtual environment: | ||
```bash | ||
# Deactivate and remove the current venv | ||
deactivate | ||
rm -rf .venv | ||
# Create a new venv with --clear flag | ||
python3.12 -m venv .venv --clear | ||
# Reactivate and reinstall dependencies | ||
source .venv/bin/activate | ||
pip install -r requirements.txt | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#!/bin/bash | ||
set -e | ||
|
||
# Ensure we're using Python 3.12 | ||
PYTHON=python3.12 | ||
|
||
# Install dependencies to the Lambda layer directory | ||
$PYTHON -m pip install -r requirements.txt -t dependencies/python | ||
|
||
# Clean up unnecessary files | ||
find dependencies/python -type d -name "tests" -exec rm -rf {} + | ||
find dependencies/python -type d -name "__pycache__" -exec rm -rf {} + | ||
find dependencies/python -type f -name "*.pyc" -delete | ||
find dependencies/python -type f -name "*.pyo" -delete | ||
find dependencies/python -type f -name "*.dist-info" -exec rm -rf {} + | ||
|
||
# Build SAM application | ||
sam build | ||
|
||
# Deploy (optional, can be run separately) | ||
# sam deploy --guided |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"HandleCheckFunction": { | ||
"REDIS_ENDPOINT": "host.docker.internal", | ||
"DYNAMODB_TABLE": "dev-handles" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"httpMethod": "GET", | ||
"queryStringParameters": { | ||
"handle": "test.arkavo.social" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"httpMethod": "HEAD", | ||
"queryStringParameters": { | ||
"handle": "test.arkavo.social" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
redis==5.0.1 | ||
boto3==1.34.64 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
version = 0.1 | ||
[default.deploy.parameters] | ||
stack_name = "handle-resolution-service" | ||
resolve_s3 = true | ||
s3_prefix = "handle-resolution-service" | ||
region = "us-east-1" | ||
confirm_changeset = true | ||
capabilities = "CAPABILITY_IAM" | ||
parameter_overrides = "Environment=\"prod\" VpcCIDR=\"10.0.0.0/16\" RedisNodeType=\"cache.t4g.micro\" DomainName=\"arkavo.net\" XrpcSubdomain=\"xrpc\"" | ||
image_repositories = [] |
Oops, something went wrong.