Skip to content

Commit 947de2f

Browse files
IaC for load custom csv lambda
1 parent 0c262d5 commit 947de2f

File tree

5 files changed

+112
-2
lines changed

5 files changed

+112
-2
lines changed

lib/workload/stateless/stacks/metadata-manager/README.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ on the model of the record.
6161

6262
## How things work
6363

64-
### How Syncing The Data Works
64+
### How Tracking Sheet Syncing Works
6565

6666
In the near future, we might introduce different ways to load data into the application. For the time being, we are
6767
loading data
@@ -100,6 +100,18 @@ Some important notes of the sync:
100100

101101
Please refer to the [tracking-sheet-service](proc/service/tracking_sheet_srv.py) implementation.
102102

103+
### Loading from external csv
104+
105+
The Metadata Manager has the capability to import metadata from an external CSV file. This CSV file should follow the
106+
same mapping structure as specified in the tracking sync process. The loading operation utilizes a presigned URL, which
107+
is subsequently used to load the data into the Metadata Manager. Not all header should be present in the CSV file, but
108+
the required fields are:
109+
110+
- `library_id`
111+
- `subject_id`
112+
113+
To trigger this operation, trigger from the lambda specified in `./deploy/README.md`.
114+
103115
### Audit Data
104116

105117
The application is configured with [django-simple-history](https://django-simple-history.readthedocs.io/en/latest/)

lib/workload/stateless/stacks/metadata-manager/deploy/README.md

+32
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,35 @@ aws lambda invoke \
5858
--cli-binary-format raw-in-base64-out \
5959
res.json
6060
```
61+
62+
### CustomCsvLambda
63+
64+
- Load tracking sheet data from csv presigned url
65+
66+
To manually trigger the sync, the lambda ARN is stored in the SSM Parameter Store named
67+
`/orcabus/metadata-manager/load-custom-csv-lambda-arn`.
68+
69+
To query in a local terminal
70+
71+
```sh
72+
load_custom_csv_lambda_arn=$(aws ssm get-parameter --name '/orcabus/metadata-manager/load-custom-csv-lambda-arn' --with-decryption | jq -r .Parameter.Value)
73+
```
74+
75+
The lambda handler will accept a json which only accepts a single key `url` which is the presigned url of the csv file.
76+
77+
```json
78+
{
79+
"url": "https://example.com/csv"
80+
}
81+
```
82+
83+
Invoking lambda cmd:
84+
85+
```sh
86+
aws lambda invoke \
87+
--function-name load_custom_csv_lambda_arn \
88+
--invocation-type Event \
89+
--payload '{ "url": "https://the.url.csv" }' \
90+
--cli-binary-format raw-in-base64-out \
91+
res.json
92+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import path from 'path';
2+
import { Construct } from 'constructs';
3+
import { Duration } from 'aws-cdk-lib';
4+
import { PythonFunction } from '@aws-cdk/aws-lambda-python-alpha';
5+
import { ISecret } from 'aws-cdk-lib/aws-secretsmanager';
6+
import { StringParameter } from 'aws-cdk-lib/aws-ssm';
7+
import {
8+
DockerImageFunction,
9+
DockerImageFunctionProps,
10+
DockerImageCode,
11+
} from 'aws-cdk-lib/aws-lambda';
12+
13+
type LambdaProps = {
14+
/**
15+
* The basic common lambda properties that it should inherit from
16+
*/
17+
basicLambdaConfig: Partial<DockerImageFunctionProps>;
18+
/**
19+
* The secret for the db connection where the lambda will need access to
20+
*/
21+
dbConnectionSecret: ISecret;
22+
};
23+
24+
export class LambdaLoadCustomCSVConstruct extends Construct {
25+
private readonly lambda: PythonFunction;
26+
27+
constructor(scope: Construct, id: string, lambdaProps: LambdaProps) {
28+
super(scope, id);
29+
30+
this.lambda = new DockerImageFunction(this, 'LoadCustomCSVLambda', {
31+
environment: {
32+
...lambdaProps.basicLambdaConfig.environment,
33+
},
34+
securityGroups: lambdaProps.basicLambdaConfig.securityGroups,
35+
vpc: lambdaProps.basicLambdaConfig.vpc,
36+
vpcSubnets: lambdaProps.basicLambdaConfig.vpcSubnets,
37+
architecture: lambdaProps.basicLambdaConfig.architecture,
38+
code: DockerImageCode.fromImageAsset(path.join(__dirname, '../../../'), {
39+
file: 'deploy/construct/lambda-load-custom-csv/lambda.Dockerfile',
40+
}),
41+
timeout: Duration.minutes(15),
42+
memorySize: 4096,
43+
});
44+
45+
lambdaProps.dbConnectionSecret.grantRead(this.lambda);
46+
47+
// We need to store this lambda ARN somewhere so that we could refer when need to load this manually
48+
const ssmParameter = new StringParameter(this, 'LoadCustomCSVLambdaArnParameterStore', {
49+
parameterName: '/orcabus/metadata-manager/load-custom-csv-lambda-arn',
50+
description: 'The ARN of the lambda that load metadata from a presigned URL CSV file',
51+
stringValue: this.lambda.functionArn,
52+
});
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM public.ecr.aws/lambda/python:3.12
2+
3+
WORKDIR ${LAMBDA_TASK_ROOT}
4+
5+
# COPY all files
6+
COPY . .
7+
8+
# Install the specified packages
9+
RUN pip install -r deps/requirements-full.txt
10+
11+
# Specify handler
12+
CMD [ "handler.load_custom_metadata_csv.handler" ]

lib/workload/stateless/stacks/metadata-manager/docs/architecture.drawio.svg

+1-1
Loading

0 commit comments

Comments
 (0)