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

  1. Open CloudShell and switch to ZSH for better AI-powered completions:
zsh
  1. 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

  1. Configure Rust installation paths:
export RUSTUP_HOME=/tmp/.rustup
export CARGO_HOME=/tmp/.cargo
  1. Install Rust:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Choose standard installation when prompted.

  1. Add Rust to your path:
source "/tmp/.cargo/env"
  1. Install the C compiler:
sudo yum install -y gcc

Creating the S3 Bucket Lister

  1. Create a new Rust project:
cd /tmp
cargo new s3-lister
cd s3-lister
  1. Add AWS SDK dependencies:
cargo add aws-config aws-sdk-s3 tokio --features tokio/full
  1. 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

  1. Debug build (larger but faster compilation):
cargo build
./target/debug/s3-lister
  1. Release build (smaller binary, optimized):
cargo build --release
./target/release/s3-lister

Binary size comparison:

Important Notes

  1. This is an ephemeral setup:

    • Everything in /tmp is cleared when CloudShell session ends
    • You'll need to reinstall Rust in new sessions
  2. 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.