Search code examples
amazon-web-servicesservicestackaws-lambdaservicestack.redisservicestack-text

ServiceStack Redis does not run on AWS Lambda


I am trying to leverage AWS Elasticache (Redis) from my Lambda function using the ServiceStack .Redis.Core library (version 1.0.44). When running the lambda from my local machine (mac osx) everything works fine; I can interact with the AWS Redis cache with no errors.

However, when I deploy my function to AWS to execute on their lambda servers the code no longer works and the ServiceStack.Text.Env library throws a PlatformNotSupportedException exception:

{
  "errorType": "RedisException",
  "errorMessage": "[13:50:14.793] Unable to Connect: sPort: 55382, Error: The type initializer for 'ServiceStack.Text.Env' threw an exception.
   at ServiceStack.Redis.RedisNativeClient.FlushSendBuffer()
   at ServiceStack.Redis.RedisNativeClient.SendReceive[T](Byte[][] cmdWithBinaryArgs, Func`1 fn, Action`1 completePipelineFn, Boolean sendWithoutRead)",
  "stackTrace": [
    "at ServiceStack.Redis.RedisNativeClient.CreateConnectionError(Exception originalEx)",
    "at ServiceStack.Redis.RedisNativeClient.SendReceive[T](Byte[][] cmdWithBinaryArgs, Func`1 fn, Action`1 completePipelineFn, Boolean sendWithoutRead)",
    "at ServiceStack.Redis.RedisNativeClient.get_Info()",
    "at ServiceStack.Redis.RedisClient.GetServerRole()",
    "at ServiceStack.Redis.RedisResolver.CreateRedisClient(RedisEndpoint config, Boolean master)",
    "at ServiceStack.Redis.RedisManagerPool.GetClient()",
    **snip**
    "at lambda_method(Closure , Stream , Stream , ContextInfo )"
  ],
  "cause":   {
    "errorType": "TypeInitializationException",
    "errorMessage": "The type initializer for 'ServiceStack.Text.Env' threw an exception.",
    "stackTrace": [
      "at ServiceStack.Redis.RedisNativeClient.FlushSendBuffer()",
      "at ServiceStack.Redis.RedisNativeClient.SendReceive[T](Byte[][] cmdWithBinaryArgs, Func`1 fn, Action`1 completePipelineFn, Boolean sendWithoutRead)"
    ],
    "cause":     {
      "errorType": "PlatformNotSupportedException",
      "errorMessage": "Operation is not supported on this platform.",
      "stackTrace": [
        "at System.Runtime.InteropServices.OSPlatform.get_Linux()",
        "at ServiceStack.Text.Env..cctor()"
      ]
    }
  }
}  

So is it possible to use the ServiceStack.Redis.Core package when running in AWS Lambda?


Solution

  • This Exception is due to AWS Lambda not implementing .NET Core's RuntimeInformation.IsOSPlatform(OSPlatform.Linux) API for detecting which OS the App is being run on.

    I've just added a fix to catch this non implemented API which is available from v4.5.15 that's now available on MyGet.