This directory contains code and instructions for creating ObsPy-based Lambda functions compatible with Amazon API Gateway that use the SCEDC Public Dataset.
Amazon API Gateway places a RESTful interface in front of a Lambda function. This means you can connect to a web URL to call your Lambda function, using tools such as curl or any library that can send HTTP requests. Using API Gateway also allows your Lambda function to stream data, but note that streaming data to the Internet incurs data transfer charges.
timewindow - Given a time window and NSCL, returns a windowed waveform from the
Public Dataset in SAC or miniSEED format. The resulting waveform can be streamed
to the user or written to S3.
filter - Given an event ID, a list of NSCLs, and an output format, this function
applies a lowpass filter to the triggered waveforms in the Public Dataset that match
the event ID and each NSCL. The results are written to S3 in the output format.
decimate - Given the key of a continuous waveform from the Public Dataset and a
decimation factor, this function decimates the waveform and saves the result to an
S3 bucket. This function is the API Gateway-compatible version of the older example in
pds-lambda-example.
In your AWS account:
-
Create an IAM role that has full AmazonS3FullAccess, AWSLambda_FullAccess, and AWSLambdaBasicExecutionRole permissions at https://console.aws.amazon.com/iam/home#/roles.
-
Create an S3 bucket for holding the zip file your Lambda function will use.
-
Create an S3 bucket for data returned by the Lambda function/API. Some functions also have the option of streaming data directly to your computer.
-
On your computer, your access keys in
.aws/credentialsshould have full AmazonS3FullAccess and AWSLambda_FullAccess permissions. Go to https://console.aws.amazon.com/iam/home#/users to create or modify access keys.
- Clone the git repo and navigate to this directory.
git clone https://github.com/SCEDC/cloud.git
cd cloud/pds-lambda-apis
- Create a Docker image named
lambda-envusing the Dockerfile:
docker build -t lambda-env .
This image runs Amazon Linux 2018.03 and has Python 3.7 and ObsPy installed.
- This directory contains three sample functions. Copy the file
process.pyfrom a function's directory into this directory. For example, to build thetimewindowfunction:
cp timewindow/process.py .
-
Copy
settings_example.pytosettings.py, and modify the fields to fit your account. -
Create the zip file that will be used to create the Lambda function:
docker run -v $(pwd):/outputs lambda-env /bin/bash /outputs/build.sh
This command starts a Docker container running the lambda-env image, mounts the current directory as /outputs in
the container, and runs the script build.sh. This script installs ObsPy 1.2.2, NumPy 1.19.5, and SciPy 1.6.0 in
a virtual environment and packages the virtual environment, necessary libraries, and process.py in a zip file named
venv.zip.
- Run
create_lambda_function.py, which uploadsvenv.zipto the S3 bucket specified inLAMBDA_BUCKETinsettings.pyand creates the Lambda function.
python3 create_lambda_function.py
-
In your AWS web console, click on "Services" and select API Gateway.
-
Click "Create API."
-
Choose HTTP API and click the "Build" button.
-
Click "Add Integration" and select "Lambda" in the dropdown. For AWS Region, select "us-west-2. Enter the name of your Lambda function.
-
Enter a name for your API in the "API name" box. Then click "Next."
-
On the "Configure routes" page, you can leave "ANY" as the method or choose POST. The example functions in this directory only support the POST method.
-
Make up a name for the resource path that starts with "/". This will be the path at the end of the URL for your API. For example, your API's URL could be
https://api-url.amazon.com/myresourcepath. -
The name of the Lambda function should be the integration target. Click "Nexto" and click "Next" on the "Configure stages" page.
-
Check over the next page and click "Create."
API Gateway generates an address for the API. Append the resource path to the address, and use this URL to access your function.
You can use tools like curl or libraries like Python's requests library.
-
Write a handler for your Lambda function in
process.py, or copy an existingprocess.pyfrom one of the example directories. -
Replace
process.pyin the zip file:
zip -r venv.zip process.py
-
Update
settings.pyor copy an existingsettings.pyto this directory. -
Then run:
python3 create_lambda_function.py
- Create an API using API Gateway as in the "Creating an API" section.