-
Notifications
You must be signed in to change notification settings - Fork 180
RUST-1253 Add AWS Lambda Examples #714
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
57902ef
169a1c1
8519853
cb67544
30dbad5
9f93794
533469e
e8cae75
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#![allow(dead_code)] | ||
|
||
#[cfg(feature = "aws-auth")] | ||
mod auth; | ||
mod no_auth; | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use crate as mongodb; | ||
|
||
// begin lambda connection example 2 | ||
use async_once::AsyncOnce; | ||
use lambda_runtime::{service_fn, LambdaEvent}; | ||
use lazy_static::lazy_static; | ||
use mongodb::{ | ||
bson::doc, | ||
options::{AuthMechanism, ClientOptions, Credential}, | ||
Client, | ||
}; | ||
use serde_json::Value; | ||
|
||
// Initialize a global static MongoDB Client with AWS authentication. The following environment | ||
// variables should also be set: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and, optionally, | ||
// AWS_SESSION_TOKEN. | ||
// | ||
// The client can be accessed as follows: | ||
// let client = MONGODB_CLIENT.get().await; | ||
lazy_static! { | ||
static ref MONGODB_CLIENT: AsyncOnce<Client> = AsyncOnce::new(async { | ||
let uri = std::env::var("MONGODB_URI") | ||
.expect("MONGODB_URI must be set to the URI of the MongoDB deployment"); | ||
let mut options = ClientOptions::parse(uri) | ||
.await | ||
.expect("Failed to parse options from URI"); | ||
let credential = Credential::builder() | ||
.mechanism(AuthMechanism::MongoDbAws) | ||
.build(); | ||
options.credential = Some(credential); | ||
Client::with_options(options).expect("Failed to create MongoDB Client") | ||
}); | ||
} | ||
|
||
// Runs a ping operation on the "db" database and returns the response. | ||
async fn handler(_: LambdaEvent<Value>) -> Result<Value, lambda_runtime::Error> { | ||
let client = MONGODB_CLIENT.get().await; | ||
let response = client | ||
.database("db") | ||
.run_command(doc! { "ping": 1 }, None) | ||
.await?; | ||
let json = serde_json::to_value(response)?; | ||
Ok(json) | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), lambda_runtime::Error> { | ||
let service = service_fn(handler); | ||
lambda_runtime::run(service).await?; | ||
Ok(()) | ||
} | ||
// end lambda connection example 2 | ||
|
||
// Runs a ping operation on the "db" database and returns the response. | ||
async fn handler_create_client(_: LambdaEvent<Value>) -> Result<Value, lambda_runtime::Error> { | ||
let uri = std::env::var("MONGODB_URI").unwrap(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. our URI for the AWS auth tests is configured a little differently than what is done for |
||
let client = Client::with_uri_str(uri).await.unwrap(); | ||
let response = client | ||
.database("db") | ||
.run_command(doc! { "ping": 1 }, None) | ||
.await?; | ||
let json = serde_json::to_value(response)?; | ||
Ok(json) | ||
} | ||
|
||
#[cfg_attr(feature = "tokio-runtime", tokio::test(flavor = "multi_thread"))] | ||
#[cfg_attr(feature = "async-std-runtime", async_std::test)] | ||
async fn test_handler() { | ||
let event = LambdaEvent::new(Value::Null, Default::default()); | ||
handler_create_client(event).await.unwrap(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use crate as mongodb; | ||
|
||
// begin lambda connection example 1 | ||
use async_once::AsyncOnce; | ||
use lambda_runtime::{service_fn, LambdaEvent}; | ||
use lazy_static::lazy_static; | ||
use mongodb::{bson::doc, Client}; | ||
use serde_json::Value; | ||
|
||
// Initialize a global static MongoDB Client. | ||
// | ||
// The client can be accessed as follows: | ||
// let client = MONGODB_CLIENT.get().await; | ||
lazy_static! { | ||
static ref MONGODB_CLIENT: AsyncOnce<Client> = AsyncOnce::new(async { | ||
let uri = std::env::var("MONGODB_URI") | ||
.expect("MONGODB_URI must be set to the URI of the MongoDB deployment"); | ||
Client::with_uri_str(uri) | ||
.await | ||
.expect("Failed to create MongoDB Client") | ||
}); | ||
} | ||
|
||
// Runs a ping operation on the "db" database and returns the response. | ||
async fn handler(_: LambdaEvent<Value>) -> Result<Value, lambda_runtime::Error> { | ||
let client = MONGODB_CLIENT.get().await; | ||
let response = client | ||
.database("db") | ||
.run_command(doc! { "ping": 1 }, None) | ||
.await?; | ||
let json = serde_json::to_value(response)?; | ||
Ok(json) | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), lambda_runtime::Error> { | ||
let service = service_fn(handler); | ||
lambda_runtime::run(service).await?; | ||
Ok(()) | ||
} | ||
// end lambda connection example 1 | ||
|
||
#[cfg_attr(feature = "tokio-runtime", tokio::test(flavor = "multi_thread"))] | ||
#[cfg_attr(feature = "async-std-runtime", async_std::test)] | ||
async fn test_handler() { | ||
if std::env::var("MONGODB_API_VERSION").is_ok() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A similar URI configuration issue -- it didn't seem super important to get versioned API coverage here so I just skipped the test. |
||
return; | ||
} | ||
let event = LambdaEvent::new(Value::Null, Default::default()); | ||
handler(event).await.unwrap(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are split into two separate modules to avoid including conditional compilation statements within the examples. Although we've talked about containing our examples in the top-level
tests
directory, I needed to include these ones here instead because thetests
directory does not follow the module structure and therefore does not permit conditional compilation of an entire file.