-
Notifications
You must be signed in to change notification settings - Fork 1.3k
CSHARP-4127: Language specific examples for AWS Lambda. #779
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
433a512
097a9a8
5fa4b01
f420582
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,91 @@ | ||
/* Copyright 2010-present MongoDB Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#if NETCOREAPP3_1_OR_GREATER | ||
using Amazon.Lambda.Core; | ||
using MongoDB.Bson; | ||
using MongoDB.Driver; | ||
using System; | ||
|
||
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class. | ||
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))] | ||
|
||
namespace LambdaTest | ||
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.
|
||
{ | ||
public class ShareMongoClientLambdaHandler | ||
{ | ||
// Start AWS Lambda Example 1 | ||
private static MongoClient MongoClient { get; set; } | ||
private static MongoClient CreateMongoClient() | ||
{ | ||
var mongoClientSettings = MongoClientSettings.FromConnectionString($"<MONGODB_URI>"); | ||
mongoClientSettings.ServerApi = new ServerApi(ServerApiVersion.V1, strict: true); | ||
return new MongoClient(mongoClientSettings); | ||
} | ||
|
||
static ShareMongoClientLambdaHandler() | ||
{ | ||
MongoClient = CreateMongoClient(); | ||
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. Is it guaranteed that the ctor will be called once/not concurrently? 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. Recommended way I found was about regular ctor, see for example here https://blog.steadycoding.com/using-singletons-in-net-core-in-aws-lambda/. The note what I didn't mention before, that there are 2 modes for AWS Lambda called "Cold" and "Warm" start ("Hot is a bit different): https://medium.com/@danielmanchev/cold-warm-and-hot-start-in-aws-lambda-bc8d64f28575. So the actually reused instance will be only after 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. From the first glance I fail to see the reason for initializing a static property in regular ctor. Do we know why is this the recommended way, and is it AWS recommendation as well? 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. 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. Yes, note that 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. it's true, I forgot that suggested MongoClient is static. I think it can work in any way, but I will look at this one more time :( 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. I think it's fine to take what is suggested in the VS templates (ie official place). So I removed static modifier 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. I asked this in this issue just in case awsdocs/aws-lambda-developer-guide#360 |
||
} | ||
|
||
public string HandleRequest(ILambdaContext context) | ||
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. In all samples a came across, I see additional Can it be omitted, and do you happen to have some official sample without it? 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. it can be omitted 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.
I just saw that it works without this argument |
||
{ | ||
var database = MongoClient.GetDatabase("db"); | ||
var collection = database.GetCollection<BsonDocument>("coll"); | ||
var result = collection.Find(FilterDefinition<BsonDocument>.Empty).First(); | ||
return result.ToString(); | ||
} | ||
// End AWS Lambda Example 1 | ||
} | ||
|
||
public class ConnectUsingAwsIamAuthenticatorLambdaHandler | ||
{ | ||
private static MongoClient MongoClient { get; set; } | ||
private static MongoClient CreateMongoClient() | ||
{ | ||
// Start AWS Lambda Example 2 | ||
string username = Environment.GetEnvironmentVariable("AWS_ACCESS_KEY_ID"); | ||
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. Are environment variables part of the Aws Lambda auth process? 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. it's not specified in our envs, but I saw it in other examples exactly in this form, so I would leave it as here 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. In this case I wonder if 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. As far as I know, it's a common way to expect these variables filled like you can see here: https://docs.aws.amazon.com/vsts/latest/userguide/lambda-netcore-deploy.html. Additionally it simplifies using this code in the driver if we want to run it. If I recall correctly, we can even not specify these values explicitly and they still be used by the driver implicitly (but not sure off the top of my head about details). I can check it later if you think that it makes sense 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. Looks like it's only one of the options, used in case of "build agent process", not sure what it is :) Not a big issue, I just think it introduces some "noise" to the sample, and not sure we got any use for it. 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. At the very least these variables present in the examples that we needed to mimic: https://www.mongodb.com/docs/atlas/manage-connections-aws-lambda/. So I think we even have no way to not set it |
||
string password = Environment.GetEnvironmentVariable("AWS_SECRET_ACCESS_KEY"); | ||
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. Password should be stored in a 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. Microsoft is no longer recommending the use of Let's skip the use of |
||
string awsSessionToken = Environment.GetEnvironmentVariable("AWS_SESSION_TOKEN"); | ||
|
||
var awsCredentials = | ||
new MongoCredential("MONGODB-AWS", new MongoExternalIdentity(username), new PasswordEvidence(password)) | ||
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.
|
||
.WithMechanismProperty("AWS_SESSION_TOKEN", awsSessionToken); | ||
|
||
var mongoUrl = MongoUrl.Create($"<MONGODB_URI>"); | ||
|
||
var mongoClientSettings = MongoClientSettings.FromUrl(mongoUrl); | ||
mongoClientSettings.Credential = awsCredentials; | ||
mongoClientSettings.ServerApi = new ServerApi(ServerApiVersion.V1, strict: true); | ||
|
||
return new MongoClient(mongoClientSettings); | ||
// End AWS Lambda Example 2 | ||
} | ||
|
||
static ConnectUsingAwsIamAuthenticatorLambdaHandler() | ||
{ | ||
MongoClient = CreateMongoClient(); | ||
} | ||
|
||
public string HandleRequest(ILambdaContext context) | ||
{ | ||
var database = MongoClient.GetDatabase("db"); | ||
var collection = database.GetCollection<BsonDocument>("coll"); | ||
var result = collection.Find(FilterDefinition<BsonDocument>.Empty).First(); | ||
return result.ToString(); | ||
} | ||
} | ||
} | ||
#endif |
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.
LambdaSerializer
is not supported for previous TFs