Recursive Lambda function Invocation
This is an example of a function that will recursively call itself.
Warning It's possible to run into infinite loops with recursive calls.
Test your functions locally before deploying to production.
Use cases
- Functions that run longer that 5 minutes
- Use recursive function to process SQS messages
- Lamdba chaining
Running a function recursively will allow you to pass state information to the next function call.
Setup
1. Deploy the function with sls deploy
The sls deploy
command will give you back the function ARN (Amazon Resource Name) needed for the function to recursively call itself.
The message should look something like:
Service Informationservice: recursive-invocation-examplestage: devregion: us-east-1api keys: Noneendpoints: Nonefunctions: recursive-invocation-example-dev-recursiveExample: arn:aws:lambda:us-east-1:488110005556:function:recursive-invocation-example-dev-recursiveExample
The ARN in this example is arn:aws:lambda:us-east-1:488110005556:function:recursive-invocation-example-dev-recursiveExample
. If you need to retrieve this data again run the serverless info
command.
2. Take your newly created function's ARN and replace the custom: functionARN value yourFunctionARN
value in serverless.yml
with your ARN.
Before:
# in serverless.ymlcustom: functionARN: yourFunctionARN
After:
# in serverless.ymlcustom: functionARN: arn:aws:lambda:us-east-1:488110005556:function:recursive-invocation-example-dev-recursiveExample
3. Uncomment the IAM statement in serverless.yml
# in serverless.ymlprovider: name: aws runtime: nodejs12.x iamRoleStatements: - Effect: "Allow" Action: - "lambda:InvokeFunction" Resource: ${self:custom.functionARN}
The custom: functionARN
value is referenced as a serverless variable in the IAM statement the variable syntax ${self:custom.functionARN}
For more information on serverless variables. Read the variable docs.
4. Redeploy the function to enable the new IAM role.
Run sls deploy
again to redeploy the service and apply the new IAM role needed for the function to call itself.
Invoking
Important Make sure to set a limit on the number of invocations and test locally first to avoid infinite recursive loops in AWS.
Invoke the function:
sls invoke -f recursiveExample -p event.json
See the logs of the recursive calls:
sls logs -f recursiveExample
The logs output should look something like:
START RequestId: 43a9d5b46c-11e6-b6bc-718f7ec807df Version: $LATEST2026 22:39:37.769 (-08:00) 43a9d5b46c-11e6-b6bc-718f7ec807df received { numberOfCalls: 5 }2026 22:39:37.792 (-08:00) 43a9d5b46c-11e6-b6bc-718f7ec807df recursive callEND RequestId: 43a9d5b46c-11e6-b6bc-718f7ec807dfREPORT RequestId: 43a9d5b46c-11e6-b6bc-718f7ec807df Duration: 270.23 ms Billed Duration: 300 ms Memory Size: 1024 MB Max Memory Used: 32 MBSTART RequestId: 446bedb46c-11e6-88fd-1bd64622e38d Version: $LATEST2026 22:39:37.966 (-08:00) 446bedb46c-11e6-88fd-1bd64622e38d received { numberOfCalls: 4 }2026 22:39:37.966 (-08:00) 446bedb46c-11e6-88fd-1bd64622e38d recursive callEND RequestId: 446bedb46c-11e6-88fd-1bd64622e38dREPORT RequestId: 446bedb46c-11e6-88fd-1bd64622e38d Duration: 119.04 ms Billed Duration: 200 ms Memory Size: 1024 MB Max Memory Used: 32 MBSTART RequestId: 4479f6b46c-11e6-b27d58a248a566 Version: $LATEST2026 22:39:38.122 (-08:00) 4479f6b46c-11e6-b27d58a248a566 received { numberOfCalls: 3 }2026 22:39:38.122 (-08:00) 4479f6b46c-11e6-b27d58a248a566 recursive callEND RequestId: 4479f6b46c-11e6-b27d58a248a566REPORT RequestId: 4479f6b46c-11e6-b27d58a248a566 Duration: 40.55 ms Billed Duration: 100 ms Memory Size: 1024 MB Max Memory Used: 32 MBSTART RequestId: 44914fb46c-11e6-ae2b-65715f0c0d90 Version: $LATEST2026 22:39:38.196 (-08:00) 44914fb46c-11e6-ae2b-65715f0c0d90 received { numberOfCalls: 2 }2026 22:39:38.196 (-08:00) 44914fb46c-11e6-ae2b-65715f0c0d90 recursive callEND RequestId: 44914fb46c-11e6-ae2b-65715f0c0d90REPORT RequestId: 44914fb46c-11e6-ae2b-65715f0c0d90 Duration: 32.38 ms Billed Duration: 100 ms Memory Size: 1024 MB Max Memory Used: 32 MBSTART RequestId: 449c72f5-b46c-11e6-a441cb6f0603cc Version: $LATEST2026 22:39:38.268 (-08:00) 449c72f5-b46c-11e6-a441cb6f0603cc received { numberOfCalls: 1 }2026 22:39:38.268 (-08:00) 449c72f5-b46c-11e6-a441cb6f0603cc recursive callEND RequestId: 449c72f5-b46c-11e6-a441cb6f0603ccREPORT RequestId: 449c72f5-b46c-11e6-a441cb6f0603cc Duration: 49.82 ms Billed Duration: 100 ms Memory Size: 1024 MB Max Memory Used: 32 MBSTART RequestId: 44a8f64b-b46c-11e6-b0535b4cab8224 Version: $LATEST2026 22:39:38.350 (-08:00) 44a8f64b-b46c-11e6-b0535b4cab8224 received { numberOfCalls: 0 }2026 22:39:38.350 (-08:00) 44a8f64b-b46c-11e6-b0535b4cab8224 recursive call finishedEND RequestId: 44a8f64b-b46c-11e6-b0535b4cab8224REPORT RequestId: 44a8f64b-b46c-11e6-b0535b4cab8224 Duration: 0.56 ms Billed Duration: 100 ms Memory Size: 1024 MB Max Memory Used: 32 MB