Skip to content

Commit d793050

Browse files
CSHARP-4127: Language specific examples for AWS Lambda.
1 parent dbea92c commit d793050

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/* Copyright 2010-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
#if NETCOREAPP3_1_OR_GREATER
17+
using Amazon.Lambda.Core;
18+
using MongoDB.Driver;
19+
using System;
20+
21+
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
22+
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
23+
24+
namespace LambdaTest
25+
{
26+
public class ShareMongoClientLambdaHandler
27+
{
28+
// Start AWS Lambda Example 1
29+
private static MongoClient MongoClient { get; set; }
30+
private static MongoClient CreateMongoClient()
31+
{
32+
var mongoClientSettings = MongoClientSettings.FromConnectionString($"<MONGODB_URI>");
33+
return new MongoClient(mongoClientSettings);
34+
}
35+
36+
public ShareMongoClientLambdaHandler()
37+
{
38+
MongoClient = CreateMongoClient();
39+
}
40+
41+
public string HandleRequest(ILambdaContext context)
42+
{
43+
var databases = MongoClient.ListDatabaseNames();
44+
return string.Join(",", databases);
45+
}
46+
// End AWS Lambda Example 1
47+
}
48+
49+
public class ConnectUsingAwsIamAuthenticatorLambdaHandler
50+
{
51+
private static MongoClient MongoClient { get; set; }
52+
private static MongoClient CreateMongoClient()
53+
{
54+
// Start AWS Lambda Example 2
55+
string username = Environment.GetEnvironmentVariable("AWS_ACCESS_KEY_ID");
56+
string password = Environment.GetEnvironmentVariable("AWS_SECRET_ACCESS_KEY");
57+
string awsSessionToken = Environment.GetEnvironmentVariable("AWS_SESSION_TOKEN");
58+
59+
var awsCredentials =
60+
new MongoCredential("MONGODB-AWS", new MongoExternalIdentity(username), new PasswordEvidence(password))
61+
.WithMechanismProperty("AWS_SESSION_TOKEN", awsSessionToken);
62+
63+
var mongoUrl = MongoUrl.Create($"<MONGODB_URI>");
64+
65+
var mongoClientSettings = MongoClientSettings.FromUrl(mongoUrl);
66+
mongoClientSettings.Credential = awsCredentials;
67+
mongoClientSettings.ServerApi = new ServerApi(ServerApiVersion.V1, strict: true);
68+
69+
return new MongoClient(mongoClientSettings);
70+
// End AWS Lambda Example 2
71+
}
72+
73+
public ConnectUsingAwsIamAuthenticatorLambdaHandler()
74+
{
75+
MongoClient = CreateMongoClient();
76+
}
77+
78+
public string HandleRequest(ILambdaContext context)
79+
{
80+
var databases = MongoClient.ListDatabaseNames();
81+
return string.Join(",", databases);
82+
}
83+
}
84+
}
85+
#endif

tests/MongoDB.Driver.Examples/MongoDB.Driver.Examples.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
<PackageReference Include="xunit" Version="2.4.0" />
4141
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
4242
<PackageReference Include="JunitXml.TestLogger" Version="2.1.81" />
43+
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
44+
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.2.0" />
4345
</ItemGroup>
4446

4547
<ItemGroup>

0 commit comments

Comments
 (0)