Skip to content

Update deployment scripts where necessary for AWS Lambda function states

The "state" of AWS Lambda functions is being reworked: https://aws.amazon.com/blogs/compute/coming-soon-expansion-of-aws-lambda-states-to-all-functions/

Previously the "state" after creation was directly "Active", but now it seems that creating and updating Lambda functions could first result in a "Pending" state during which any updates via CLI/SDK would fail. According to the article the HOW to create/update/invoke Lambda Functions doesn't change.

A solution to this problem I can think of could look something like this:

  • Create a new function (consider PowerShell naming conventions) in deployment\psFunctions.ps1 which:
    1. Waits while the status of the AWS Lambda function is Pending. It should have some kind of configurable parameters for a timeout, i.e. when to stop waiting even if it still is pending.
    2. Prints out some debug information (Write-Verbose) about the status after it has stopped waiting.
    3. When the status is Active or Inactive returns true (i.e. we can update the function) or otherwise false (still pending or failed).
  • Use this function in all other functions of deployment\psFunctions.ps1 before invoking / updating anything on an AWS Lambda Function and react accordingly to its result. Note that this is only necessary for SOME uses of CLI aws lambda. For example neither aws lambda create-function nor aws lambda get-function would have to consider the state.

The following CLI commands can be used to get some of the AWS Lambda status information (replace [FUNCTION_NAME]):

# Gets the State and the LastUpdateStatus
aws lambda get-function --function-name '[FUNCTION_NAME]' --query 'Configuration.[State, LastUpdateStatus]'

# Gets more detailed information and in addition contain a StateReason and StateReasonCode
aws lambda get-function-configuration --function-name '[FUNCTION_NAME]'

It would probably be best to place the "new wait for AWS Lambda State" function before calling the relevant aws lambda commands in these functions in deployment\psFunctions.ps1 (which should cover all relevant cases):

  • Update-AWSLambdaFunctionCode - before aws lambda update-function-code
  • Update-AWSLambdaFunction - note that here the command is executed not directly but from a string with the line & "aws" $UpdateFunctionParams >$null
  • Set-AWSLambdaFunctionLayers - before aws lambda update-function-configuration
  • Add-AWSLambdaApiPermission - probably enough to do it before aws lambda add-permission
  • New-AWSApiLambdaIntegration - before aws lambda add-permission

More details on AWS Lambda States: