9

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?

2
  • 4
    Why not try aws lambda get-function --function-name abc. This will return ResourceNotFoundException if the function does not exist. Commented Jun 10, 2019 at 19:48
  • Either what @krishna_mee2004 suggested or you can list your functions to loop through and find a function if it exists, etc. aws lambda list-functions Commented Jun 10, 2019 at 19:52

1 Answer 1

17

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
Sign up to request clarification or add additional context in comments.

3 Comments

my exit code showed up as 254 now on failure lols.
another way is to use function-exists CLI command as described here: awscli.amazonaws.com/v2/documentation/api/latest/reference/…
In my case its not responding anything, I wanted to return the value instead of echo so I have returned $? but it doesn't work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.