meisterlobi
meisterlobi

Reputation: 43

How to trigger AWS Lambda function error to test Cloudwatch Alarm

Have a lambda function in production, however I need to verify that upon an execution error, the configured cloudwatch alarm will successfully go to Alarm state. One option is to update the code and simply break it temporarily, but this isn't feasible.

Looking for an API call that can force an AWS Lambda to error without having to modify the actual code.

Upvotes: 1

Views: 1645

Answers (2)

Jens
Jens

Reputation: 21510

As mentioned in other answers, there is no such API. There are however two ways I could imagine doing this:

  1. Invoking your Lambda with faulty input data using either the AWS Console or AWS CLI (see example below).
  2. Manipulating the configuration of the Lambda in such a way, that it causes an error. For example setting the memory too low, using a timeout that too short or removing required permissions.

Depending on your code, there might be a way by invoking your Lambda with faulty input data (see 1. above). Imagine a Go Lambda for a party planning service like the following:

func handleRequest(event MyEvent) error {
    if event.NumberOfGuests < 2 {
        return errors.New("there should be at least 2 guests")
    }
    return nil
}

func main() {
    lambda.Start(handleRequest)
}

You could now invoke your Lambda with a payload like this:

{
  "NumberOfGuests": 1
}

This would force the Lambda above to return an error.

Obviously, this highly depends on the code of your Lambda, but if you have code paths like this, all you need to do is to invoke the Lambda with input data that would run this code paths.

Upvotes: 0

Marcin
Marcin

Reputation: 238199

Looking for an API call that can force an AWS Lambda to error without having to modify the actual code.

This is no such call. But some lambda errors (specially runtime errors) can be "forced" without changing a source code. For example, but removing lambda permissions or changing function execution timeout.

You can also manually set your CW alarm into alarm state using set-alarm-state API call.

Upvotes: 1

Related Questions