williamsandonz
williamsandonz

Reputation: 16420

How to identify the first execution of a Lambda version at runtime?

I want to run some code only on the first execution of a Lambda version. (NB: I'm not referring to a cold start scenario as this will occur more than once).

computeInstanceInvocationCount is unfortunately reset to 0 upon every cold start.

functionVersion is an available property but unless I store this in memory outside the lambda I cannot calculate if it is indeed the first execution.

Is it possible to deduce this based on runtime values in event or context? Or is there any other way?

Upvotes: 1

Views: 652

Answers (2)

Adiii
Adiii

Reputation: 59946

One way that you can try is to use AWS parameter store.

On every deployment update the parameter store value with

{"version":"latest","is_firsttime":true}

So run the below command after deployment

aws secretsmanager update-secret --secret-id demo --secret-string  '{"version":"latest","is_firsttime":true}'

So this is something that we need to make sure before deployment.

Now we can set logic inside the lambda, in the demo we will look into is_firsttime only.

var AWS = require('aws-sdk'),
    region = "us-west-2",
    secretName = "demo",
    secret,
    decodedBinarySecret;
var client = new AWS.SecretsManager({
    region: region
});
client.getSecretValue({SecretId: secretName}, function(err, data) {

 secret = data.SecretString;
        secret=JSON.parse(secret)
        if ( secret.is_firsttime == true)
            {
             console.log("lambda is running first time")
            //  any init operation here
            // init completed, now we are good to set back it `is_firsttime` to false
            var params = {
                Description: "Init completeed, updating value at date or anythign", 
                SecretId: "demo",
                SecretString : '[{ "version" : "latest", "is_firsttime": false}]'
               };
               client.updateSecret(params, function(err, data) {
                 if (err) console.log(err, err.stack); // an error occurred
                 else     console.log(data);           // successful response
               });
               
            }
        else{
            console.log("init already completed");
            // rest of logic incase of not first time
        }
})

This is just a demo code that will work in a non-lambda environment, adjust it accordingly.

Expected response for first time

{
  ARN: 'arn:aws:secretsmanager:us-west-2:12345:secret:demo-0Nlyli',
  Name: 'demo',
  VersionId: '3ae6623a-1111-4a41-88e5-12345'
}


Second time

init already completed

Upvotes: 1

Chris Williams
Chris Williams

Reputation: 35188

There is no way of knowing if this is the first time that a Lambda has ever run from any information passed into the Lambda.

You would have to include functionality to check elsewhere by setting a flag or parameter there, remember though that multiple copies of the Lambda could be invoked at the same time so any data store for this would presumably need to be transactional to ensure that it occurs only once.

Upvotes: 3

Related Questions