Marc
Marc

Reputation: 553

Restarting AWS lambda function to clear cache

I have a AWS Lambda function that creates an object from a s3 call in cold start. I then hold the object in the cache while the function is warm to keep load times down. When files are changed in s3, I have a trigger to run the lambda, but not all the running instances of lambda restart and pull from s3.

Is there a way to bring down all instances of lambda forcing a full cold start?

Also, I don't want to use python.

Upvotes: 55

Views: 69254

Answers (11)

Sajjad Ali Vayani
Sajjad Ali Vayani

Reputation: 1

Updating description from AWS console will do the job. My use case was, I was storing SSM parameter in static variable in lambda function as a cache. I updated the value of ssm parameter and updated the description of lambda from AWS console, the updated value reflected in lambda.

Upvotes: 0

Paul Şular
Paul Şular

Reputation: 155

Following Renato Byrro's answer I made a lambda function using JavaScript AWS SDK to restart another lambda function by updating the description.

import { LambdaClient, UpdateFunctionConfigurationCommand } from '@aws-sdk/client-lambda';

const forceLambdaRestart = async event => {
    try {
        const client = new LambdaClient({
            region: 'your region here',
            credentials: {
                accessKeyId: 'your access key id',
                secretAccessKey: 'your secret access key',
            },
        });

        const command = new UpdateFunctionConfigurationCommand({
            FunctionName: event.functionName,
            Description: `forced update ${Date.now()}`,
        });

        const data = await client.send(command);

        console.log(data);
        return data;
    } catch (error) {
        console.error(error);
        return error;
    }
};

forceLambdaRestart();

It seems like that is enough to restart the lambda and clear in-memory cache.

Upvotes: 1

Nilesh
Nilesh

Reputation: 127

The simplest answer for this question I found is. Make some changes in function like adding a simple comment line or removing any white space and then redeploy the function.

It will clear the cache while deploying.

Upvotes: 2

johncurrier
johncurrier

Reputation: 493

Simply add a new environment variable and / or change an existing one. I created one named BOGUS and gave it a number that I increment whenever I want to force a cold start.

Upvotes: 11

Jochem Schulenklopper
Jochem Schulenklopper

Reputation: 6914

In addition to some of the valid answers above: I happened to run an experiment on the (average) AWS Lambda instance lifetime. I could not find instances that ran for much longer than (on average) two hours: https://xebia.com/blog/til-that-aws-lambda-terminates-instances-preemptively/.

TL;DR: AWS Lambda is preemptively terminating instances (even those handling traffic) after two hours, with a standard deviation of 30 minutes.

Upvotes: 4

user3041539
user3041539

Reputation: 628

Easiest way I found was changing something in Basic Settings like timeout: Basic Settings

I've upped+1 by a second, saved, and the function got refreshed Memory and timeout settings

Upvotes: 13

Baked Inhalf
Baked Inhalf

Reputation: 3735

I made an Answer based on my comment and verification from @DejanVasic

aws lambda update-function-configuration --function-name "myLambda" --description "foo"

This will force the next invokation of the lambda to "cold start".

To verify:

@timestamp, @message | sort @timestamp desc | limit 1000 | filter @message like "cold_start:true"

Upvotes: 45

Ludicrous
Ludicrous

Reputation: 61

If you are using the Lambda versioning system, another way to do this is by publishing a new version and using an alias to direct all traffic to it.

Here's an example:

Publish version: aws lambda publish-version --function-name your-function-name-here

Update the alias pointing to the new version: aws lambda update-alias --function-name your-function-name-here --name alias-name-here --function-version 123 (use the function version in the output message from the first command above)

Upvotes: 4

Renato Byrro
Renato Byrro

Reputation: 3784

Use the UpdateFunctionCode API endpoint to force a refresh of all containers. AWS SDKs wrap this up to make it easier for you to call the API using your preferred language.

Upvotes: 15

Moe
Moe

Reputation: 2842

The only way force lambda to discard existing containers is to redeploy the function with something different.

Check out my answer here: Force Discard AWS Lambda Container

Good luck, Moe

Upvotes: 3

Noel Llevares
Noel Llevares

Reputation: 16037

Currently, there is no way to force restarts on running Lambda containers.

You can, however, redeploy the function so that it will start using new containers from that point onwards.

Upvotes: 5

Related Questions