-
Notifications
You must be signed in to change notification settings - Fork 6
adding a lambda handler and client for database access #206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
1faacd6
feat: add AWS Lambda containerized deployment support
aaarendt 65d6186
fix: update Lambda test invocation payload and add CLI binary format …
aaarendt 0c15665
fix: enhance Lambda test script with improved response handling and l…
aaarendt 8daf438
fix: add missing dependencies for shapely and pandas in Lambda requir…
aaarendt 78b4a1d
fix: add pytest markers for integration and Lambda-specific tests
aaarendt 79329be
fix: enhance Lambda integration by improving dependency imports and a…
aaarendt c92cd84
feat: add SnowEx Lambda API client and handler for serverless databas…
aaarendt 8b29864
fix: replace db_session with db_session_with_credentials so that DB n…
aaarendt b66b09d
additional serialization functionality
aaarendt da9eec3
add from_area method
aaarendt e813a35
Merge branch 'master' into lambda
aaarendt 8617aa5
feat: enhance from_area method to support server-side spatial filteri…
aaarendt 0a65a6b
refactor: streamline handle_event_with_secret and update from_area me…
aaarendt c4cf19c
refactor: enhance spatial query handling by eliminating geoalchemy2 d…
aaarendt 116d992
feat: add conftest.py for deployment/integration tests with Lambda su…
aaarendt 50ba6ba
feat: add end-to-end tests for SnowExLambdaClient functionality with …
aaarendt 7ba4417
feat: add comprehensive tests for Lambda handler functionality with l…
aaarendt ef13420
feat: add script to update Lambda function configuration for timeout …
aaarendt c74fdd3
fix: update from_area method to include CRS parameter in TestLayerMea…
aaarendt ec9c98e
feat: enhance timeout handling in Lambda client tests and update requ…
aaarendt 6224b48
fix: include CRS parameter in from_area method for PointMeasurementFi…
aaarendt bd7d3ae
fix: update to db_session_with_credentials to align with our newer cr…
aaarendt 1dde1ba
fix: refactor local_credentials fixture to align with our new approac…
aaarendt 7a69c7d
fix: enhance geometry handling and optimize query performance in Base…
aaarendt dcd8f5d
improve code readability by formatting comments and docstrings in api.py
aaarendt c1415c0
fix: starting on modifying gallery examples to work with the new Lamb…
aaarendt 15d9f23
fix: add boto3 to dependencies
aaarendt da65214
small blunder
aaarendt 885eae8
fix: update test cases to include layer_data fixture for area tests
aaarendt d9857bd
fix: update geometry handling to maintain provided CRS for spatial qu…
aaarendt 9b99bb6
fix: update pytest command to exclude integration tests
aaarendt 3551413
fix: update pytest command to exclude integration tests
aaarendt 98ce1a9
refactor: simplify conftest.py by removing unused DB session fixtures
aaarendt c994d04
fix: enhance geometry handling to detect and use database SRID for sp…
aaarendt 98b83ac
fix: add fixtures for test point and layer data to enhance spatial qu…
aaarendt f3f219c
Initial example of the functionality of the lambda client and new db …
aaarendt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| # Ignore unnecessary files for Docker build | ||
| .git | ||
| .gitignore | ||
| README.rst | ||
| docs/ | ||
| notebooks/ | ||
| tests/ | ||
| .pytest_cache/ | ||
| __pycache__/ | ||
| *.pyc | ||
| *.pyo | ||
| *.pyd | ||
| .Python | ||
| *.egg-info/ | ||
| .coverage | ||
| .tox | ||
| venv/ | ||
| env/ | ||
| ENV/ | ||
| .venv/ | ||
| .env | ||
| .DS_Store | ||
| Thumbs.db | ||
| *.log | ||
| .mypy_cache/ | ||
| .idea/ | ||
| .vscode/ | ||
| *.swp | ||
| *.swo | ||
| *~ | ||
|
|
||
| # Build artifacts | ||
| build/ | ||
| dist/ | ||
| *.egg-info/ | ||
|
|
||
| # Docker files (don't include docker-compose in container) | ||
| docker-compose.yml | ||
| Dockerfile | ||
| .dockerignore | ||
|
|
||
| # Development and documentation | ||
| CHANGELOG.rst | ||
| CODE_OF_CONDUCT.md | ||
| CONTRIBUTING.rst | ||
| LICENSE | ||
| Makefile | ||
| MANIFEST.in | ||
| deploy.sh |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # Deployment | ||
|
|
||
| This directory contains all the infrastructure and deployment configurations for | ||
| running snowexsql on AWS Lambda. | ||
|
|
||
| ## Structure | ||
|
|
||
| - **`docker/`** - Docker container configuration for Lambda | ||
| - `Dockerfile` - Lambda-compatible container definition | ||
| - `.dockerignore` - Optimization for container builds | ||
| - `requirements-lambda.txt` - Lightweight dependencies | ||
|
|
||
| - **`aws/`** - AWS IAM policies and configurations | ||
| - `ecr_policy.json` - ECR repository permissions for Lambda | ||
| - `secrets_policy.json` - Secrets Manager access policy | ||
|
|
||
| - **`scripts/`** - Deployment automation scripts | ||
| - `deploy.sh` - Main deployment script (container-based) | ||
| - `test_lambda.sh` - Automated testing script | ||
|
|
||
| ## Quick Start | ||
|
|
||
| 1. Run `scripts/deploy.sh` to deploy the Lambda function | ||
| 2. Test with `scripts/test_lambda.sh` | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - AWS CLI configured | ||
| - Docker installed and running | ||
| - Existing ECR repository named `snowexsql` | ||
| - Lambda function with container image support |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| { | ||
| "Version": "2008-10-17", | ||
| "Statement": [ | ||
| { | ||
| "Sid": "LambdaECRImageRetrievalPolicy", | ||
| "Effect": "Allow", | ||
| "Principal": { | ||
| "Service": "lambda.amazonaws.com" | ||
| }, | ||
| "Action": [ | ||
| "ecr:BatchGetImage", | ||
| "ecr:GetDownloadUrlForLayer" | ||
| ], | ||
| "Condition": { | ||
| "StringLike": { | ||
| "aws:sourceArn": "arn:aws:lambda:us-west-2:390402539674:function:*" | ||
| } | ||
| } | ||
| }, | ||
| { | ||
| "Sid": "LambdaECRImageCrossAccount", | ||
| "Effect": "Allow", | ||
| "Principal": { | ||
| "AWS": "arn:aws:iam::390402539674:root" | ||
| }, | ||
| "Action": [ | ||
| "ecr:BatchGetImage", | ||
| "ecr:GetDownloadUrlForLayer", | ||
| "ecr:GetAuthorizationToken" | ||
| ] | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "Version": "2012-10-17", | ||
| "Statement": [ | ||
| { | ||
| "Effect": "Allow", | ||
| "Action": [ | ||
| "secretsmanager:GetSecretValue" | ||
| ], | ||
| "Resource": "arn:aws:secretsmanager:us-west-2:390402539674:secret:rds/snowexsql/credentials-*" | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| # Ignore unnecessary files for Docker build | ||
| .git | ||
| .gitignore | ||
| README.rst | ||
| docs/ | ||
| notebooks/ | ||
| tests/ | ||
| .pytest_cache/ | ||
| __pycache__/ | ||
| *.pyc | ||
| *.pyo | ||
| *.pyd | ||
| .Python | ||
| *.egg-info/ | ||
| .coverage | ||
| .tox | ||
| venv/ | ||
| env/ | ||
| ENV/ | ||
| .venv/ | ||
| .env | ||
| .DS_Store | ||
| Thumbs.db | ||
| *.log | ||
| .mypy_cache/ | ||
| .idea/ | ||
| .vscode/ | ||
| *.swp | ||
| *.swo | ||
| *~ | ||
|
|
||
| # Build artifacts | ||
| build/ | ||
| dist/ | ||
| *.egg-info/ | ||
|
|
||
| # Docker files (don't include docker-compose in container) | ||
| docker-compose.yml | ||
| Dockerfile | ||
| .dockerignore | ||
|
|
||
| # Development and documentation | ||
| CHANGELOG.rst | ||
| CODE_OF_CONDUCT.md | ||
| CONTRIBUTING.rst | ||
| LICENSE | ||
| Makefile | ||
| MANIFEST.in | ||
| deploy.sh |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| FROM public.ecr.aws/lambda/python:3.12 | ||
|
|
||
| # Copy requirements and install dependencies | ||
| COPY deployment/docker/requirements-lambda.txt ${LAMBDA_TASK_ROOT}/ | ||
| RUN pip install --no-cache-dir -r requirements-lambda.txt | ||
|
|
||
| # Copy the snowexsql package | ||
| COPY snowexsql/ ${LAMBDA_TASK_ROOT}/snowexsql/ | ||
|
|
||
| # Set the CMD to your handler | ||
| CMD ["snowexsql.lambda_handler.lambda_handler"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # Lambda-optimized requirements without heavy dependencies | ||
| utm>=0.5.0,<1.0 | ||
| geoalchemy2>=0.6,<1.0 | ||
| shapely>=2.0.0,<3.0 | ||
| pandas>=1.5.0,<3.0 | ||
| psycopg2-binary>=2.9.0,<2.10.0 | ||
| SQLAlchemy>=2.0.0 | ||
| boto3>=1.26.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| #!/bin/bash | ||
|
|
||
| # AWS Lambda Deployment Script for SnowEx SQL | ||
| # This script builds and deploys the Docker container to AWS Lambda | ||
|
|
||
| set -e | ||
|
|
||
| # Configuration | ||
| AWS_ACCOUNT_ID="390402539674" | ||
| AWS_REGION="us-west-2" | ||
| ECR_REPOSITORY="snowexsql" | ||
| LAMBDA_FUNCTION_NAME="lambda-snowex-sql" | ||
| IMAGE_TAG="$(git rev-parse --short HEAD)" | ||
|
|
||
| # Colors for output | ||
| RED='\033[0;31m' | ||
| GREEN='\033[0;32m' | ||
| YELLOW='\033[1;33m' | ||
| NC='\033[0m' # No Color | ||
|
|
||
| echo -e "${GREEN}Starting SnowEx SQL Lambda deployment...${NC}" | ||
|
|
||
| # Check if AWS CLI is installed and configured | ||
| if ! command -v aws &> /dev/null; then | ||
| echo -e "${RED}Error: AWS CLI is not installed${NC}" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Check if Docker is running | ||
| if ! docker info &> /dev/null; then | ||
| echo -e "${RED}Error: Docker is not running${NC}" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Build the Docker image | ||
| echo -e "${YELLOW}Building Docker image...${NC}" | ||
| DOCKER_BUILDKIT=0 docker build -f ../docker/Dockerfile -t ${ECR_REPOSITORY}:${IMAGE_TAG} ../.. | ||
|
|
||
| # Get ECR login token | ||
| echo -e "${YELLOW}Logging into ECR...${NC}" | ||
| aws ecr get-login-password --region ${AWS_REGION} | docker login --username AWS --password-stdin ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com | ||
|
|
||
| # Tag the image for ECR | ||
| ECR_URI="${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${ECR_REPOSITORY}:${IMAGE_TAG}" | ||
| echo -e "${YELLOW}Tagging image for ECR: ${ECR_URI}${NC}" | ||
| docker tag ${ECR_REPOSITORY}:${IMAGE_TAG} ${ECR_URI} | ||
|
|
||
| # Push to ECR | ||
| echo -e "${YELLOW}Pushing image to ECR...${NC}" | ||
| docker push ${ECR_URI} | ||
|
|
||
| # Update Lambda function | ||
| echo -e "${YELLOW}Updating Lambda function...${NC}" | ||
| aws lambda update-function-code \ | ||
| --region ${AWS_REGION} \ | ||
| --function-name ${LAMBDA_FUNCTION_NAME} \ | ||
| --image-uri ${ECR_URI} | ||
|
|
||
| # Wait for the update to complete | ||
| echo -e "${YELLOW}Waiting for Lambda function update to complete...${NC}" | ||
| aws lambda wait function-updated \ | ||
| --region ${AWS_REGION} \ | ||
| --function-name ${LAMBDA_FUNCTION_NAME} | ||
|
|
||
| echo -e "${GREEN}Deployment completed successfully!${NC}" | ||
| echo -e "${GREEN}Lambda function '${LAMBDA_FUNCTION_NAME}' has been updated with the new image.${NC}" | ||
|
|
||
| # Optional: Test the function | ||
| read -p "Would you like to test the Lambda function? (y/n): " -n 1 -r | ||
| echo | ||
| if [[ $REPLY =~ ^[Yy]$ ]]; then | ||
| echo -e "${YELLOW}Testing Lambda function...${NC}" | ||
| aws lambda invoke \ | ||
| --region ${AWS_REGION} \ | ||
| --function-name ${LAMBDA_FUNCTION_NAME} \ | ||
| --cli-binary-format raw-in-base64-out \ | ||
| --payload '{"action":"test_connection"}' \ | ||
| response.json | ||
|
|
||
| echo -e "${GREEN}Test response:${NC}" | ||
| cat response.json | ||
| echo | ||
| rm -f response.json | ||
| fi | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.