You will need to create an AWS accounts and setup your access keys. Follow the instructions here.
- Create new project
cargo new aws_rust_sdk
- Add dependencies (tokio, aws-config, aws-sdk-s3)
cargo add tokio@1 --features full
cargo add aws-config
cargo add aws-sdk-s3
Your dependencies on Cargo.toml
will look like this:
[dependencies]
aws-config = "0.48.0"
aws-sdk-s3 = "0.18.0"
tokio = { version = "1", features = ["full"] }
- We will use tokio as our asynchronous runtime:
#[tokio::main]
async fn main() {
// Our code will go here
}
- Create an
SdkConfig
by loading the default configuration from the environment:
let config = aws_config::load_from_env().await;
- Create and
aws_sdk_s3::Client
:
let client = aws_sdk_s3::Client::new(&config);
- Create a
ByteStream
from the file that will be uploaded:
let body = aws_sdk_s3::types::ByteStream::from_path(std::path::Path::new("test.txt")).await;
- Create a
PutObject
request:
let response = client
.put_object()
.bucket("bucket-name")
.key("key")
.body(body.unwrap())
.send()
.await?;
- Get the object version:
let version = response.version_id().unwrap();
println!("Uploaded file version {}", version);
Our entire main.rs
file will look like this:
use aws_sdk_s3::{types::ByteStream, Client, Error};
use std::path::Path;
#[tokio::main]
async fn main() -> Result<(), Error> {
let config = aws_config::load_from_env().await;
let client = Client::new(&config);
let body = ByteStream::from_path(Path::new("test.txt")).await;
let response = client
.put_object()
.bucket("tech-day-ifood")
.key("demo")
.body(body.unwrap())
.send()
.await?;
let version = response.version_id().unwrap();
println!("Uploaded file version {}", version);
Ok(())
}
- Install cargo-lambda
brew tap cargo-lambda/cargo-lambda
brew install cargo-lambda
- Create a function
cargo lambda new FUNCTION_NAME
- Build
cargo lambda build --release
cargo lambda build --release --arm64
- Deploy
cargo lambda deploy \
--iam-role arn:aws:iam::XXXXXXXXXXXXX:role/your_lambda_execution_role
- Test
cargo lambda invoke --remote \
--data-ascii '{"command": "hi"}' \
--output-format json \
FUNCTION_NAME
- Innovating with Rust
- A New AWS SDK for Rust – Alpha Launch
- How our AWS Rust team will contribute to Rust’s future successes
- Why AWS loves Rust, and how we’d like to help
- Rust Runtime for AWS Lambda
- Rust Linz June 2022 - How AWS is building the Rust SDK and how you can use it today (Zelda Hessler)
- AWS Summit San Francisco 2022 - Sustainability with Rust
- AWS re:Invent 2021 - Building with the new AWS SDKs for Rust, Kotlin, and Swift
- AWS re:Invent 2021 - Using Rust to minimize environmental impact
- AWS re:Invent 2020: Next-gen networking infrastructure with Rust and Tokio