Skip to content

hollanddd/axum-lambda-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rust Axum API on AWS Lambda

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.

Features

  • 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

Project Structure

├── api/           # Rust Axum API
│   ├── src/
│   │   └── main.rs
│   └── Cargo.toml
└── cdk/           # AWS CDK Infrastructure
    ├── src/
    │   └── app.ts
    └── package.json

Quick Start

Prerequisites

  • Rust (with cargo)
  • Node.js and npm
  • AWS CLI configured (for deployment)

Local Development

Run the API locally

cd api
cargo run

The API will be available at http://localhost:3000

Test the endpoints

# POST data
curl -X POST http://localhost:3000/ \
  -H "Content-Type: application/json" \
  -d '{"name": "test"}'

# GET data
curl http://localhost:3000/

AWS Deployment

Install CDK dependencies

cd cdk
npm install

Deploy to AWS

npx cdk deploy

The deployment will output a Function URL that you can use to access your API.

API Endpoints

  • POST / - Add new data item
    • Body: {"name": "string"}
    • Returns: 201 Created
  • GET / - Retrieve all data items
    • Returns: 200 OK with JSON array

Architecture

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 during cdk deploy

This approach allows seamless development and testing locally while deploying the same codebase to Lambda.

Dependencies

Rust (api/)

  • 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)

TypeScript CDK (cdk/)

  • @cdklabs/aws-lambda-rust: CDK construct for Rust Lambda functions
  • aws-cdk-lib: Core AWS CDK library

Development Commands

Rust API

cd api
cargo run          # Run locally
cargo build        # Build debug
cargo build --release  # Build for Lambda

CDK Infrastructure

cd 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

References

About

Axum lambda api example

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors