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
gefragt vor 9 Monaten240 Aufrufe
2 Antworten
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
beantwortet vor 9 Monaten
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
beantwortet vor 9 Monaten

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen