|
| 1 | +# AWS Lambda Automation |
| 2 | + |
| 3 | +AWS Lambda functions are a smart way to auto-execute code on every log file upload. |
| 4 | + |
| 5 | +Below we describe how you can set up an AWS Lambda function to automatically run the dashboard-writer script every time a new log file is uploaded. |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## Before you get started |
| 10 | + |
| 11 | +This is an advanced topic and we recommend that you get the basics in place first: |
| 12 | +- Test that your InfluxDB/Grafana setup works with the sample data |
| 13 | +- Test that your setup works with your own data/server when manually running the script from your PC |
| 14 | +- Make sure your log file split size, `inputs.py` and InfluxDB account settings are setup as needed (see below) |
| 15 | + |
| 16 | +### Regarding dependencies |
| 17 | +The dashboard-writer script relies on a number of dependencies that need to be provided to the AWS Lambda function. To make this easy, we have pre-built 'layers' for the major AWS S3 regions. You can find the latest layer list in our Releases page. See below for details. By providing your AWS Lambda function with an 'ARN identifier' for a pre-built layer, you ensure that all relevant dependencies are in place. The ARN should match the region that your S3 bucket is setup in. |
| 18 | + |
| 19 | +### Regarding log file size and InfluxDB account type |
| 20 | +If you're initially testing your setup with a free InfluxDB Cloud starter account, keep in mind that there is a 'write restriction' of 5 MB per 5 minutes. This means that if you try to write e.g. 30 MB of data, it will take > 30 minutes. This exceeds the AWS Lambda max timeout. If you're using AWS Lambda, we recommend that you ensure your log file split size is 2-5 MB and that the data you extract is optimized (i.e. only push relevant signals at relevant resampled frequency). Depending on your use case, this will let you set up a basic PoC. |
| 21 | + |
| 22 | +For 'production setups', we recommend that you either self-host InfluxDB or use a paid InfluxDB Cloud if you intend to utilize AWS Lambda for automation. |
| 23 | + |
| 24 | +### Use a test bucket before you start |
| 25 | +Before you deploy the AWS Lambda function with your 'main bucket' we recommend to set up an empty test bucket for testing. You can then change the bucket you trigger based on once you're ready. |
| 26 | + |
| 27 | +### Regarding AWS and Lambda costs |
| 28 | +We recommend tracking your AWS billing costs when working with Lambda functions to ensure everything is set up correctly. We do not take responsibility for incorrect implementations or unexpected costs related to implementation of the below. Note also that the below is intended as a 'getting started' guide - not a fully cost optimized setup. |
| 29 | + |
| 30 | +--- |
| 31 | + |
| 32 | +## Deploy your AWS Lambda function |
| 33 | + |
| 34 | +1. Create an [IAM execution](https://docs.aws.amazon.com/lambda/latest/dg/lambda-intro-execution-role.html) role with permissions: `AWSLambdaBasicExecutionRole` + `AmazonS3FullAccess` |
| 35 | +2. Go to 'Services/Lambda', then select your S3 bucket region (upper right corner) |
| 36 | +3. Add a new Lambda function with a name, a Python 3.7 environment and your execution role |
| 37 | +4. Add a 'Trigger': Select S3, your test bucket, `All object create events` and suffix `.MF4` |
| 38 | +5. Create a zip containing `lambda_handler.py`, `utils.py` and `inputs.py` (ensure your inputs are updated) |
| 39 | +6. Upload the zip via the Lambda 'Actions' button and confirm that your code shows in the online code editor |
| 40 | +7. Find the pre-built layer ARN for your AWS S3 region in the `aws_lambda_layers.txt` file on our releases page |
| 41 | +8. In the 'Designer' tap, click Layers/Add a layer/Specify an ARN and parse your ARN |
| 42 | +9. Scroll to 'Basic settings' and set the 'Timeout' to `3 min` and memory to `1024 MB` (you can test/tweak these later) |
| 43 | +10. Save the script and click 'Deploy', then 'Test' (using the below test data) and verify that it succeeds |
| 44 | +11. Click 'Actions/Publish' and test that it works by |
| 45 | +12. Click 'Test' in the upper right corner and add the test JSON content below |
| 46 | +13. When you're ready, click 'Actions/Publish' to save a new version |
| 47 | +14. In AWS Services, go to Cloudwatch/Logs/Log groups and click your Lambda function to monitor events |
| 48 | +15. Download a logfile via CANcloud from your main bucket and upload to your test bucket via CANcloud (from the Home tab) |
| 49 | +16. Verify that the Lambda function is triggered within a minute and check from the log output that it processes the data |
| 50 | +17. Verify that data is written correctly to InfluxDB |
| 51 | + |
| 52 | +Once everything is tested, you can change the 'Trigger' S3 bucket to your main bucket. You should of course monitor that it works as intended over a period. |
| 53 | + |
| 54 | + |
| 55 | +#### Lambda function test event data |
| 56 | + |
| 57 | +``` |
| 58 | +{ |
| 59 | + "Records": [ |
| 60 | + { |
| 61 | + "s3": { |
| 62 | + "bucket": { |
| 63 | + "name": "arbitrary-bucket-name", |
| 64 | + "arn": "arn:aws:s3:::arbitrary-bucket-name" |
| 65 | + }, |
| 66 | + "object": { |
| 67 | + "key": "3F78A21D/00000086/00000001-5F4E8ABC.MF4" |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + ] |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +--- |
| 76 | + |
| 77 | +## Build custom ARN layer package |
| 78 | +If you need to create your own AWS Lambda layer, you can take outset in the steps below (Windows): |
| 79 | + |
| 80 | +1. Add a new build folder for the build process, e.g. `aws-lambda-layer/` |
| 81 | +2. Install [Docker Desktop for Windows](https://hub.docker.com/editions/community/docker-ce-desktop-windows) |
| 82 | +3. Open your command prompt and run `docker pull lambci/lambda` |
| 83 | +4. Install the [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) |
| 84 | +5. Open your command prompt and run `aws configure` and provide your credentials |
| 85 | +6. Open Docker and go to 'Settings/Resources/File Sharing', then add your new folder |
| 86 | +7. Copy the dashboard-writer `requirements.txt` file into your build folder |
| 87 | +8. In the build folder, create a `build.bat` file with below content (update the layer name and region) |
| 88 | +9. Open your command line in the folder and run `run.bat` - this will take a few minutes |
| 89 | +10. Once done, you can use the `LayerVersionArn` value from the `APN.txt` - e.g. as below: |
| 90 | +`arn:aws:lambda:us-east-2:319723967016:layer:css-electronics-dashboard-writer:10` |
| 91 | + |
| 92 | +``` |
| 93 | +rmdir /S/Q python |
| 94 | +mkdir python\lib\python3.7\site-packages |
| 95 | +docker run -v "%cd%":/var/task "lambci/lambda:build-python3.7" /bin/sh -c "pip install -r requirements.txt -t python/lib/python3.7/site-packages/; exit" |
| 96 | +rmdir /S/Q python\lib\python3.7\site-packages\botocore |
| 97 | +zip -r dashboard-writer.zip python |
| 98 | +aws lambda publish-layer-version --region us-east-2 --layer-name my-layer --description "Dashboard Writer Script Dependencies" --zip-file "fileb://dashboard-writer.zip" --compatible-runtimes python3.7 > APN.txt |
| 99 | +``` |
0 commit comments