github.com/Jeffail/benthos/v3@v3.65.0/website/docs/guides/serverless/lambda.md (about)

     1  ---
     2  title: Lambda
     3  description: Deploying Benthos as an AWS Lambda function
     4  ---
     5  
     6  The `benthos-lambda` distribution is a version of Benthos specifically tailored
     7  for deployment as an AWS Lambda function on the `go1.x` runtime,
     8  which runs Amazon Linux on the `x86_64` architecture.
     9  The `benthos-lambda-al2` distribution supports the `provided.al2` runtime,
    10  which runs Amazon Linux 2 on either the `x86_64` or `arm64` architecture.
    11  
    12  It uses the same configuration format as a regular Benthos instance, which can be
    13  provided in 1 of 2 ways:
    14  
    15  1. Inline via the `BENTHOS_CONFIG` environment variable (YAML format).
    16  2. Via the filesystem using a layer, extension, or container image. By default,
    17     the `benthos-lambda` distribution will look for a valid configuration file in
    18     the locations listed below. Alternatively, the configuration file path can be
    19     set explicity by passing a `BENTHOS_CONFIG_PATH` environment variable.
    20    - `./benthos.yaml`
    21    - `./config.yaml`
    22    - `/benthos.yaml`
    23    - `/etc/benthos/config.yaml`
    24    - `/etc/benthos.yaml`
    25  
    26  Also, the `http`, `input` and `buffer` sections are ignored as the service wide
    27  HTTP server is not used, and messages are inserted via function invocations.
    28  
    29  If the `output` section is omitted in your config then the result of the
    30  processing pipeline is returned back to the caller, otherwise the resulting data
    31  is sent to the output destination.
    32  
    33  ### Running with an output
    34  
    35  The flow of a Benthos lambda function with an output configured looks like this:
    36  
    37  ```text
    38                      benthos-lambda
    39             +------------------------------+
    40             |                              |
    41         -------> Processors ----> Output -----> Somewhere
    42  invoke     |                              |        |
    43         <-------------------------------------------/
    44             |         <Ack/Noack>          |
    45             |                              |
    46             +------------------------------+
    47  ```
    48  
    49  Where the call will block until the output target has confirmed receipt of the
    50  resulting payload. When the message is successfully propagated a JSON payload is
    51  returned of the form `{"message":"request successful"}`, otherwise an error is
    52  returned containing the reason for the failure.
    53  
    54  ### Running without an output
    55  
    56  The flow when an output is not configured looks like this:
    57  
    58  ```text
    59                 benthos-lambda
    60             +--------------------+
    61             |                    |
    62         -------> Processors --\  |
    63  invoke     |                 |  |
    64         <---------------------/  |
    65             |     <Result>       |
    66             |                    |
    67             +--------------------+
    68  ```
    69  
    70  Where the function returns the result of processing directly back to the caller.
    71  The format of the result differs depending on the number of batches and messages
    72  of a batch that resulted from the invocation:
    73  
    74  - Single message of a single batch: `{}` (JSON object)
    75  - Multiple messages of a single batch: `[{},{}]` (Array of JSON objects)
    76  - Multiple batches: `[[{},{}],[{}]]` (Array of arrays of JSON objects, batches
    77    of size one are a single object array in this case)
    78  
    79  #### Processing Errors
    80  
    81  The default behaviour of a Benthos lambda is that the handler will not return an
    82  error unless the output fails. This means that errors that occur within your
    83  processors will not result in the handler failing, which will instead return the
    84  final state of the message.
    85  
    86  In the next major version release (V4) this will change and the handler will
    87  fail if messages have encountered an uncaught error during execution. However,
    88  in the meantime it is possible to configure your output to use the new
    89  [`reject` output][output.reject] in order to trigger a handler error on
    90  processor errors:
    91  
    92  ```yaml
    93  output:
    94    switch:
    95      retry_until_success: false
    96      cases:
    97        - check: '!errored()'
    98          output:
    99            sync_response: {}
   100        - output:
   101            reject: "processing failed due to: ${! error() }"
   102  ```
   103  
   104  ### Running a combination
   105  
   106  It's possible to configure pipelines that send messages to third party
   107  destinations and also return a result back to the caller. This is done by
   108  configuring an output block and including an output of the type
   109  `sync_response`.
   110  
   111  For example, if we wished for our lambda function to send a payload to Kafka
   112  and also return the same payload back to the caller we could use a
   113  [broker][output-broker]:
   114  
   115  ```yml
   116  output:
   117    broker:
   118      pattern: fan_out
   119      outputs:
   120      - kafka:
   121          addresses:
   122          - todo:9092
   123          client_id: benthos_serverless
   124          topic: example_topic
   125      - sync_response: {}
   126  ```
   127  
   128  ## Upload to AWS
   129  
   130  ### go1.x on x86_64
   131  
   132  Grab an archive labelled `benthos-lambda` from the [releases page][releases]
   133  page and then create your function:
   134  
   135  ```sh
   136  LAMBDA_ENV=`cat yourconfig.yaml | jq -csR {Variables:{BENTHOS_CONFIG:.}}`
   137  aws lambda create-function \
   138    --runtime go1.x \
   139    --handler benthos-lambda \
   140    --role benthos-example-role \
   141    --zip-file fileb://benthos-lambda.zip \
   142    --environment "$LAMBDA_ENV" \
   143    --function-name benthos-example
   144  ```
   145  
   146  There is also an example [SAM template][sam-template] and
   147  [Terraform resource][tf-example] in the repo to copy from.
   148  
   149  ### provided.al2 on amd64
   150  
   151  Grab an archive labelled `benthos-lambda-al2` for `arm64` from the [releases page][releases]
   152  page and then create your function (AWS CLI v2 only):
   153  
   154  ```sh
   155  LAMBDA_ENV=`cat yourconfig.yaml | jq -csR {Variables:{BENTHOS_CONFIG:.}}`
   156  aws lambda create-function \
   157    --runtime provided.al2 \
   158    --architectures arm64 \
   159    --handler not.used.for.provided.al2.runtime \
   160    --role benthos-example-role \
   161    --zip-file fileb://benthos-lambda.zip \
   162    --environment "$LAMBDA_ENV" \
   163    --function-name benthos-example
   164  ```
   165  
   166  There is also an example [SAM template][sam-template-al2] and
   167  [Terraform resource][tf-example-al2] in the repo to copy from.
   168  
   169  Note that you can also run `benthos-lambda-al2` on x86_64, just use the `amd64` zip instead.
   170  
   171  ## Invoke
   172  
   173  ```sh
   174  aws lambda invoke \
   175    --function-name benthos-example \
   176    --payload '{"your":"document"}' \
   177    out.txt && cat out.txt && rm out.txt
   178  ```
   179  
   180  ## Build
   181  
   182  You can build and archive the function yourself with:
   183  
   184  ```sh
   185  go build github.com/Jeffail/benthos/v3/cmd/serverless/benthos-lambda
   186  zip benthos-lambda.zip benthos-lambda
   187  ```
   188  
   189  [releases]: https://github.com/Jeffail/benthos/releases
   190  [sam-template]: https://github.com/Jeffail/benthos/tree/master/resources/serverless/lambda/benthos-lambda-sam.yaml
   191  [tf-example]: https://github.com/Jeffail/benthos/tree/master/resources/serverless/lambda/benthos-lambda.tf
   192  [sam-template-al2]: https://github.com/Jeffail/benthos/tree/master/resources/serverless/lambda/benthos-lambda-al2-sam.yaml
   193  [tf-example-al2]: https://github.com/Jeffail/benthos/tree/master/resources/serverless/lambda/benthos-lambda-al2.tf
   194  [output-broker]: /docs/components/outputs/broker
   195  [output.reject]: /docs/components/outputs/reject