Skip to content
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
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .evergreen/check-clippy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -o errexit

source ./.evergreen/configure-rust.sh
# source ./.evergreen/configure-rust.sh
source ./.evergreen/feature-combinations.sh

# Pin clippy to the latest version. This should be updated when new versions of Rust are released.
Expand Down
1 change: 1 addition & 0 deletions .evergreen/run-aws-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ set -o errexit
source ./.evergreen/configure-rust.sh

RUST_BACKTRACE=1 cargo test --features aws-auth auth_aws::auth_aws
RUST_BACKTRACE=1 cargo test --features aws-auth lambda_example::test_handler
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ derive_more = "0.99.13"
function_name = "0.2.1"
futures = "0.3"
home = "0.5"
lambda_runtime = "0.6.0"
pretty_assertions = "1.1.0"
serde_json = "1.0.64"
semver = "1.0.0"
Expand Down
5 changes: 5 additions & 0 deletions src/test/lambda_examples.rs
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;
Copy link
Contributor Author

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 the tests directory does not follow the module structure and therefore does not permit conditional compilation of an entire file.

57 changes: 57 additions & 0 deletions src/test/lambda_examples/auth.rs
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
48 changes: 48 additions & 0 deletions src/test/lambda_examples/no_auth.rs
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();
}
2 changes: 2 additions & 0 deletions src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ mod db;
#[cfg(all(not(feature = "sync"), not(feature = "tokio-sync")))]
mod documentation_examples;
mod index_management;
#[cfg(all(not(feature = "sync"), not(feature = "tokio-sync")))]
mod lambda_examples;
pub mod spec;
mod util;

Expand Down