Paul
Paul

Reputation: 73

How to use AWS Elasticache from Lambda in c#

I've searched and searched and have been unable to find a tutorial / example / walkthrough with all of the above! I am trying to write a Lambda function in C# which makes use of some ElastiCache storage. I can find examples of ElastiCache access from C#, but every Library I have found referenced will not operate with .NetCore 1.0, which is what Lambda uses! Has anyone managed to do this? Many thanks.

Upvotes: 3

Views: 3348

Answers (1)

Zaxxon
Zaxxon

Reputation: 667

Yes, it is possible and you're right, the information on this subject is sparse. The key is to host your Elasticache instance and Lambda Function in the same VPC. From a high level you need to:

  1. Setup a VPC with at least two subnet groups, a route table, and a security group.
  2. Create an Elasticache Subnet Group pointing to the two subnet groups created in step 1.
  3. Create your Elasticache instance with it pointed to the Elasticache Subnet Group created in step 2.
  4. Create your C# Lambda function and use a 3rd party library to connect to Elasticache. For Redis, I used StackExchange.Redis 1.2.1 successfully with .Net Core 1.0. More recent versions will not work with .Net Core 1.0.
  5. Associate your Lambda with the same VPC, subnets, and security group.
  6. Associate your Lambda function with an IAM Role that allows you to execute the Lambda and call ec2:CreateNetworkInterface (I think it is needed for the VPC call, but not sure). Something like AWSLambdaFullAccess and AWSLambdaVPCAccessExecutionRole will work.
  7. Test your Lambda for connectivity.

This blog posting has a better walkthrough: http://fitsofury.blogspot.com/2018/02/aws-connect-to-elasticache-redis.html

Upvotes: 1

Related Questions