-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
CSHARP-4127: Language specific examples for AWS Lambda. #779
Conversation
* limitations under the License. | ||
*/ | ||
|
||
#if NETCOREAPP3_1_OR_GREATER |
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
// 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 comment
The reason will be displayed to describe this comment to others. Learn more.
The scope of these code examples is limited to the instructions on:
How to share the client (“Example 1”. See first snippet in this section for structure)
How to connect to the deployment using AWS IAM authentication (“Example 2”. Same structure as “Example 1”, but see the blue note box in the docs for which lines need to be replaced to work with AWS IAM auth)
d793050
to
433a512
Compare
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.
See comment on DRIVERS-2018. We need to expand our example slightly to include some minimal CRUD operations otherwise we aren't guaranteed to be successfully talking to a server. We might have just established connection pool, but not successfully connected to a cluster from AWS Lambda.
it was decided to use |
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.
Minor change requested.
{ | ||
// Start AWS Lambda Example 2 | ||
string username = Environment.GetEnvironmentVariable("AWS_ACCESS_KEY_ID"); | ||
string password = Environment.GetEnvironmentVariable("AWS_SECRET_ACCESS_KEY"); |
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.
Password should be stored in a SecureString
.
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.
Microsoft is no longer recommending the use of SecureString
.
https://docs.microsoft.com/en-us/dotnet/api/system.security.securestring?view=net-6.0
And this rule:
https://github.com/dotnet/platform-compat/blob/master/docs/DE0001.md
Let's skip the use of SecureString
that I initially suggested and keep the example easier to follow as you have it.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
PasswordEvidence
has a ctor that accepts a SecureString
.
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.
LGTM
|
||
public ShareMongoClientLambdaHandler() | ||
{ | ||
MongoClient = CreateMongoClient(); |
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.
Is it guaranteed that the ctor will be called once/not concurrently?
Or should it be static ShareMongoClientLambdaHandler
?
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.
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 warm
start. Each Cold
start will create new instance of lambda handler
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.
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?
In AWS .Net samples static ctors are used.
Probably we'd need to look deeper into this.
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.
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.
Yes, note that S3Client
in this case is not static. The initialization of a static field in non-static constructor is what puzzles me, but this does not mean it's not working though. I think we have to be able to explain such unusual combination, or follow the official samples.
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.
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 comment
The 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 comment
The 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
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Are environment variables part of the Aws Lambda auth process?
If not, maybe for simplicity just use dummy values: var username = "<username>"
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
In this case I wonder if Environment.GetEnvironmentVariable
adds any additional value for the sample. Maybe we'd better keep it as simple as possible...
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.
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 comment
The 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 :)
Also I don't think we should prioritize simplifying the usage of this code in our driver.
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 comment
The 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
|
||
public ShareMongoClientLambdaHandler() | ||
{ | ||
MongoClient = CreateMongoClient(); |
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.
Yes, note that S3Client
in this case is not static. The initialization of a static field in non-static constructor is what puzzles me, but this does not mean it's not working though. I think we have to be able to explain such unusual combination, or follow the official samples.
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 comment
The 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 :)
Also I don't think we should prioritize simplifying the usage of this code in our driver.
Not a big issue, I just think it introduces some "noise" to the sample, and not sure we got any use for it.
MongoClient = CreateMongoClient(); | ||
} | ||
|
||
public string HandleRequest(ILambdaContext context) |
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.
In all samples a came across, I see additional inputType
parameter in Handler function:
https://docs.aws.amazon.com/lambda/latest/dg/csharp-handler.html
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
to have some official sample without it?
I just saw that it works without this argument
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.
LGTM
How to configure AWS auth for atlas cluster:
Pay attention that values should be regenerated from time to time.
2. Configure credentials folder here:
c:\Users\{user_name}\.aws\
3. Get your arn via
get-caller-identity
:pay attention on %ROLE_NAME%
4. list all roles via:
in the provided roles, search for a record with a RoleName equal to %ROLE_NAME% and record his
arn
.5. In your atlas cluster, create a new user with AWS authentication and set AWS IAM Role ARN from #4.
6. Then configure a mongoClient in the same way as it's done in this PR with
MONGODB-AWS
auth credentials