5.5 KiB
description
| description |
|---|
| Build Rust Lambda function for AWS deployment with optimizations |
You are helping the user build their Rust Lambda function for AWS deployment.
Your Task
Guide the user through building their Lambda function with appropriate optimizations:
-
Verify project setup:
- Check that Cargo.toml has release profile optimizations
- Verify lambda_runtime dependency is present
- Confirm project compiles:
cargo check
-
Choose architecture: Ask the user which architecture to target:
- x86_64 (default): Compatible with most existing infrastructure
- ARM64 (Graviton2): 20% better price/performance, often faster cold starts
- Both: Build for both architectures
-
Build command:
For x86_64:
cargo lambda build --releaseFor ARM64 (recommended):
cargo lambda build --release --arm64For both:
cargo lambda build --release cargo lambda build --release --arm64With zip output (for manual deployment):
cargo lambda build --release --output-format zip -
Verify build:
- Check binary size:
ls -lh target/lambda/*/bootstrap - Typical sizes:
- Small function: 1-3 MB
- With AWS SDK: 5-10 MB
- Large dependencies: 10-20 MB
- If too large, suggest optimizations (see below)
- Check binary size:
-
Build output location:
- x86_64:
target/lambda/<function-name>/bootstrap - ARM64:
target/lambda/<function-name>/bootstrap(when building with --arm64) - Zip:
target/lambda/<function-name>.zip
- x86_64:
Release Profile Optimization
Ensure Cargo.toml has optimal release profile:
[profile.release]
opt-level = 'z' # Optimize for size (or 3 for speed)
lto = true # Link-time optimization
codegen-units = 1 # Better optimization (slower compile)
strip = true # Remove debug symbols
panic = 'abort' # Smaller panic handling
Optimization Tradeoffs
For smaller binary (faster cold start):
opt-level = 'z'
For faster execution:
opt-level = 3
Size Optimization Tips
If the binary is too large:
-
Check dependencies:
cargo treeLook for unnecessary or duplicate dependencies
-
Use feature flags:
# Only enable needed features tokio = { version = "1", features = ["macros", "rt"] } # Instead of: # tokio = { version = "1", features = ["full"] } -
Audit with cargo-bloat:
cargo install cargo-bloat cargo bloat --release -n 20 -
Consider lighter alternatives:
- Use
ureqinstead ofreqwestfor simple HTTP - Use
rustlsinstead ofnative-tls - Minimize AWS SDK crates
- Use
-
Remove unused code:
- Ensure
strip = truein profile - Use
cargo-unused-featuresto find unused features
- Ensure
Cross-Compilation Requirements
cargo-lambda uses Zig for cross-compilation. If you encounter issues:
-
Install Zig:
# macOS brew install zig # Linux (download from ziglang.org) wget https://ziglang.org/download/0.11.0/zig-linux-x86_64-0.11.0.tar.xz tar xf zig-linux-x86_64-0.11.0.tar.xz export PATH=$PATH:$PWD/zig-linux-x86_64-0.11.0 -
Verify Zig:
zig version
Build Flags
Additional useful flags:
# Build specific binary in workspace
cargo lambda build --release --bin my-function
# Build all binaries
cargo lambda build --release --all
# Build with compiler flags
cargo lambda build --release -- -C target-cpu=native
# Verbose output
cargo lambda build --release --verbose
Testing the Build
After building, test locally:
# Start local Lambda runtime
cargo lambda watch
# In another terminal, invoke the function
cargo lambda invoke --data-ascii '{"test": "data"}'
Build Performance
Speed up builds:
-
Use sccache:
cargo install sccache export RUSTC_WRAPPER=sccache -
Parallel compilation (already enabled by default)
-
Incremental compilation (for development):
[profile.dev] incremental = true
Architecture Decision Guide
Choose x86_64 when:
- Need compatibility with existing x86 infrastructure
- Using dependencies that don't support ARM64
- Already have x86 configuration/scripts
Choose ARM64 when:
- Want better price/performance (20% savings)
- Need faster execution
- Want potentially faster cold starts
- Starting fresh project (recommended)
Build both when:
- Want to test both architectures
- Supporting multiple deployment targets
- Migrating from x86 to ARM
Common Build Issues
Issue: "Zig not found"
Solution: Install Zig (see above)
Issue: "Cannot find -lssl"
Solution: Install OpenSSL development files
# Ubuntu/Debian
sudo apt-get install libssl-dev pkg-config
# macOS
brew install openssl
Issue: "Binary too large" (>50MB uncompressed)
Solution:
- Review dependencies with
cargo tree - Enable all size optimizations
- Consider splitting into multiple functions
Issue: Build succeeds but Lambda fails
Solution:
- Ensure building for correct architecture
- Test locally with
cargo lambda watch - Check CloudWatch logs for specific errors
Next Steps
After successful build:
- Test locally:
/lambda-invokeorcargo lambda watch - Deploy: Use
/lambda-deploy - Set up CI/CD: Use
/lambda-github-actions
Report the build results including:
- Binary size
- Architecture
- Build time
- Any warnings or suggestions