问题
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.
回答1:
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:
- Setup a VPC with at least two subnet groups, a route table, and a security group.
- Create an Elasticache Subnet Group pointing to the two subnet groups created in step 1.
- Create your Elasticache instance with it pointed to the Elasticache Subnet Group created in step 2.
- 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.
- Associate your Lambda with the same VPC, subnets, and security group.
- 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.
- Test your Lambda for connectivity.
This blog posting has a better walkthrough: http://fitsofury.blogspot.com/2018/02/aws-connect-to-elasticache-redis.html
来源:https://stackoverflow.com/questions/44741141/how-to-use-aws-elasticache-from-lambda-in-c-sharp