Hacking AWS CloudShell with Rust

· 3min · Pragmatic AI Labs

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:

  • Debug: ~151MB
  • Release: ~15MB

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.


Want expert ML/AI training? Visit paiml.com

For hands-on courses: DS500 Platform

Based on this article's content, here are some courses that might interest you:

  1. AWS Advanced AI Engineering (1 week) Production LLM architecture patterns using Rust, AWS, and Bedrock.

  2. Rust-Powered AWS Serverless (4 weeks) Learn to develop serverless applications on AWS using Rust and AWS Lambda. Master the fundamentals of serverless architecture while building practical applications and understanding performance optimizations.

  3. AWS AI Analytics: Enhancing Analytics Pipelines with AI (3 weeks) Transform analytics pipelines with AWS AI services, focusing on performance and cost optimization

  4. AWS AI Analytics: Building High-Performance Systems with Rust (3 weeks) Build high-performance AWS AI analytics systems using Rust, focusing on efficiency, telemetry, and production-grade implementations

  5. Generative AI with AWS (4 weeks) This GenAI course will guide you through everything you need to know to use generative AI on AWS

Learn more at Pragmatic AI Labs