Organize Files Structure
How to structure your Rust serverless project for real-world use.
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:
rust-serverless-api/
├── events/
│ └── event.json
├── rust_app/
│ ├── Cargo.toml
│ ├── src/
│ │ └── main.rs
├── README.md
├── samconfig.toml
└── template.yamlEverything 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
rust-serverless-api/
├── src/
│ ├── Cargo.toml
│ └── main.rs
├── README.md
├── samconfig.toml
└── template.yamlMove Cargo.toml to the root directory
rust-serverless-api/
├── src/
│ └── hello.rs
├── Cargo.toml
├── README.md
├── samconfig.toml
└── template.yamlMove main.rs to bin directory inside the src folder and rename it to hello.rs
rust-serverless-api/
├── src/
│ └── bin/
│ └── hello.rs
├── Cargo.toml
├── README.md
├── samconfig.toml
└── template.yamlThe
bindirectory 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.