Are there any best practices or documentation available for using Dependency Injection or mocking Environment Variables when using AWS Lambda with .NET Core v1.0 ?
As an example, below is an example Lambda function ProcessKinesisMessageById
that accepts a KinesisEvent and does some sort of processing. Part of this processing involves accessing some sort of External Service (like AWS S3 or a database) that need access to Environment Variables for setup.
public class AWSLambdaFileProcessingService
{
private IFileUploadService _fileUploadService;
// No constructor in the Lambda Function
[LambdaSerializer(typeof(JsonSerializer))]
public void ProcessKinesisMessageById(KinesisEvent kinesisEvent, ILambdaContext context)
{
Console.WriteLine("Processing Kinesis Request");
_fileUploadService = new AWSFileUploadService(); // Can this be injected? (Constructor shown below)
// some sort of processing
_fileUploadService.DoSomethingWithKinesisEvent(kinesisEvent);
}
}
// Example of of a class that needs access to environment variables
// Can this class be injected into the AWS Lambda function?
// Or the Environment Variables mocked?
public class AWSFileUploadService : IFileUploadService
{
private readonly IAmazonS3 _amazonS3Client;
private readonly TransferUtility _fileTransferUtility;
public AWSFileUploadService()
{
_amazonS3Client = new AmazonS3Client(
System.Environment.GetEnvironmentVariable("AWS_S3_KEY"),
System.Environment.GetEnvironmentVariable("AWS_S3_SECRET_KEY")
);
_fileTransferUtility = new TransferUtility(_amazonS3Client);
}
public bool DoSomethingWithKinesisEvent(KinesisEvent kinesisEvent)
{
// ....
}
```
The function works okay after publishing it with Environment variables, and it can be tested using the Lambda Function View test console (in Visual Studio 2017) after publishing it to AWS. However, I am having trouble creating unit or integration tests without being able to mock or set the environment variables for use in local testing.
Does anyone have any suggestions or practices for testing the Lambda function locally?
IFileUploadService
which in turn should be injected intoAWSLambdaFileProcessingService
– Nkosi