└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # Rust on AWS 2 | 3 | ## Prerequisites 4 | 5 | You will need to create an AWS accounts and setup your access keys. Follow the instructions [here](https://docs.aws.amazon.com/sdk-for-rust/latest/dg/getting-started.html#getting-started-prerequisites). 6 | 7 | ## AWS Rust SDK 8 | 9 | ### Upload file to Amazon S3 bucket 10 | 11 | 1. Create new project 12 | 13 | ```shell 14 | cargo new aws_rust_sdk 15 | ``` 16 | 17 | 2. Add dependencies (tokio, aws-config, aws-sdk-s3) 18 | 19 | ```shell 20 | cargo add tokio@1 --features full 21 | cargo add aws-config 22 | cargo add aws-sdk-s3 23 | ``` 24 | 25 | Your dependencies on `Cargo.toml` will look like this: 26 | 27 | ```toml 28 | [dependencies] 29 | aws-config = "0.48.0" 30 | aws-sdk-s3 = "0.18.0" 31 | tokio = { version = "1", features = ["full"] } 32 | ``` 33 | 34 | 3. We will use [tokio](https://tokio.rs) as our asynchronous runtime: 35 | 36 | ```rust 37 | #[tokio::main] 38 | async fn main() { 39 | // Our code will go here 40 | } 41 | ``` 42 | 43 | 4. Create an `SdkConfig` by loading the default configuration from the environment: 44 | 45 | ```rust 46 | let config = aws_config::load_from_env().await; 47 | ``` 48 | 49 | 5. Create and `aws_sdk_s3::Client`: 50 | 51 | ```rust 52 | let client = aws_sdk_s3::Client::new(&config); 53 | ``` 54 | 55 | 6. Create a `ByteStream` from the file that will be uploaded: 56 | 57 | ```rust 58 | let body = aws_sdk_s3::types::ByteStream::from_path(std::path::Path::new("test.txt")).await; 59 | ``` 60 | 61 | 7. Create a [`PutObject`](https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutObject.html) request: 62 | 63 | ```rust 64 | let response = client 65 | .put_object() 66 | .bucket("bucket-name") 67 | .key("key") 68 | .body(body.unwrap()) 69 | .send() 70 | .await?; 71 | ``` 72 | 73 | 8. Get the object version: 74 | 75 | ```rust 76 | let version = response.version_id().unwrap(); 77 | println!("Uploaded file version {}", version); 78 | ``` 79 | 80 | Our entire `main.rs` file will look like this: 81 | 82 | ```rust 83 | use aws_sdk_s3::{types::ByteStream, Client, Error}; 84 | use std::path::Path; 85 | 86 | #[tokio::main] 87 | async fn main() -> Result<(), Error> { 88 | let config = aws_config::load_from_env().await; 89 | let client = Client::new(&config); 90 | 91 | let body = ByteStream::from_path(Path::new("test.txt")).await; 92 | 93 | let response = client 94 | .put_object() 95 | .bucket("tech-day-ifood") 96 | .key("demo") 97 | .body(body.unwrap()) 98 | .send() 99 | .await?; 100 | 101 | let version = response.version_id().unwrap(); 102 | println!("Uploaded file version {}", version); 103 | 104 | Ok(()) 105 | } 106 | ``` 107 | 108 | - [AWS Rust SDK](https://aws.amazon.com/sdk-for-rust/) 109 | - [Developer Guide](https://docs.aws.amazon.com/sdk-for-rust/latest/dg/welcome.html) 110 | - [GitHub repo](https://github.com/awslabs/aws-sdk-rust) 111 | - [Exemplos de código](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/rust_dev_preview) 112 | - [Roadmap](https://github.com/orgs/awslabs/projects/50/views/1) 113 | 114 | ## AWS Lambda Rust Runtime 115 | 116 | 1. Install [cargo-lambda](https://github.com/cargo-lambda/cargo-lambda) 117 | 118 | ```shell 119 | brew tap cargo-lambda/cargo-lambda 120 | brew install cargo-lambda 121 | ``` 122 | 123 | 2. Create a function 124 | 125 | ```shell 126 | cargo lambda new FUNCTION_NAME 127 | ``` 128 | 129 | 3. Build 130 | 131 | ```shell 132 | cargo lambda build --release 133 | cargo lambda build --release --arm64 134 | ``` 135 | 136 | 4. Deploy 137 | 138 | ```shell 139 | cargo lambda deploy \ 140 | --iam-role arn:aws:iam::XXXXXXXXXXXXX:role/your_lambda_execution_role 141 | ``` 142 | 143 | 5. Test 144 | 145 | ```shell 146 | cargo lambda invoke --remote \ 147 | --data-ascii '{"command": "hi"}' \ 148 | --output-format json \ 149 | FUNCTION_NAME 150 | ``` 151 | 152 | - [Rust Runtime for AWS Lambda](https://github.com/awslabs/aws-lambda-rust-runtime) 153 | - [cargo-lambda](https://github.com/cargo-lambda/cargo-lambda) 154 | 155 | ## Links 156 | 157 | - [Rust on AWS](https://aws.amazon.com/developer/language/rust/) 158 | 159 | ### Blogs 160 | 161 | - [Innovating with Rust](https://aws.amazon.com/blogs/opensource/innovating-with-rust/) 162 | - [A New AWS SDK for Rust – Alpha Launch](https://aws.amazon.com/blogs/developer/a-new-aws-sdk-for-rust-alpha-launch/) 163 | - [How our AWS Rust team will contribute to Rust’s future successes](https://aws.amazon.com/blogs/opensource/how-our-aws-rust-team-will-contribute-to-rusts-future-successes/) 164 | - [Why AWS loves Rust, and how we’d like to help](https://aws.amazon.com/blogs/opensource/why-aws-loves-rust-and-how-wed-like-to-help/) 165 | - [Rust Runtime for AWS Lambda](https://aws.amazon.com/blogs/opensource/rust-runtime-for-aws-lambda/) 166 | 167 | ### Videos 168 | 169 | - [Rust Linz June 2022 - How AWS is building the Rust SDK and how you can use it today (Zelda Hessler)](https://youtu.be/N0XMjokwTIM) 170 | - [AWS Summit San Francisco 2022 - Sustainability with Rust](https://youtu.be/BHRLbMCpmtY) 171 | - [AWS re:Invent 2021 - Building with the new AWS SDKs for Rust, Kotlin, and Swift](https://youtu.be/Nhk1K1AjYvg) 172 | - [AWS re:Invent 2021 - Using Rust to minimize environmental impact](https://youtu.be/yQZaBtUjQ1w) 173 | - [AWS re:Invent 2020: Next-gen networking infrastructure with Rust and Tokio](https://youtu.be/MZyleK8elPk) 174 | --------------------------------------------------------------------------------