-
Notifications
You must be signed in to change notification settings - Fork 173
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
RUST-1253 Add AWS Lambda Examples #714
Merged
isabelatkinson
merged 8 commits into
mongodb:main
from
isabelatkinson:aws-lambda-examples/RUST-1253
Aug 15, 2022
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
57902ef
RUST-1253 Add AWS Lambda Examples
isabelatkinson 169a1c1
refactor
isabelatkinson 8519853
add new files
isabelatkinson cb67544
revert script
isabelatkinson 30dbad5
fix test invocation
isabelatkinson 9f93794
create client manually for test
isabelatkinson 533469e
skip on versioned api
isabelatkinson e8cae75
revert script
isabelatkinson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
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 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(()) | ||
} | ||
|
||
#[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(event).await.unwrap(); | ||
} | ||
// end lambda connection example 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
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() { | ||
let event = LambdaEvent::new(Value::Null, Default::default()); | ||
handler(event).await.unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.