Skip to content

Commit b3fd019

Browse files
committed
trying a different approach (serverless), tests pass!
1 parent 0b59556 commit b3fd019

31 files changed

+1926
-18
lines changed

tooling/sparta-aws/Dockerfile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Use a slim base image
2+
FROM node:18-slim
3+
4+
# Install system dependencies
5+
RUN apt-get update && \
6+
apt-get install -y curl && \
7+
rm -rf /var/lib/apt/lists/*
8+
9+
# Set working directory
10+
WORKDIR /app
11+
12+
# Copy package files
13+
COPY package*.json ./
14+
15+
# Install dependencies
16+
RUN npm ci --only=production
17+
18+
# Copy application files
19+
COPY dist/ ./dist/
20+
21+
# Set environment variables
22+
ENV NODE_ENV=production
23+
24+
# Command will be provided by ECS task definition
25+
CMD ["node", "dist/index.js"]

tooling/sparta-aws/README.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Sparta AWS Infrastructure
2+
3+
This is the AWS infrastructure version of the Sparta Discord bot for managing validators. It uses serverless architecture with AWS Lambda, API Gateway, and other AWS services.
4+
5+
## Architecture
6+
7+
- AWS Lambda for the Discord bot logic
8+
- API Gateway for Discord webhook endpoints
9+
- DynamoDB for state management (if needed)
10+
- CloudWatch for logging and monitoring
11+
- ECR for Docker container storage
12+
- Parameter Store for secrets
13+
- CloudWatch Alarms for monitoring
14+
15+
## Prerequisites
16+
17+
1. AWS CLI installed and configured
18+
2. Terraform installed
19+
3. Node.js 18.x or later
20+
4. Discord bot token and application ID
21+
5. Required environment variables (see below)
22+
23+
## Setup
24+
25+
1. Create a `.env` file based on `.env.example`
26+
2. Deploy the infrastructure:
27+
```bash
28+
cd terraform
29+
terraform init
30+
terraform plan
31+
terraform apply
32+
```
33+
3. Deploy the Lambda function:
34+
```bash
35+
cd src
36+
npm install
37+
npm run build
38+
npm run deploy
39+
```
40+
41+
## Environment Variables
42+
43+
Required environment variables in AWS Parameter Store:
44+
45+
- `/sparta/discord/bot_token`
46+
- `/sparta/discord/client_id`
47+
- `/sparta/discord/guild_id`
48+
- `/sparta/discord/prod_channel_id`
49+
- `/sparta/discord/dev_channel_id`
50+
- `/sparta/ethereum/host`
51+
- `/sparta/ethereum/rollup_address`
52+
- `/sparta/ethereum/admin_address`
53+
- `/sparta/ethereum/chain_id`
54+
- `/sparta/ethereum/mnemonic`
55+
- `/sparta/ethereum/private_key`
56+
- `/sparta/ethereum/value`
57+
58+
## Architecture Details
59+
60+
### Lambda Function
61+
The main bot logic runs in a Lambda function, triggered by API Gateway when Discord sends webhook events.
62+
63+
### API Gateway
64+
Handles incoming webhook requests from Discord and routes them to the appropriate Lambda function.
65+
66+
### CloudWatch
67+
All Lambda function logs are automatically sent to CloudWatch Logs. CloudWatch Alarms monitor for errors and latency.
68+
69+
### Parameter Store
70+
Stores all sensitive configuration values securely.
71+
72+
### ECR
73+
Stores the Docker container used for validator operations (implementation pending).
74+
75+
## Monitoring
76+
77+
CloudWatch dashboards and alarms are set up to monitor:
78+
- Lambda function errors
79+
- API Gateway latency
80+
- Lambda function duration
81+
- Lambda function throttles
82+
- API Gateway 4xx/5xx errors
83+
84+
## Security
85+
86+
- All secrets are stored in AWS Parameter Store
87+
- IAM roles follow least privilege principle
88+
- API Gateway endpoints are secured with Discord signature verification
89+
- Network access is restricted where possible

tooling/sparta-aws/src/.env.example

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Environment (development or production)
2+
ENVIRONMENT=development
3+
4+
# Discord Bot Configuration
5+
# For development (sparta-dev), use these values locally
6+
# For production (sparta), these values should be set in CI/CD pipeline
7+
DISCORD_BOT_TOKEN=
8+
DISCORD_CLIENT_ID=
9+
DISCORD_PUBLIC_KEY=
10+
DISCORD_GUILD_ID=
11+
DISCORD_CHANNEL_ID=1329081299490570296 # Dev channel: bot-test
12+
# Production channel reference: 1302946562745438238
13+
14+
# Ethereum Configuration
15+
ETHEREUM_HOST=http://localhost:8545
16+
ETHEREUM_MNEMONIC=
17+
ETHEREUM_PRIVATE_KEY=
18+
ETHEREUM_ROLLUP_ADDRESS=
19+
ETHEREUM_CHAIN_ID=1337
20+
ETHEREUM_VALUE=20ether
21+
ETHEREUM_ADMIN_ADDRESS=
22+
23+
# AWS Configuration
24+
AWS_REGION=us-east-1
25+
AWS_ACCOUNT_ID=
26+
27+
# For local development
28+
# Note: Install ngrok globally with: npm install -g ngrok
29+
# Then authenticate with: ngrok config add-authtoken YOUR_AUTH_TOKEN
30+
ENDPOINT_URL=https://your-domain.ngrok-free.app/discord-webhook
31+
32+
# ECS Configuration
33+
ECS_CLUSTER_ARN=your_cluster_arn_here
34+
ECS_TASK_DEFINITION=your_task_definition_here
35+
ECS_CONTAINER_NAME=your_container_name_here
36+
ECS_SUBNET_IDS=subnet-xxxxx,subnet-yyyyy
37+
ECS_SECURITY_GROUP_IDS=sg-xxxxx,sg-yyyyy

tooling/sparta-aws/src/.gitignore

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
2+
3+
# Logs
4+
5+
logs
6+
_.log
7+
npm-debug.log_
8+
yarn-debug.log*
9+
yarn-error.log*
10+
lerna-debug.log*
11+
.pnpm-debug.log*
12+
13+
# Caches
14+
15+
.cache
16+
17+
# Diagnostic reports (https://nodejs.org/api/report.html)
18+
19+
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
20+
21+
# Runtime data
22+
23+
pids
24+
_.pid
25+
_.seed
26+
*.pid.lock
27+
28+
# Directory for instrumented libs generated by jscoverage/JSCover
29+
30+
lib-cov
31+
32+
# Coverage directory used by tools like istanbul
33+
34+
coverage
35+
*.lcov
36+
37+
# nyc test coverage
38+
39+
.nyc_output
40+
41+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
42+
43+
.grunt
44+
45+
# Bower dependency directory (https://bower.io/)
46+
47+
bower_components
48+
49+
# node-waf configuration
50+
51+
.lock-wscript
52+
53+
# Compiled binary addons (https://nodejs.org/api/addons.html)
54+
55+
build/Release
56+
57+
# Dependency directories
58+
59+
node_modules/
60+
jspm_packages/
61+
62+
# Snowpack dependency directory (https://snowpack.dev/)
63+
64+
web_modules/
65+
66+
# TypeScript cache
67+
68+
*.tsbuildinfo
69+
70+
# Optional npm cache directory
71+
72+
.npm
73+
74+
# Optional eslint cache
75+
76+
.eslintcache
77+
78+
# Optional stylelint cache
79+
80+
.stylelintcache
81+
82+
# Microbundle cache
83+
84+
.rpt2_cache/
85+
.rts2_cache_cjs/
86+
.rts2_cache_es/
87+
.rts2_cache_umd/
88+
89+
# Optional REPL history
90+
91+
.node_repl_history
92+
93+
# Output of 'npm pack'
94+
95+
*.tgz
96+
97+
# Yarn Integrity file
98+
99+
.yarn-integrity
100+
101+
# dotenv environment variable files
102+
103+
.env
104+
.env.development.local
105+
.env.test.local
106+
.env.production.local
107+
.env.local
108+
109+
# parcel-bundler cache (https://parceljs.org/)
110+
111+
.parcel-cache
112+
113+
# Next.js build output
114+
115+
.next
116+
out
117+
118+
# Nuxt.js build / generate output
119+
120+
.nuxt
121+
dist
122+
123+
# Gatsby files
124+
125+
# Comment in the public line in if your project uses Gatsby and not Next.js
126+
127+
# https://nextjs.org/blog/next-9-1#public-directory-support
128+
129+
# public
130+
131+
# vuepress build output
132+
133+
.vuepress/dist
134+
135+
# vuepress v2.x temp and cache directory
136+
137+
.temp
138+
139+
# Docusaurus cache and generated files
140+
141+
.docusaurus
142+
143+
# Serverless directories
144+
145+
.serverless/
146+
147+
# FuseBox cache
148+
149+
.fusebox/
150+
151+
# DynamoDB Local files
152+
153+
.dynamodb/
154+
155+
# TernJS port file
156+
157+
.tern-port
158+
159+
# Stores VSCode versions used for testing VSCode extensions
160+
161+
.vscode-test
162+
163+
# yarn v2
164+
165+
.yarn/cache
166+
.yarn/unplugged
167+
.yarn/build-state.yml
168+
.yarn/install-state.gz
169+
.pnp.*
170+
171+
# IntelliJ based IDEs
172+
.idea
173+
174+
# Finder (MacOS) folder config
175+
.DS_Store

tooling/sparta-aws/src/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# sparta-discord-bot
2+
3+
To install dependencies:
4+
5+
```bash
6+
bun install
7+
```
8+
9+
To run:
10+
11+
```bash
12+
bun run dist/index.js
13+
```
14+
15+
This project was created using `bun init` in bun v1.1.43. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.

tooling/sparta-aws/src/bun.lockb

251 KB
Binary file not shown.

0 commit comments

Comments
 (0)