Search code examples
amazon-web-servicesaws-lambdaaws-cli

AWS CLI check if a lambda function exists


How do I do a one-time check if a lambda function exists via the CLI? I saw this function-exists option - https://docs.aws.amazon.com/cli/latest/reference/lambda/wait/function-exists.html

But it polls every second and returns a failure after 20 failed checks. I only want to check once and fail if it isn't found. Is there a way to do that?


Solution

  • You can check the exit code of get-function in bash. If the function does not exist, it returns exit code 255 else it returns 0 on success. e.g.

    aws lambda get-function --function-name my_lambda
    echo $?
    

    And you can use it like below: (paste this in your terminal)

    function does_lambda_exist() {
      aws lambda get-function --function-name $1 > /dev/null 2>&1
      if [ 0 -eq $? ]; then
        echo "Lambda '$1' exists"
      else
        echo "Lambda '$1' does not exist"
      fi
    }
    
    does_lambda_exist my_lambda_fn_name