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

CSHARP-4127: Language specific examples for AWS Lambda. #779

Merged
merged 4 commits into from
Jun 16, 2022

Conversation

DmitryLukyanov
Copy link
Contributor

@DmitryLukyanov DmitryLukyanov commented Apr 25, 2022

How to configure AWS auth for atlas cluster:

  1. Get your AWS_* (aws_access_key_id, aws_secret_access_key, aws_session_token) credentials in Single Sign-on page:
    image

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:

   $ ./aws sts get-caller-identity
    {
        "UserId": "blablabla:[dmitry.lukyanov@mongodb.com](mailto:dmitry.lukyanov@mongodb.com)",
        "Account": "%ID_VALUE%",
        "Arn": "arn:aws:sts::%ID_VALUE%:assumed-role/%ROLE_NAME%/[dmitry.lukyanov@mongodb.com](mailto:dmitry.lukyanov@mongodb.com)"
     }

pay attention on %ROLE_NAME%
4. list all roles via:

     $ ./aws iam list-roles
     {
       "Roles": [
       {
             "Path": "..",
             "RoleName": "%ROLE_NAME%",
             "Arn": "arn:aws...:
        ...

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

* limitations under the License.
*/

#if NETCOREAPP3_1_OR_GREATER
Copy link
Contributor Author

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
Copy link
Contributor Author

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)

Copy link
Contributor

@JamesKovacs JamesKovacs left a 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.

https://jira.mongodb.org/browse/DRIVERS-2018?focusedCommentId=4504303&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-4504303

@DmitryLukyanov
Copy link
Contributor Author

it was decided to use find instead listdatabases.

@JamesKovacs JamesKovacs requested review from BorisDog and JamesKovacs and removed request for JamesKovacs April 26, 2022 15:55
Copy link
Contributor

@JamesKovacs JamesKovacs left a 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");
Copy link
Contributor

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.

Copy link
Contributor

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))
Copy link
Contributor

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.

Copy link
Contributor

@JamesKovacs JamesKovacs left a 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();
Copy link
Contributor

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 ?

Copy link
Contributor Author

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

Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As far as I understand, both ways can be used. This is what created by VS "Simple S3 function" project:
image

Copy link
Contributor

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.

Copy link
Contributor Author

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 :(

Copy link
Contributor Author

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

Copy link
Contributor Author

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");
Copy link
Contributor

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>"

Copy link
Contributor Author

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

Copy link
Contributor

@BorisDog BorisDog Apr 28, 2022

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...

Copy link
Contributor Author

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

Copy link
Contributor

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.

Copy link
Contributor Author

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();
Copy link
Contributor

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");
Copy link
Contributor

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)
Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it can be omitted

Copy link
Contributor Author

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

Copy link
Contributor

@BorisDog BorisDog left a comment

Choose a reason for hiding this comment

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

LGTM

@DmitryLukyanov DmitryLukyanov merged commit 1674b07 into mongodb:master Jun 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants