Skip to content

Commit 4c71a49

Browse files
authored
Merge pull request #33 from ONS-Innovation/lambda_memory_scaling
Configurable Lambda Memory
2 parents bc4c89f + 404adc5 commit 4c71a49

5 files changed

Lines changed: 70 additions & 66 deletions

File tree

data_logger/README.md

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,38 @@ This repository makes use of a Makefile to execute common commands. To view all
2626
make all
2727
```
2828

29+
## `config.json`
30+
31+
The lambda function contains a configuration file ([`config.json`](./config/config.json)). This file is used to setup key values within the script.
32+
33+
`config.json` is divided into 2 high level keys: features and settings.
34+
35+
The features key is used to enable or disable specific features within the tool (for example if you didn't want to collect dependabot information).
36+
37+
The settings key is used to store key values used to collect and process data. This includes values such as how many threads to collect data with and how many years a repository must go without updates before it is considered inactive.
38+
39+
The table below describes each configuration parameter and its use.
40+
41+
### Features
42+
43+
| Parameter | Description | Default |
44+
| --------- | ----------- | ------- |
45+
| `repository_collection` | Whether to collect repository data or not. | true |
46+
| `dependabot_collection` | Whether to collect dependabot data or not. | true |
47+
| `secret_scanning_collection` | Whether to collection secret scanning data or not. | true |
48+
| `show_log_locally` | This is for development purposes. This controls whether the log is stored locally as `debug.log`. This allows developers to see logging outputs when running the tool locally | true |
49+
| `write_to_s3` | Whether the tool should write its outputs to S3 or store them locally. Local storage is useful when testing / developing the tool locally. Local outputs are kept within `./output/`. When deploying to AWS, this key should **always** be `true`. | true |
50+
51+
### Settings
52+
53+
| Parameter | Description | Default |
54+
| --------- | ----------- | ------- |
55+
| `thread_count` | The number of threads to collect and process data with. | 20 |
56+
| `dependabot_thresholds` | This contains information about how many days a dependabot alert for a given severity is open before being considered a policy breach. | critical (5), high (15), medium (60), low (90) |
57+
| `secret_scanning_threshold` | The number of days a secret scanning alert must be open for before being considered a policy breach. | 5 days |
58+
| `inactivity_threshold` | The number of years a project goes without updates before being considered inactive. | 1 year |
59+
| `signed_commit_number` | The number of repository commits to check within the signed commit check (for example, when set to 15, only the 15 most recent commits). | 15 |
60+
2961
## Development
3062

3163
To work on this project, you need to:
@@ -217,23 +249,31 @@ All of the commands (steps 2-5) are available for your environment within the AW
217249
aws ecr get-login-password --region eu-west-2 | docker login --username AWS --password-stdin <aws_account_id>.dkr.ecr.eu-west-2.amazonaws.com
218250
```
219251
220-
3. Ensuring you're at the root of the repository, build a docker image of the project.
252+
3. Ensure `config.json` is set correctly.
253+
254+
When running the data logger within AWS, it is essential that `write_to_s3` is set to `true`.
255+
256+
If this is not set correctly, the lambda function will run *without* storing its output.
257+
258+
For more information, see [`config.json`](#configjson).
259+
260+
4. Ensuring you're at the root of the repository, build a docker image of the project.
221261
222262
```bash
223263
docker build -t sdp-dev-policy-dashboard-lambda .
224264
```
225265
226266
**Please Note:** Change `sdp-dev-policy-dashboard-lambda` within the above command to `<env_name>-<lambda_name>`.
227267
228-
4. Tag the docker image to push to AWS, using the correct versioning mentioned in [prerequisites](#deployment-prerequisites).
268+
5. Tag the docker image to push to AWS, using the correct versioning mentioned in [prerequisites](#deployment-prerequisites).
229269
230270
```bash
231271
docker tag sdp-dev-policy-dashboard-lambda:latest <aws_account_id>.dkr.ecr.eu-west-2.amazonaws.com/sdp-dev-policy-dashboard-lambda:<semantic_version>
232272
```
233273
234274
**Please Note:** Change `sdp-dev-policy-dashboard-lambda` within the above command to `<env_name>-<lambda_name>`.
235275
236-
5. Push the image to ECR.
276+
6. Push the image to ECR.
237277
238278
```bash
239279
docker push <aws_account_id>.dkr.ecr.eu-west-2.amazonaws.com/sdp-dev-policy-dashboard-lambda:<semantic_version>
@@ -243,6 +283,22 @@ Once pushed, you should be able to see your new image version within the ECR rep
243283
244284
### Deploying the Lambda
245285
286+
#### Configuration
287+
288+
##### `lambda_memory`
289+
290+
Within your `.tfvars` for the respective environment, you can define the amount of memory assigned to the lambda function. The amount of memory given to a lambda also affects how much CPU resource is assigned to it. For more information on this see:
291+
292+
- [Configure Lambda function memory](https://docs.aws.amazon.com/lambda/latest/dg/configuration-memory.html)
293+
- [Lambda quotas](https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-limits.html)
294+
295+
Within our environments, this value will need to vary.
296+
297+
- For **sdp-dev**, `lambda_memory` should be set to 128. This is to reduce cost and, because of the decreased volume of information, reduces wasted resource.
298+
- For **sdp-prod**, `lambda_memory` should be set to 1024. This is because of the larger volume of data needing to be processed within the lambda's runtime (15 minutes).
299+
300+
#### Instructions
301+
246302
Once AWS ECR has the new container image, we need to update the Lambda's configuration to use it. To do this, use the repository's provided [Terraform](./terraform/).
247303
248304
Within the terraform directory, there is a [service](../terraform/data_logger/) subdirectory which contains the terraform to setup the lambda on AWS.
@@ -261,6 +317,8 @@ Within the terraform directory, there is a [service](../terraform/data_logger/)
261317
262318
**It is crucial that the completed `.tfvars` file does not get committed to GitHub.**
263319
320+
Ensure that you have read the [configuration guide](#configuration) **before** deployment.
321+
264322
3. Initialise the terraform using the appropriate `.tfbackend` file for the environment (`env/dev/backend-dev.tfbackend` or `env/prod/backend-prod.tfbackend`).
265323
266324
```bash

terraform/dashboard/.terraform.lock.hcl

Lines changed: 0 additions & 63 deletions
This file was deleted.

terraform/data_logger/env/sandbox/example_tfvars.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ lambda_name = "policy-dashboard-lambda"
88
lambda_version = "v0.0.1"
99
lambda_arch = "x86_64"
1010
lambda_timeout = 300
11+
lambda_memory = 1024
1112
schedule = "cron(0 6 ? * 2 *)"
1213
github_org = "ONS-Innovation"
1314
github_app_client_id = "123456789"

terraform/data_logger/main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ resource "aws_lambda_function" "lambda_function" {
3636
security_group_ids = [aws_security_group.lambda_sg.id] // Dedicated security group for Lambda function
3737
}
3838

39+
memory_size = var.lambda_memory
40+
3941
role = aws_iam_role.lambda_function_role.arn
4042

4143
environment {

terraform/data_logger/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ variable "github_app_client_id" {
7171
type = string
7272
}
7373

74+
variable "lambda_memory" {
75+
description = "AWS Lambda Memory Size"
76+
type = number
77+
default = 1024
78+
}
79+
7480
variable "region" {
7581
description = "AWS region"
7682
type = string

0 commit comments

Comments
 (0)