Hacking AWS CloudShell with Rust
2024-11-29
Hacking AWS CloudShell with Rust: Building an S3 Bucket Lister
Introduction
AWS CloudShell provides a browser-based shell environment with AWS credentials pre-configured. While Rust isn't officially supported, we can hack it to work by using the /tmp
directory to circumvent space limitations.
Getting Started with CloudShell
- Open CloudShell and switch to ZSH for better AI-powered completions:
zsh
- Check available space:
df -h
Note: Your home directory has limited space (~1GB), but /tmp
has more room for our hack.
Setting Up Rust in CloudShell
- Configure Rust installation paths:
export RUSTUP_HOME=/tmp/.rustup
export CARGO_HOME=/tmp/.cargo
- Install Rust:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Choose standard installation when prompted.
- Add Rust to your path:
source "/tmp/.cargo/env"
- Install the C compiler:
sudo yum install -y gcc
Creating the S3 Bucket Lister
- Create a new Rust project:
cd /tmp
cargo new s3-lister
cd s3-lister
- Add AWS SDK dependencies:
cargo add aws-config aws-sdk-s3 tokio --features tokio/full
- Replace
src/main.rs
content:
use aws_config::{load_defaults, BehaviorVersion};
use aws_sdk_s3::Client;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = load_defaults(BehaviorVersion::latest()).await;
let client = Client::new(&config);
let buckets = client.list_buckets().send().await?;
for bucket in buckets.buckets() {
if let Some(name) = bucket.name() {
println!("{}", name);
}
}
Ok(())
}
Building and Running
- Debug build (larger but faster compilation):
cargo build
./target/debug/s3-lister
- Release build (smaller binary, optimized):
cargo build --release
./target/release/s3-lister
Binary size comparison:
- Debug: ~151MB
- Release: ~15MB
Important Notes
-
This is an ephemeral setup:
- Everything in
/tmp
is cleared when CloudShell session ends - You'll need to reinstall Rust in new sessions
- Everything in
-
CloudShell advantages:
- Pre-configured AWS credentials
- No local setup required
- ZSH with Amazon Q completion
Troubleshooting
If cargo
command isn't found:
export PATH="/tmp/.cargo/bin:$PATH"
If compilation fails:
sudo yum install -y gcc
Conclusion
While not officially supported, this hack lets you use Rust in CloudShell for quick AWS development. The release build produces an efficient binary that you can use for testing AWS services directly in the browser.