/aws-lambda-post-runner

Run a command after the lambda handler function returns.

Primary LanguageRustMIT LicenseMIT

AWS Lambda Post Runner

version license rust

overview

Run a command after the lambda handler function returns.

This is helpful if you want to response to the synchronous invoker (e.g. API gateway) as soon as possible then run some tasks after that (e.g. uploading logs to S3, sending metrics to CloudWatch).

Usage

As a Lambda Layer

  1. Download the prebuilt zip from the release page. You can also build it yourself by running cargo build --release, then zip scripts/entry.sh with target/release/aws-lambda-post-runner.
  2. Upload the zip as a lambda layer. Add the layer to your lambda function.
  3. Add an environment variable AWS_LAMBDA_EXEC_WRAPPER to the lambda function with the value /opt/entry.sh to enable the runner.
  4. Configure the environment variables below to set the command you want to run.

As a Binary Executable

If you are using a custom lambda runtime (for rust, golang, c++, etc) or container image, you can run this as a parent process of your main handler process.

  1. Download the prebuilt zip from the release page to get the aws-lambda-post-runner executable. You can also build it yourself by running cargo build --release.
  2. Modify the entry command of the lambda function to aws-lambda-post-runner <handler-command> <handler-args>
  3. Configure the environment variables below to set the command you want to run.

Environment Variables

  • AWS_LAMBDA_POST_RUNNER_COMMAND (required)
    • The command to run after the lambda handler function returns.
  • AWS_LAMBDA_POST_RUNNER_MODE
    • Controls when the command is executed.
    • Valid values are:
      • AfterResponse
      • AfterError
      • Multiple values can be separated by ,.
    • If not set, the default value is AfterResponse,AfterError.
  • AWS_LAMBDA_POST_RUNNER_LOG_LEVEL
    • The log level. Set to debug to print debug logs.

FAQ

  • Q: Will this save my money by running some logic after the lambda handler returns?
    • No. The lambda charges until the post runner finishes.
  • Q: How can I verify that the command is executed after the lambda handler returns?
    • Try to set the command to something like sleep 10 && echo done then invoke the lambda via API Gateway. You should see your HTTP response immediately and the logs show done after 10 seconds.
  • Q: Can I bring my own binary executable and run it in the command?
  • Q: Will the command be executed when the lambda function timeout?
    • No. When timeout nothing can be executed.
  • Q: How does this work?
    • By using AWS Lambda Runtime Proxy this tool can intercept the lambda handler function's return value and run the command after that.
  • Q: Can I run some logic in another thread of the handler process after the handler function returns, instead of running a separate command?
    • It depends on the runtime. If the runtime is blocked when waiting for the runtime API response, then the post processing logic might not be executed.
    • For example in NodeJS runtime, the Lambda Runtime API client is implemented in C++. When we suppress the runtime API invocation, the C++ code will wait for the response and block the execution of NodeJS code. You can verify this by add a setInterval(() => console.log('tick'), 100) to your NodeJS code and sleep for some seconds in the post runner command. You will see the tick stops during the sleep time.