Skip to content
This repository was archived by the owner on Dec 29, 2023. It is now read-only.

Commit 95b8b4a

Browse files
authored
Merge pull request #1 from gigapipehq/lambda-fix
Fixed lambda
2 parents 16b60b3 + 03ffd76 commit 95b8b4a

File tree

5 files changed

+94
-34
lines changed

5 files changed

+94
-34
lines changed

lambda/Dockerfile

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
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"
52

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" ]

lambda/README.md

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,24 @@ curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" \
2626
<br>
2727

2828
## 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.
3030

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
3732
```
3833
$ aws configure
3934
```
4035

41-
3. Review and execute the ‘deploy.sh’ script:
36+
2. Review and execute the ‘deploy.sh’ script:
4237
```
43-
$ ./deploy.sh
38+
$ ./deploy.sh [--tag <value>] [--region <value>] [--profile <default>] [--no-push]
4439
```
4540

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:
4742

4843
![image](https://github.com/chdb-io/chdb-server/assets/1423657/887894c3-35ef-4083-a4b8-29d247f1fc1c)
4944

5045

51-
6. Test your Lambda function with a JSON payload:
46+
4. Test your Lambda function with a JSON payload:
5247

5348
![image](https://github.com/chdb-io/chdb-server/assets/1423657/daa26b0b-68e2-4cec-b665-5505efe99b99)
5449

lambda/deploy.sh

100644100755
Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,58 @@
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+
545
# 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+
1052
# 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

lambda/lambda.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import json
2+
23
import chdb
34

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()
913
return {
1014
"statusCode": 200,
1115
"headers": {
1216
"Content-Type": "application/json"
1317
},
14-
"body": json.dumps(out)
18+
"body": str(res) if not isinstance(res, (dict, list)) else json.dumps(res),
1519
}

lambda/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
boto3
12
chdb

0 commit comments

Comments
 (0)