Skip to content

Commit 433a512

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

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
mongoClientSettings.ServerApi = new ServerApi(ServerApiVersion.V1, strict: true);
34+
return new MongoClient(mongoClientSettings);
35+
}
36+
37+
public ShareMongoClientLambdaHandler()
38+
{
39+
MongoClient = CreateMongoClient();
40+
}
41+
42+
public string HandleRequest(ILambdaContext context)
43+
{
44+
var databases = MongoClient.ListDatabaseNames();
45+
return string.Join(",", databases);
46+
}
47+
// End AWS Lambda Example 1
48+
}
49+
50+
public class ConnectUsingAwsIamAuthenticatorLambdaHandler
51+
{
52+
private static MongoClient MongoClient { get; set; }
53+
private static MongoClient CreateMongoClient()
54+
{
55+
// Start AWS Lambda Example 2
56+
string username = Environment.GetEnvironmentVariable("AWS_ACCESS_KEY_ID");
57+
string password = Environment.GetEnvironmentVariable("AWS_SECRET_ACCESS_KEY");
58+
string awsSessionToken = Environment.GetEnvironmentVariable("AWS_SESSION_TOKEN");
59+
60+
var awsCredentials =
61+
new MongoCredential("MONGODB-AWS", new MongoExternalIdentity(username), new PasswordEvidence(password))
62+
.WithMechanismProperty("AWS_SESSION_TOKEN", awsSessionToken);
63+
64+
var mongoUrl = MongoUrl.Create($"<MONGODB_URI>");
65+
66+
var mongoClientSettings = MongoClientSettings.FromUrl(mongoUrl);
67+
mongoClientSettings.Credential = awsCredentials;
68+
mongoClientSettings.ServerApi = new ServerApi(ServerApiVersion.V1, strict: true);
69+
70+
return new MongoClient(mongoClientSettings);
71+
// End AWS Lambda Example 2
72+
}
73+
74+
public ConnectUsingAwsIamAuthenticatorLambdaHandler()
75+
{
76+
MongoClient = CreateMongoClient();
77+
}
78+
79+
public string HandleRequest(ILambdaContext context)
80+
{
81+
var databases = MongoClient.ListDatabaseNames();
82+
return string.Join(",", databases);
83+
}
84+
}
85+
}
86+
#endif

Diff for: tests/MongoDB.Driver.Examples/MongoDB.Driver.Examples.csproj

+5
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@
4242
<PackageReference Include="JunitXml.TestLogger" Version="2.1.81" />
4343
</ItemGroup>
4444

45+
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
46+
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
47+
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.2.0" />
48+
</ItemGroup>
49+
4550
<ItemGroup>
4651
<ProjectReference Include="..\..\src\MongoDB.Bson\MongoDB.Bson.csproj" />
4752
<ProjectReference Include="..\..\src\MongoDB.Driver\MongoDB.Driver.csproj" />

0 commit comments

Comments
 (0)