Skip to content

Commit bf10dbe

Browse files
RUST-1253 Add AWS Lambda Examples (#714)
1 parent 8694ef5 commit bf10dbe

File tree

6 files changed

+131
-0
lines changed

6 files changed

+131
-0
lines changed

.evergreen/run-aws-tests.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,4 @@ set -o errexit
4545
source ./.evergreen/configure-rust.sh
4646

4747
RUST_BACKTRACE=1 cargo test --features aws-auth auth_aws::auth_aws
48+
RUST_BACKTRACE=1 cargo test --features aws-auth lambda_examples::auth::test_handler

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ derive_more = "0.99.13"
162162
function_name = "0.2.1"
163163
futures = "0.3"
164164
home = "0.5"
165+
lambda_runtime = "0.6.0"
165166
pretty_assertions = "1.1.0"
166167
serde = { version = "*", features = ["rc"] }
167168
serde_json = "1.0.64"

src/test/lambda_examples.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#![allow(dead_code)]
2+
3+
#[cfg(feature = "aws-auth")]
4+
mod auth;
5+
mod no_auth;

src/test/lambda_examples/auth.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
use crate as mongodb;
2+
3+
// begin lambda connection example 2
4+
use async_once::AsyncOnce;
5+
use lambda_runtime::{service_fn, LambdaEvent};
6+
use lazy_static::lazy_static;
7+
use mongodb::{
8+
bson::doc,
9+
options::{AuthMechanism, ClientOptions, Credential},
10+
Client,
11+
};
12+
use serde_json::Value;
13+
14+
// Initialize a global static MongoDB Client with AWS authentication. The following environment
15+
// variables should also be set: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and, optionally,
16+
// AWS_SESSION_TOKEN.
17+
//
18+
// The client can be accessed as follows:
19+
// let client = MONGODB_CLIENT.get().await;
20+
lazy_static! {
21+
static ref MONGODB_CLIENT: AsyncOnce<Client> = AsyncOnce::new(async {
22+
let uri = std::env::var("MONGODB_URI")
23+
.expect("MONGODB_URI must be set to the URI of the MongoDB deployment");
24+
let mut options = ClientOptions::parse(uri)
25+
.await
26+
.expect("Failed to parse options from URI");
27+
let credential = Credential::builder()
28+
.mechanism(AuthMechanism::MongoDbAws)
29+
.build();
30+
options.credential = Some(credential);
31+
Client::with_options(options).expect("Failed to create MongoDB Client")
32+
});
33+
}
34+
35+
// Runs a ping operation on the "db" database and returns the response.
36+
async fn handler(_: LambdaEvent<Value>) -> Result<Value, lambda_runtime::Error> {
37+
let client = MONGODB_CLIENT.get().await;
38+
let response = client
39+
.database("db")
40+
.run_command(doc! { "ping": 1 }, None)
41+
.await?;
42+
let json = serde_json::to_value(response)?;
43+
Ok(json)
44+
}
45+
46+
#[tokio::main]
47+
async fn main() -> Result<(), lambda_runtime::Error> {
48+
let service = service_fn(handler);
49+
lambda_runtime::run(service).await?;
50+
Ok(())
51+
}
52+
// end lambda connection example 2
53+
54+
// Runs a ping operation on the "db" database and returns the response.
55+
async fn handler_create_client(_: LambdaEvent<Value>) -> Result<Value, lambda_runtime::Error> {
56+
let uri = std::env::var("MONGODB_URI").unwrap();
57+
let client = Client::with_uri_str(uri).await.unwrap();
58+
let response = client
59+
.database("db")
60+
.run_command(doc! { "ping": 1 }, None)
61+
.await?;
62+
let json = serde_json::to_value(response)?;
63+
Ok(json)
64+
}
65+
66+
#[cfg_attr(feature = "tokio-runtime", tokio::test(flavor = "multi_thread"))]
67+
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
68+
async fn test_handler() {
69+
let event = LambdaEvent::new(Value::Null, Default::default());
70+
handler_create_client(event).await.unwrap();
71+
}

src/test/lambda_examples/no_auth.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use crate as mongodb;
2+
3+
// begin lambda connection example 1
4+
use async_once::AsyncOnce;
5+
use lambda_runtime::{service_fn, LambdaEvent};
6+
use lazy_static::lazy_static;
7+
use mongodb::{bson::doc, Client};
8+
use serde_json::Value;
9+
10+
// Initialize a global static MongoDB Client.
11+
//
12+
// The client can be accessed as follows:
13+
// let client = MONGODB_CLIENT.get().await;
14+
lazy_static! {
15+
static ref MONGODB_CLIENT: AsyncOnce<Client> = AsyncOnce::new(async {
16+
let uri = std::env::var("MONGODB_URI")
17+
.expect("MONGODB_URI must be set to the URI of the MongoDB deployment");
18+
Client::with_uri_str(uri)
19+
.await
20+
.expect("Failed to create MongoDB Client")
21+
});
22+
}
23+
24+
// Runs a ping operation on the "db" database and returns the response.
25+
async fn handler(_: LambdaEvent<Value>) -> Result<Value, lambda_runtime::Error> {
26+
let client = MONGODB_CLIENT.get().await;
27+
let response = client
28+
.database("db")
29+
.run_command(doc! { "ping": 1 }, None)
30+
.await?;
31+
let json = serde_json::to_value(response)?;
32+
Ok(json)
33+
}
34+
35+
#[tokio::main]
36+
async fn main() -> Result<(), lambda_runtime::Error> {
37+
let service = service_fn(handler);
38+
lambda_runtime::run(service).await?;
39+
Ok(())
40+
}
41+
// end lambda connection example 1
42+
43+
#[cfg_attr(feature = "tokio-runtime", tokio::test(flavor = "multi_thread"))]
44+
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
45+
async fn test_handler() {
46+
if std::env::var("MONGODB_API_VERSION").is_ok() {
47+
return;
48+
}
49+
let event = LambdaEvent::new(Value::Null, Default::default());
50+
handler(event).await.unwrap();
51+
}

src/test/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ mod db;
1010
#[cfg(all(not(feature = "sync"), not(feature = "tokio-sync")))]
1111
mod documentation_examples;
1212
mod index_management;
13+
#[cfg(all(not(feature = "sync"), not(feature = "tokio-sync")))]
14+
mod lambda_examples;
1315
pub mod spec;
1416
mod util;
1517

0 commit comments

Comments
 (0)