|
| 1 | +--- |
| 2 | +title: Create custom tasks (AWS lambdas) and execute using Carrier Platform |
| 3 | +author: User |
| 4 | +date: 2024-03-01 12:00:00 +0800 |
| 5 | +categories: [Performance, Tutorial, Tasks] |
| 6 | +tags: [performance, backend, tasks, reporting] |
| 7 | +render_with_liquid: false |
| 8 | +--- |
| 9 | + |
| 10 | +## Overview |
| 11 | + |
| 12 | +This guide provides step-by-step instructions on how to create custom tasks (AWS lambdas) and execute using Carrier Platform. Using custom tasks you can add additional functionality for processing and reporting test results. Additional info about lambdas you can find [here](https://github.com/carrier-io/docker-lambda) |
| 13 | + |
| 14 | +### Entrypoint and parameters for task |
| 15 | + |
| 16 | +The Lambda function handler is the method in your function code that processes events. When your function is invoked, Lambda runs the handler method. Your function runs until the handler returns a response, exits, or times out. You can use the following general syntax when creating a function handler in Python: |
| 17 | + |
| 18 | +```bash |
| 19 | +def handler_name(event, context): |
| 20 | + ... |
| 21 | + return some_value |
| 22 | +``` |
| 23 | + |
| 24 | +The Lambda function handler name specified at the time that you create a Task in Carrier is derived from: |
| 25 | + |
| 26 | + - The name of the file in which the Lambda handler function is located. |
| 27 | + |
| 28 | + - The name of the Python handler function. |
| 29 | + |
| 30 | +A function handler can be any name; however, the default name in the Lambda console is lambda_function.lambda_handler. This function handler name reflects the function name (lambda_handler) and the file where the handler code is stored (lambda_function.py). |
| 31 | + |
| 32 | +The first argument for lambda handler is the event object. It contains all the parameters that you have specified for the task during task creation. An event is a JSON-formatted document that contains data for a Lambda function to process. The Lambda runtime converts the event to an object and passes it to your function code. It is usually of the Python dict type. It can also be list, str, int, float, or the NoneType type. |
| 33 | + |
| 34 | +In your function code you can read parameters from event object like this: |
| 35 | + |
| 36 | +```bash |
| 37 | +report_id = event.get("report_id") |
| 38 | +project_id = event.get("project_id") |
| 39 | +token = event.get("token") |
| 40 | +some_parameter = event.get("some_parameter") |
| 41 | +``` |
| 42 | + |
| 43 | +### Additional requirements and packaging |
| 44 | + |
| 45 | +The official documentation from AWS you can find [here](https://docs.aws.amazon.com/lambda/latest/dg/python-package.html#python-package-create-dependencies) |
| 46 | + |
| 47 | +If your function requires some not standard python libraries you need to specify them in requirements.txt file. Example you can find [here](https://github.com/carrier-io/control_tower/blob/master/package/requirements.txt). After that you can build your lambda function as zip archive using instruction from official documentation. Also, you can create a bash script to build lambda function using docker container + zip utility. An example below: |
| 48 | + |
| 49 | +```bash |
| 50 | +#!/bin/bash |
| 51 | + |
| 52 | +mkdir lambda |
| 53 | +# build all dependencies from requirements.txt |
| 54 | +docker run --rm -v "$PWD":/var/task lambci/lambda:build-python3.7 pip install -r requirements.txt -t /var/task/lambda |
| 55 | +# copy some additional code required by lambda function |
| 56 | +cp -r perfreporter lambda/perfreporter |
| 57 | +# copy template for email notification |
| 58 | +cp -r templates lambda/templates |
| 59 | +# copy lambda handler |
| 60 | +cp post_processing.py lambda/post_processing.py |
| 61 | +cd lambda |
| 62 | +# create zip archive |
| 63 | +zip jtl_to_excel.zip -r . |
| 64 | +cp jtl_to_excel.zip ../ |
| 65 | +cd .. |
| 66 | +rm -rf lambda |
| 67 | +``` |
| 68 | + |
| 69 | +### Create task in Carrier |
| 70 | + |
| 71 | +To create a lambda task in Carrier Platform you should follow next steps: |
| 72 | + |
| 73 | +1. Go to Configuration section and then select Tasks subsection |
| 74 | + |
| 75 | +2. Click "+" button to start creation of new task |
| 76 | + |
| 77 | +3. Set task name and select Runtime for your function |
| 78 | + |
| 79 | +4. Upload your zip file with lambda function and specify lambda handler |
| 80 | + |
| 81 | +5. Add parameters for your task |
| 82 | + |
| 83 | +6. Click Save button to create the task |
| 84 | + |
| 85 | + |
| 86 | +### Execute task |
| 87 | + |
| 88 | +To execute the task you need to select it from tasks list and click Play button |
| 89 | + |
| 90 | + |
| 91 | +After that you can check tasks logs in runtime below |
| 92 | + |
0 commit comments