How to wait for a lambda to be deployed?

0

I have some code that updates a lambda (using lambda_client.send(UpdateFunctionCommand)) and that then needs to wait until this completes before calling lambdaClient.send(UpdateFunctionConfigurationCommand) to update parameters such as timeout and memory size.

I'm using this code to wait until the update completes before doing the second update:

async #waitForFunction(lambda_client, functionName)
    {
        let maxAttempts = 100;
        let attempts = 0;
        let state;

        while (attempts < maxAttempts){
            state = await lambda_client.send(new GetFunctionConfigurationCommand( { FunctionName: functionName }));
            if (state.LastUpdateStatus != 'Successful'){
                if (attempts > 0 && attempts % 5 == 0)
                    console.log(`Waiting ...`);
                attempts++;
            }
            else
                break;
        }

        if (attempts >= maxAttempts)
            throw new Error('updateFunction: timed out');
    }

I'm coming across occasional rate errors, and presumably need some form of sleep in the loop but this all feels very hacky. Is there a "proper" way to do this? Are there waiters available in v3.0 to handle this?

Thanks,

David

dmb0058
posta 10 mesi fa255 visualizzazioni
2 Risposte
0

Hmm, very interesting. I hadn't considered using a separate state machine like this. Looks nice, I'll definitely investigate!

I can't see why they didn't carry the waiter architecture forward more quickly but in the interim I stuck a blocking poll every 0.2s ... feels hacky but it works until I find a better way :)

Thanks,

David

dmb0058
con risposta 10 mesi fa
0

The best way to handle this would be to move the workflow logic into AWS Step Functions. Here is an example state machine which submits a job, waits, polls for job status, then waits again if needed. Step Functions will give you built-in error handling and retry capabilities.

See Serverless Workflows Collection for more examples and templates

AWS
con risposta 10 mesi fa

Accesso non effettuato. Accedi per postare una risposta.

Una buona risposta soddisfa chiaramente la domanda, fornisce un feedback costruttivo e incoraggia la crescita professionale del richiedente.

Linee guida per rispondere alle domande