Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 482
covfefe Avatar asked Jun 10 '19 19:06

covfefe


People also ask

Is AWS CLI available in Lambda?

You can use the AWS Command Line Interface to manage functions and other AWS Lambda resources. The AWS CLI uses the AWS SDK for Python (Boto) to interact with the Lambda API. You can use it to learn about the API, and apply that knowledge in building applications that use Lambda with the AWS SDK.

Are lambda functions public by default?

By default, Lambda functions have access to the public internet. This is not the case after they have been configured with access to one of your VPCs. If you continue to need access to resources on the internet, set up a NAT instance or Amazon NAT Gateway.

What happens when lambda is invoked?

When you invoke a function, you can choose to invoke it synchronously or asynchronously. With synchronous invocation, you wait for the function to process the event and return a response. With asynchronous invocation, Lambda queues the event for processing and returns a response immediately.


1 Answers

You can check the exit code of get-function in bash. If the function does not exists 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
like image 136
Nirdosh Gautam Avatar answered Oct 20 '22 14:10

Nirdosh Gautam