4

I have a lambda function my-func which should only be run once at a time. Because of that, I set the Reserved Concurrency to 1. I am trying to invoke it with the command:

aws lambda invoke --function-name my-func --invocation-type RequestResponse --cli-binary-format raw-in-base64-out --payload '{\"recreate\":true}' response.json

However, it results in this error after a few seconds:

An error occurred (TooManyRequestsException) when calling the Invoke operation (reached max retries: 2): Rate Exceeded.

It appears that it tries to invoke the function multiple times even though the original invocation never ran into an error. If I increase the Reserved Concurrency to a value like 5, then the single lambda invoke command results in multiple invocations even though the first invocation continues to execute without any problem.

Another thing that is throwing me off is that it works correctly from the AWS console GUI. I created a test event in the AWS lambda function on the console interface. It invokes my-func with the same payload I am using in the aws-cli command:

{
  "recreate": true
}

Invoking the function using this test event works flawlessly. It seems to just run the function once and doesn't cause a TooManyRequestsException. Does this mean something is wrong with my aws-cli command?

8
  • What is your function code? Maybe you try to run it recursively? Commented Feb 1, 2022 at 22:46
  • @Marcin I thought about that at first, but the function doesn't call itself anywhere. In fact it doesn't perform any lambda operations. It only configures some NAT gateway stuff using ec2 client in boto3 Commented Feb 1, 2022 at 22:51
  • It does not have to be explicit invocation. Maybe the function triggers something non-directly (e.g. store object in S3 and S3 event triggers the same function). Commented Feb 1, 2022 at 22:57
  • @Marcin I just did a test: I removed all code from the function and replaced it with time.sleep(60). Again, the function works correctly when I invoke it from the AWS console but gets TooManyRequestsException when I invoke from the aws-cli. Could it be something with the way I'm invoking it? Commented Feb 1, 2022 at 23:31
  • Can you post --debug logs? Commented Feb 1, 2022 at 23:51

1 Answer 1

6

I've figured out the problem. Even though my function's timeout was set to 600 (5 minutes), the aws cli has it's own socket timeout which defaults to 60 seconds. Each time it reached this timeout, it must have triggered a retry. I fixed it by adding --cli-read-timeout 600 to my command like so:

aws --cli-read-timeout 600 lambda invoke --function-name my-func --invocation-type RequestResponse --cli-binary-format raw-in-base64-out --payload '{\"recreate\":true}' response.json
Sign up to request clarification or add additional context in comments.

4 Comments

Why would read take more than 60 seconds? Does the function have a large output and you have a slow connection?
@jellycsc He mentioned that the lambda modifies NAT gateway resources. Creating NAT gateways is a slow operation that can easily take 60+ seconds. It is simply a very slow operation ion the AWS side. So this has nothing to do with his connection, it is working as expected.
Thank You for that! when doing and invoke through e.g. python boto library the same aplies, the timeout can be changed in the config: botocore.amazonaws.com/v1/documentation/api/latest/reference/…
Good find. I am seeing the same thing with lambda execution > 60 seconds.

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.