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
asked 9 months ago229 views
2 Answers
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
answered 9 months ago
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
answered 9 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions