TutorialbeginnerPart 4 of 18

Organize Files Structure

How to structure your Rust serverless project for real-world use.

April 13, 20262 min read
Prerequisites:Installation of RustBootstrap Rust Template

When you run sam init, you get a project that puts everything in a single folder. This is fine for a demo, but once you start adding actual features and more functions, it gets hard to manage.

After the bootstrap, your project looks like this:

Terminal
bash
rust-serverless-api/
├── events/
│   └── event.json
├── rust_app/
│   ├── Cargo.toml
│   ├── src/
│   │   └── main.rs
├── README.md
├── samconfig.toml
└── template.yaml

Everything is inside rust_app even though we have our root directory rust-serverless-api

Rearrange your files as this and remove what is not needed

Terminal
bash
rust-serverless-api/
├── src/
│   ├── Cargo.toml
│   └── main.rs
├── README.md
├── samconfig.toml
└── template.yaml

Move Cargo.toml to the root directory

Terminal
bash
rust-serverless-api/
├── src/
│   └── hello.rs
├── Cargo.toml
├── README.md
├── samconfig.toml
└── template.yaml

Move main.rs to bin directory inside the src folder and rename it to hello.rs

Terminal
bash
rust-serverless-api/
├── src/
│   └── bin/
│       └── hello.rs
├── Cargo.toml
├── README.md
├── samconfig.toml
└── template.yaml

The bin directory is where your lambda function code lives for different endpoints you may have

Now that the files are organized, we can start to look what our configuration files looks like and editing them for our needs.