20

For example I have lambda functions that consume messages from a KinesisStream. How do stop and resume the function so that I don't incur charges and I don't loose data in the stream.

I know that if the events keep failing, Kinesis will keep retrying and the cost can be very high.

I cannot delete the function because there is lots of automation around it through CloudFormation. Is there a way to stop and restart the function?

SOLUTION: http://alestic.com/2015/11/aws-lambda-kinesis-pause-resume

NOTE: Event sources for rules, log streaming, cannot be disable using the event source. You will not event get it in the list when calling the API using the SDK. For those you have to disable the Event Rule, or the Log Subscription.

Jedi
  • 3,088
  • 2
  • 28
  • 47
victor m
  • 2,012
  • 2
  • 14
  • 23
  • 1
    Do you using aliases and versioning or your lambda functions? – arjabbar Jun 22 '16 at 15:36
  • 1
    Are you worried about events which are in flight during the deployment of new lambda version? – Shibashis Jun 22 '16 at 22:59
  • Updating a deployed lambda function should be an atomic operation. All requests should go to the code in the old version until the new one is fully deployed. There should be no intermediate period of invalidity. Are you trying to solve an actual observed problem or are you trying to avoid a perceived/potential problem? – Michael - sqlbot Jun 22 '16 at 23:58
  • I am using aliases and I am worried about events in flight but more specifically, I'm worried about an outage where I have to take the lambdas down to fix a code problem or a database problem such as increasing the provisioning on DynamoDB where the db becomes unusable for a couple of seconds if not minutes. Lets say someone checkin a code bug that miscalculates analytics and I need to take a lambda down for 3 days because it is taking a long time to fix the issue. – victor m Jun 23 '16 at 16:30

2 Answers2

12

The updated Lambda console on AWS supports this in the UI now. Click on the Kinesis stream feeding your lambda function, toggle the "Enabled/Disabled" toggle at the bottom, and Save. This will essentially pause/resume your function.Screenshot - Toggling Kinesis input into Lambda

Guy Harel
  • 121
  • 1
  • 2
7

Let's talk about Kinesis for a moment. When you pull records off the stream, Kinesis will not 'delete' those records until you 'checkpoint' the stream. You can read the same records over and over until you confirm with Kinesis that you don't need them anymore.

AWS Lambda does not checkpoint the stream until the function completes its execution without an error. (context.success())

If you deploy a Lambda function and it is broken in some way (exits with an exception/error), the Lambda function will not checkpoint the stream, and your records will stay in the stream for as long until retention period expires (24 hours, by default). The 'un-checkpointed' records can then be read in a subsequent Lambda execution.

During deployment, the same thing applies. Any currently executing Lambdas that are interrupted will not checkpoint the stream, and any currently executing Lambdas that complete successfully will checkpoint as you expect.

devonlazarus
  • 1,277
  • 10
  • 24
  • So, if the code is throwing an exception and I want to prevent charges from a large number of event failures. What do I do? I want to stop the Lambda's from running, but I want messages to stay in the Kinesis stream till I solve the problem. I know I can increase the retention period on the Kinesis stream but how do I stop my Lambda function. I can't delete it because there is large number of artifacts attached to them, which where created using CloudFormation. – victor m Jun 28 '16 at 14:58
  • @VAM, To prevent charges during outages, you should disable the Lambda. The messages will stay in the stream without problem. You don't have to delete a Lambda, but you can disable the event that triggers the Lambda. Set a CW alarm on your Lambda's Errors metric and disable it when it reaches the threshold (send notification, too). – devonlazarus Jun 29 '16 at 04:13
  • 1
    Base on your posting I search and found this: https://alestic.com/2015/11/aws-lambda-kinesis-pause-resume/. Your answer was right on. – victor m Jun 30 '16 at 15:41