Skip to content

Commit

Permalink
Merge pull request #69 from dag-hammarskjold-library/lambda
Browse files Browse the repository at this point in the history
Add Lambda function and GitHub Action to deploy
  • Loading branch information
aaronhelton authored Mar 7, 2024
2 parents 6c8dcb3 + 97bad4a commit dad8a8d
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 4 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
on:
push:
branches: [ master ]

jobs:
deploy Lambda function:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.10
uses: actions/setup-python@v3
with:
python-version: 3.10
- name: Setting up AWS Credentials
run: |
pip install awscli
aws configure set region us-east-1
aws configure set output json
aws configure set aws_access_key_id ${{secrets.AWS_ACCESS_KEY}}
aws configure set aws_secret_access_key ${{secrets.AWS_SECRET_KEY}}
- name: Create virtual environment
working-directory: ./aws-lambda
run: |
pip install virtualenv
python3 -m virtualenv venv
- name: Install requiremenents
working-directory: ./aws-lambda
run: |
. venv/bin/activate
python3 -m pip install --upgrade pip
pip install -r requirements.txt
deactivate
- name: Deploy
working-directory: ./aws-lambda
run: |
. venv/bin/activate
lambda deploy
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

venv/
env/
__pycache__/
*.py[co]
.vscode
_build/
/docs/_build/
.DS_Store
.vs
21 changes: 21 additions & 0 deletions aws-lambda/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

### About

This is a Lambda function for running dlx-dl-sync.

### Deployment

This describes how to deploy this function to AWS Lambda using the Python library [python-lambda](https://pypi.org/project/python-lambda/). The files in this directory are the standard files necessary for deploying Python code to Lambda.

AWS credentials must be configured in the deployment environment.

1. Navigate to this directory (`dlx-dl/aws-lambda`)
2. Make sure a virtual environment (venv) is activated.
3. ```pip install -r requirements.txt```
4. ```lambda deploy```

Note that once the Lambda function is deployed, it will not run until it is "invoked". [Several methods exist to invoke the function](https://docs.aws.amazon.com/lambda/latest/dg/lambda-invocation.html).

### Running the Lambda function locally (for development/testing purposes)

```lambda invoke -v --event-file=bib-event.json```
5 changes: 5 additions & 0 deletions aws-lambda/auth-event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"source": "dlx-dl-lambda",
"type": "auth",
"modified_within": 86400
}
5 changes: 5 additions & 0 deletions aws-lambda/bib-event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"source": "dlx-dl-lambda",
"type": "bib",
"modified_within": 86400
}
13 changes: 13 additions & 0 deletions aws-lambda/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
region: us-east-1

function_name: dlx-dl
handler: service.handler
description: DLX-DL Periodic Task Runner
runtime: python3.8
role: service-role/dlx-dl-role-ggxqj9bh
timeout: 900

# Build options
build:
source_directories: lib # a comma delimited list of directories in your project root that contains source to package.

2 changes: 2 additions & 0 deletions aws-lambda/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
../. # installs dlx-dl from the directory above
python-lambda @ git+https://github.com/nficano/python-lambda@2f9f17a5c5993e65ee2b61d06f29ed5a6689d337
13 changes: 13 additions & 0 deletions aws-lambda/service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import json
from dlx_dl.scripts import sync

def handler(event, context):
# event dict should contain the params expected by dlx.scripts.sync

print(f'running with args: {json.dumps(event)}')

try:
sync.run(**event)
except Exception as exc:
print('; '.join(str(exc).split('\n'))) # puts exception text on one line for CloudWatch logs

2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ cffi==1.15.1
charset-normalizer==2.1.1
click==8.1.3
cryptography==41.0.2
dlx @ git+https://github.com/dag-hammarskjold-library/dlx@9f8100087561dbade56a47043371d56b9124b1d6
dlx @ git+https://github.com/dag-hammarskjold-library/dlx@b4f554c454449f23850941af53debd605fc033b6
idna==3.4
Jinja2==3.1.2
jmespath==1.0.1
Expand Down
4 changes: 1 addition & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
license = 'http://www.opensource.org/licenses/bsd-license.php',
packages = find_packages(exclude=['test']),
test_suite = 'tests',
install_requires = [
'dlx @ git+https://github.com/dag-hammarskjold-library/dlx#egg=dlx'
],
install_requires = requirements,
description = 'Export data fom DLX to DL',
long_description = long_description,
long_description_content_type = "text/markdown",
Expand Down

0 comments on commit dad8a8d

Please sign in to comment.