This repository was archived by the owner on Dec 29, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 5 files changed +94
-34
lines changed Expand file tree Collapse file tree 5 files changed +94
-34
lines changed Original file line number Diff line number Diff line change 1
- FROM public.ecr.aws/lambda/python:latest
2
- COPY lambda.py ./
3
- COPY requirements.txt ./
4
- RUN python3 -m pip install -r requirements.txt
1
+ ARG FUNCTION_DIR="/function"
5
2
6
- CMD ["lambda.lambda_handler" ]
3
+ FROM python:3.11 as build-image
4
+
5
+ ARG FUNCTION_DIR
6
+
7
+ RUN mkdir -p ${FUNCTION_DIR}
8
+ COPY lambda.py ${FUNCTION_DIR}
9
+ COPY requirements.txt ${FUNCTION_DIR}
10
+
11
+ RUN pip install --target ${FUNCTION_DIR} -r "${FUNCTION_DIR}/requirements.txt"
12
+ RUN pip install --target ${FUNCTION_DIR} awslambdaric
13
+
14
+ FROM python:3.11-slim
15
+
16
+ ARG FUNCTION_DIR
17
+ WORKDIR ${FUNCTION_DIR}
18
+
19
+ COPY --from=build-image ${FUNCTION_DIR} ${FUNCTION_DIR}
20
+
21
+ ENTRYPOINT [ "/usr/local/bin/python" , "-m" , "awslambdaric" ]
22
+ CMD [ "lambda.handler" ]
Original file line number Diff line number Diff line change @@ -26,29 +26,24 @@ curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" \
26
26
<br >
27
27
28
28
## Upload Docker image on ECR and Lambda
29
- Lambda function continers must be hosted on the AWS Elastic Container Registry.
29
+ Lambda function containers must be hosted on the AWS Elastic Container Registry.
30
30
31
- 1 . Export your AWS account id in the shell or better yet, add it your ~ /.bashrc or ~ /.bash_profile
32
- ```
33
- $ export AWS_ACCOUNT_ID = <account_id>
34
- ```
35
-
36
- 2 . Install the AWS CLI and configure with your AWS credentials
31
+ 1 . Install the AWS CLI and configure with your AWS credentials
37
32
```
38
33
$ aws configure
39
34
```
40
35
41
- 3 . Review and execute the ‘deploy.sh’ script:
36
+ 2 . Review and execute the ‘deploy.sh’ script:
42
37
```
43
- $ ./deploy.sh
38
+ $ ./deploy.sh [--tag <value>] [--region <value>] [--profile <default>] [--no-push]
44
39
```
45
40
46
- 4 . Create Lambda function and attach your ECR Image. Make sure the name and image ID match:
41
+ 3 . Create Lambda function and attach your ECR Image. Make sure the name and image ID match:
47
42
48
43
![ image] ( https://github.com/chdb-io/chdb-server/assets/1423657/887894c3-35ef-4083-a4b8-29d247f1fc1c )
49
44
50
45
51
- 6 . Test your Lambda function with a JSON payload:
46
+ 4 . Test your Lambda function with a JSON payload:
52
47
53
48
![ image] ( https://github.com/chdb-io/chdb-server/assets/1423657/daa26b0b-68e2-4cec-b665-5505efe99b99 )
54
49
Original file line number Diff line number Diff line change 1
- URL_STRING=" .dkr.ecr.us-east-1.amazonaws.com"
2
- CONTAINER_STRING=" chdb"
3
- IMAGE_STRING=" latest"
4
- ECR_IMAGE_URI=" $AWS_ACCOUNT_ID$URL_STRING /$CONTAINER_STRING :$IMAGE_STRING "
1
+ # get flag variables
2
+ profile=" default"
3
+ region=" us-east-1"
4
+ tag=" latest"
5
+ no_push=false
6
+
7
+ while (( "$# " )) ; do
8
+ case " $1 " in
9
+ --tag)
10
+ tag=" $2 "
11
+ shift 2
12
+ ;;
13
+ --region)
14
+ region=" $2 "
15
+ shift 2
16
+ ;;
17
+ --profile)
18
+ profile=" $2 "
19
+ shift 2
20
+ ;;
21
+ --no-push)
22
+ no_push=true
23
+ shift
24
+ ;;
25
+ --)
26
+ shift
27
+ break
28
+ ;;
29
+ -* |--* =)
30
+ echo " Error: Unsupported flag $1 " >&2
31
+ exit 1
32
+ ;;
33
+ * )
34
+ shift
35
+ ;;
36
+ esac
37
+ done
38
+
39
+
40
+ # set variables
41
+ AWS_ACCOUNT_ID=$( aws sts get-caller-identity --query Account --output text)
42
+ ECR_IMAGE_URI=" $AWS_ACCOUNT_ID .dkr.ecr.$region .amazonaws.com"
43
+ IMAGE_NAME=" $ECR_IMAGE_URI /chdb:$tag "
44
+
5
45
# log in to ECR
6
- aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin " $AWS_ACCOUNT_ID$URL_STRING "
7
- # remove previous images to save space
8
- docker rmi " $AWS_ACCOUNT_ID$URL_STRING /$CONTAINER_STRING "
9
- docker rmi " $CONTAINER_STRING "
46
+ aws ecr get-login-password --region $region --profile $profile | \
47
+ docker login --username AWS --password-stdin $ECR_IMAGE_URI
48
+
49
+ # remove existing image
50
+ docker rmi $IMAGE_NAME 2> /dev/null || true
51
+
10
52
# build image
11
- docker build --tag " $CONTAINER_STRING " .
12
- # tag and push to AWS ECR
13
- docker tag $CONTAINER_STRING :latest " $ECR_IMAGE_URI "
14
- docker push " $ECR_IMAGE_URI "
53
+ docker build -t $IMAGE_NAME .
54
+
55
+ if [ " $no_push " = false ]; then
56
+ # push to ECR
57
+ docker push $IMAGE_NAME
58
+ fi
Original file line number Diff line number Diff line change 1
1
import json
2
+
2
3
import chdb
3
4
4
- def lambda_handler (event , context ):
5
- query = event ['query' ] or "SELECT version()"
6
- format = event ['default_format' ] or "JSONCompact"
7
- res = chdb .query (query , format )
8
- out = json .loads (res .data ())
5
+
6
+ def handler (event , context ):
7
+ if "requestContext" in event :
8
+ event = json .loads (event ["body" ])
9
+ query = event ["query" ] if "query" in event else "SELECT version()"
10
+ format = event ["default_format" ] if "default_format" in event else "JSONCompact"
11
+
12
+ res = chdb .query (query , format ).data ()
9
13
return {
10
14
"statusCode" : 200 ,
11
15
"headers" : {
12
16
"Content-Type" : "application/json"
13
17
},
14
- "body" : json .dumps (out )
18
+ "body" : str ( res ) if not isinstance ( res , ( dict , list )) else json .dumps (res ),
15
19
}
Original file line number Diff line number Diff line change
1
+ boto3
1
2
chdb
You can’t perform that action at this time.
0 commit comments