A serverless REST API built with Rust and Axum that runs both locally and on AWS Lambda. This project demonstrates how to build a modern Rust web service with conditional compilation for development and production environments.
- Dual Runtime: Runs as local server in debug mode, AWS Lambda in release mode
- Modern Stack: Axum web framework with tower-http middleware
- Infrastructure as Code: AWS CDK TypeScript for deployment
- CORS Support: Configured for cross-origin requests
- Compression: Built-in gzip and deflate compression
- Structured Logging: JSON logging optimized for CloudWatch
├── api/ # Rust Axum API
│ ├── src/
│ │ └── main.rs
│ └── Cargo.toml
└── cdk/ # AWS CDK Infrastructure
├── src/
│ └── app.ts
└── package.json
- Rust (with cargo)
- Node.js and npm
- AWS CLI configured (for deployment)
cd api
cargo runThe API will be available at http://localhost:3000
# POST data
curl -X POST http://localhost:3000/ \
-H "Content-Type: application/json" \
-d '{"name": "test"}'
# GET data
curl http://localhost:3000/cd cdk
npm installnpx cdk deployThe deployment will output a Function URL that you can use to access your API.
POST /- Add new data item- Body:
{"name": "string"} - Returns: 201 Created
- Body:
GET /- Retrieve all data items- Returns: 200 OK with JSON array
The application uses conditional compilation to provide different runtime behaviors:
- Debug Mode (
cargo run): Starts a local TCP server on port 3000 - Release Mode (
cargo build --release): AWS Lambda Runtime compiles duringcdk deploy
This approach allows seamless development and testing locally while deploying the same codebase to Lambda.
- axum: Modern web framework
- lambda_http: AWS Lambda HTTP integration
- axum-aws-lambda: Bridge between Axum and Lambda
- tower-http: HTTP middleware (CORS, compression, tracing)
- @cdklabs/aws-lambda-rust: CDK construct for Rust Lambda functions
- aws-cdk-lib: Core AWS CDK library
cd api
cargo run # Run locally
cargo build # Build debug
cargo build --release # Build for Lambdacd cdk
npm install # Install dependencies
npm run build # Compile TypeScript
npm run watch # Watch for changes
npm test # Run tests
npx cdk deploy # Deploy to AWS
npx cdk diff # Compare with deployed stack
npx cdk synth # Generate CloudFormation