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
已提问 9 个月前242 查看次数
2 回答
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
已回答 9 个月前
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
已回答 9 个月前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则