|
| 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.Bson; |
| 19 | +using MongoDB.Driver; |
| 20 | +using System; |
| 21 | + |
| 22 | +// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class. |
| 23 | +[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))] |
| 24 | + |
| 25 | +namespace LambdaTest |
| 26 | +{ |
| 27 | + public class ShareMongoClientLambdaHandler |
| 28 | + { |
| 29 | + // Start AWS Lambda Example 1 |
| 30 | + private static MongoClient MongoClient { get; set; } |
| 31 | + private static MongoClient CreateMongoClient() |
| 32 | + { |
| 33 | + var mongoClientSettings = MongoClientSettings.FromConnectionString($"<MONGODB_URI>"); |
| 34 | + mongoClientSettings.ServerApi = new ServerApi(ServerApiVersion.V1, strict: true); |
| 35 | + return new MongoClient(mongoClientSettings); |
| 36 | + } |
| 37 | + |
| 38 | + static ShareMongoClientLambdaHandler() |
| 39 | + { |
| 40 | + MongoClient = CreateMongoClient(); |
| 41 | + } |
| 42 | + |
| 43 | + public string HandleRequest(ILambdaContext context) |
| 44 | + { |
| 45 | + var database = MongoClient.GetDatabase("db"); |
| 46 | + var collection = database.GetCollection<BsonDocument>("coll"); |
| 47 | + var result = collection.Find(FilterDefinition<BsonDocument>.Empty).First(); |
| 48 | + return result.ToString(); |
| 49 | + } |
| 50 | + // End AWS Lambda Example 1 |
| 51 | + } |
| 52 | + |
| 53 | + public class ConnectUsingAwsIamAuthenticatorLambdaHandler |
| 54 | + { |
| 55 | + private static MongoClient MongoClient { get; set; } |
| 56 | + private static MongoClient CreateMongoClient() |
| 57 | + { |
| 58 | + // Start AWS Lambda Example 2 |
| 59 | + string username = Environment.GetEnvironmentVariable("AWS_ACCESS_KEY_ID"); |
| 60 | + string password = Environment.GetEnvironmentVariable("AWS_SECRET_ACCESS_KEY"); |
| 61 | + string awsSessionToken = Environment.GetEnvironmentVariable("AWS_SESSION_TOKEN"); |
| 62 | + |
| 63 | + var awsCredentials = |
| 64 | + new MongoCredential("MONGODB-AWS", new MongoExternalIdentity(username), new PasswordEvidence(password)) |
| 65 | + .WithMechanismProperty("AWS_SESSION_TOKEN", awsSessionToken); |
| 66 | + |
| 67 | + var mongoUrl = MongoUrl.Create($"<MONGODB_URI>"); |
| 68 | + |
| 69 | + var mongoClientSettings = MongoClientSettings.FromUrl(mongoUrl); |
| 70 | + mongoClientSettings.Credential = awsCredentials; |
| 71 | + mongoClientSettings.ServerApi = new ServerApi(ServerApiVersion.V1, strict: true); |
| 72 | + |
| 73 | + return new MongoClient(mongoClientSettings); |
| 74 | + // End AWS Lambda Example 2 |
| 75 | + } |
| 76 | + |
| 77 | + static ConnectUsingAwsIamAuthenticatorLambdaHandler() |
| 78 | + { |
| 79 | + MongoClient = CreateMongoClient(); |
| 80 | + } |
| 81 | + |
| 82 | + public string HandleRequest(ILambdaContext context) |
| 83 | + { |
| 84 | + var database = MongoClient.GetDatabase("db"); |
| 85 | + var collection = database.GetCollection<BsonDocument>("coll"); |
| 86 | + var result = collection.Find(FilterDefinition<BsonDocument>.Empty).First(); |
| 87 | + return result.ToString(); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | +#endif |
0 commit comments