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

Add AWS Lambda example #986

Merged
merged 2 commits into from
Aug 8, 2022
Merged

Add AWS Lambda example #986

merged 2 commits into from
Aug 8, 2022

Conversation

jyemin
Copy link
Contributor

@jyemin jyemin commented Jul 27, 2022

JAVA-4474

Note that there is only one example rather than a separate one for MONGODB-AWS authentication. My argument for it is that the AWS credentials should properly be obtained by the driver via either environment variables or, perhaps, EC2 or ECS endpoints, and can be used just by specifying a MONGODB_URI environment variable like:

mongodb+srv://<host>/?authMechanism=MONGODB-AWS

@jyemin jyemin requested a review from terakilobyte July 27, 2022 17:19
@jyemin jyemin self-assigned this Jul 27, 2022
import org.bson.Document;

// Start AWS Lambda Example 1
public class TestAwsLambdaHandler implements RequestHandler<String, String> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to suggest a different name for this class. It can be anything we want.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps ExampleAWSLambdaHandler

Suggested change
public class TestAwsLambdaHandler implements RequestHandler<String, String> {
public class ExampleAWSLambdaHandler implements RequestHandler<String, String> {

private final MongoClient client;

public TestAwsLambdaHandler() {
client = MongoClients.create(System.getenv("MONGODB_URI"));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create the client in the constructor

@terakilobyte
Copy link
Member

terakilobyte commented Jul 27, 2022

I wonder if we should also have an example of fetching those AWS credentials since it would help fit in the existing page structure. Eventually, we'll pull in all the examples and put them in tabs here.

@jyemin
Copy link
Contributor Author

jyemin commented Jul 27, 2022

The credentials are fetched internally to the driver via reading environment variables or accessing the EC2 endpoint (I think... I haven't actually tried it). There is no code to show in the application for that, except perhaps showing the template for the connection string indicating the authMechanism of MONGODB-AWS.

Copy link
Member

@terakilobyte terakilobyte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Would it also make sense to provide an example using this handler from within a lambda function? We'll make sure to point out how to structure the connection string much like we do for Node in https://www.mongodb.com/docs/atlas/manage-connections-aws-lambda/

import org.bson.Document;

// Start AWS Lambda Example 1
public class TestAwsLambdaHandler implements RequestHandler<String, String> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps ExampleAWSLambdaHandler

Suggested change
public class TestAwsLambdaHandler implements RequestHandler<String, String> {
public class ExampleAWSLambdaHandler implements RequestHandler<String, String> {

@jyemin
Copy link
Contributor Author

jyemin commented Aug 4, 2022

Perhaps ExampleAWSLambdaHandler

The Java "standard" is to use lowercase for acronyms, so I went with ExampleAwsLambdaHandler

@jyemin jyemin merged commit 8a9a781 into mongodb:master Aug 8, 2022
@jyemin jyemin deleted the j4474 branch August 8, 2022 13:16
ispringer pushed a commit to evergage/mongo-java-driver that referenced this pull request May 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants