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

Commit

Permalink
Merge pull request #1 from gigapipehq/lambda-fix
Browse files Browse the repository at this point in the history
Fixed lambda
  • Loading branch information
lmangani authored Aug 1, 2023
2 parents 16b60b3 + 03ffd76 commit 95b8b4a
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 34 deletions.
26 changes: 21 additions & 5 deletions lambda/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
FROM public.ecr.aws/lambda/python:latest
COPY lambda.py ./
COPY requirements.txt ./
RUN python3 -m pip install -r requirements.txt
ARG FUNCTION_DIR="/function"

CMD ["lambda.lambda_handler"]
FROM python:3.11 as build-image

ARG FUNCTION_DIR

RUN mkdir -p ${FUNCTION_DIR}
COPY lambda.py ${FUNCTION_DIR}
COPY requirements.txt ${FUNCTION_DIR}

RUN pip install --target ${FUNCTION_DIR} -r "${FUNCTION_DIR}/requirements.txt"
RUN pip install --target ${FUNCTION_DIR} awslambdaric

FROM python:3.11-slim

ARG FUNCTION_DIR
WORKDIR ${FUNCTION_DIR}

COPY --from=build-image ${FUNCTION_DIR} ${FUNCTION_DIR}

ENTRYPOINT [ "/usr/local/bin/python", "-m", "awslambdaric" ]
CMD [ "lambda.handler" ]
17 changes: 6 additions & 11 deletions lambda/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,24 @@ curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" \
<br>

## Upload Docker image on ECR and Lambda
Lambda function continers must be hosted on the AWS Elastic Container Registry.
Lambda function containers must be hosted on the AWS Elastic Container Registry.

1. Export your AWS account id in the shell or better yet, add it your ~/.bashrc or ~/.bash_profile
```
$ export AWS_ACCOUNT_ID = <account_id>
```

2. Install the AWS CLI and configure with your AWS credentials
1. Install the AWS CLI and configure with your AWS credentials
```
$ aws configure
```

3. Review and execute the ‘deploy.sh’ script:
2. Review and execute the ‘deploy.sh’ script:
```
$ ./deploy.sh
$ ./deploy.sh [--tag <value>] [--region <value>] [--profile <default>] [--no-push]
```

4. Create Lambda function and attach your ECR Image. Make sure the name and image ID match:
3. Create Lambda function and attach your ECR Image. Make sure the name and image ID match:

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


6. Test your Lambda function with a JSON payload:
4. Test your Lambda function with a JSON payload:

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

Expand Down
68 changes: 56 additions & 12 deletions lambda/deploy.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,14 +1,58 @@
URL_STRING=".dkr.ecr.us-east-1.amazonaws.com"
CONTAINER_STRING="chdb"
IMAGE_STRING="latest"
ECR_IMAGE_URI="$AWS_ACCOUNT_ID$URL_STRING/$CONTAINER_STRING:$IMAGE_STRING"
# get flag variables
profile="default"
region="us-east-1"
tag="latest"
no_push=false

while (( "$#" )); do
case "$1" in
--tag)
tag="$2"
shift 2
;;
--region)
region="$2"
shift 2
;;
--profile)
profile="$2"
shift 2
;;
--no-push)
no_push=true
shift
;;
--)
shift
break
;;
-*|--*=)
echo "Error: Unsupported flag $1" >&2
exit 1
;;
*)
shift
;;
esac
done


# set variables
AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
ECR_IMAGE_URI="$AWS_ACCOUNT_ID.dkr.ecr.$region.amazonaws.com"
IMAGE_NAME="$ECR_IMAGE_URI/chdb:$tag"

# log in to ECR
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin "$AWS_ACCOUNT_ID$URL_STRING"
# remove previous images to save space
docker rmi "$AWS_ACCOUNT_ID$URL_STRING/$CONTAINER_STRING"
docker rmi "$CONTAINER_STRING"
aws ecr get-login-password --region $region --profile $profile | \
docker login --username AWS --password-stdin $ECR_IMAGE_URI

# remove existing image
docker rmi $IMAGE_NAME 2>/dev/null || true

# build image
docker build --tag "$CONTAINER_STRING" .
# tag and push to AWS ECR
docker tag $CONTAINER_STRING:latest "$ECR_IMAGE_URI"
docker push "$ECR_IMAGE_URI"
docker build -t $IMAGE_NAME .

if [ "$no_push" = false ]; then
# push to ECR
docker push $IMAGE_NAME
fi
16 changes: 10 additions & 6 deletions lambda/lambda.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import json

import chdb

def lambda_handler(event, context):
query = event['query'] or "SELECT version()"
format = event['default_format'] or "JSONCompact"
res = chdb.query(query, format)
out = json.loads(res.data())

def handler(event, context):
if "requestContext" in event:
event = json.loads(event["body"])
query = event["query"] if "query" in event else "SELECT version()"
format = event["default_format"] if "default_format" in event else "JSONCompact"

res = chdb.query(query, format).data()
return {
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": json.dumps(out)
"body": str(res) if not isinstance(res, (dict, list)) else json.dumps(res),
}
1 change: 1 addition & 0 deletions lambda/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
boto3
chdb

0 comments on commit 95b8b4a

Please sign in to comment.