This tutorial will show you how to use Docker to create a Lambda function that uses ObsPy to decimate a data file from the the SCEDC Open Data Set and stores the resulting file an S3 bucket in your account.
You will be using a Cloud9 environment to create and upload a Docker image to ECR that contains processing code and ObsPy. You will then create a Lambda function using the Docker image
The code for the Lambda function is in app.py. You can modify it to create Lambda functions that do different types of processing on Public Data Set files.
-
Log in to the AWS console.
-
Navigate to the S3 service.
-
Click on "Create bucket."
-
Create a bucket with the name
lambda-output-followed by the last four digits of your account number. The rest of the settings don't need to be changed. Your Lambda function will write to this bucket. -
Navigate to the Cloud9 service. Make sure Oregon (us-west-2) is the region selected in the upper right corner.
-
Click "Create Environment."
-
Choose a name for your environment. Choose
t3.smallfor the instance type should be sufficient.t2.microdoes not work in the US-West-2 region. -
Click "Create."
-
On the next screen, wait for the environment to finish being created.
-
Click "Open" to open the Cloud9 IDE. In the IDE, the terminal is at the bottom of the workspace. The file explorer is on the right.
-
From your Event Engine login page, copy the "export" statements. Paste them into your terminal and hit ENTER. These statements set your AWS keys and session token as environment variables and make sure you have permission to run
awscommands from the terminal. -
In Cloud9, click the button in left sidebar above the "aws" logo. Click "Clone Repository." Paste https://github.com/SCEDC/tutorials.git into the "Repository URL" field and hit Enter.
-
Click on the folder icon in the sidebar to navigate the filesystem. You can open files in the IDE by double clicking them. The files for this tutorial are in
tutorials/pds-lambda-docker. -
In the terminal, navigate to tutorials/pds-lambda-docker.
cd tutorials/pds-lambda-docker -
Choose a name for your Docker image, like decimation-lambda, or my_docker_image, and run the command below to build the image using the provided Dockerfile.
docker build -t decimation-lambda .This Dockerfile builds a Docker image based on AWS's lambda/python:3.8 base image that has ObsPy installed. ObsPy and its dependencies are listed in
requirements.txt. The Dockerfile also tells Docker to make a copy ofapp.pyin the image and to run the functionapp.handleras the entrypoint for the Lambda function.
- Go to the ECR (Elastic Container Registry) service from your AWS console.
-
Click "Get Started."
-
Make sure Oregon is select as the region in the upper right corner of your window, and enter a name for your repository.
-
Scroll down and click "Create Repository."
-
Click the check box next to the repository name. Then click "View Push Commands."
-
Copy and paste the
aws ecrcommand into the Cloud9 terminal and run it to authenticate your Docker client. -
Copy and paste the
docker tagcommand into the terminal and run it to tag the image. -
Copy and paste the
docker pushcommand to upload the image. If you click on your Docker repository on the ECR page, you should see the latest image.
-
Go to the IAM service from your AWS console.
-
Click on "Roles" under "Access Management."
-
Click "Create Role."
-
Scroll down and select "Lambda" under "Common Use Cases." Click "Next."
-
Select AWSLambdaBasicExecution.
-
In the search box, type
AmazonS3Full. Select AmazonS3FullAccess, and click Next.
Do the same for CloudwatchLogsFullAccess. This may be required for debugging the Lambda function.
-
Go to the Lambda service from your AWS console. Make sure you are in the Oregon region.
-
Click "Create Function."
-
Select "Container image."
-
Enter a name for your function.
-
Click "Browse Images."
-
Click "Select Repository" and your ECR repository name.
-
Select your image, and click "Select image."
-
Open "Change default execution role" to open it. Select "Use an existing role."
-
Click the down arrow next to the "Existing role" field, and select the role that you created earlier.
-
Click "Create Function."
-
Scroll down and click "Configuration."
-
Click "Edit."
-
Change memory and ephemeral storage to 1024 MB. Change the timeout to 1 min.
-
Click "Save."
-
Click "Functions" to return to the list of functions, and click your function's name to load it.
-
Click on "Test."
-
Create a name for your test event, and paste the following code into the "Event JSON" box.
{ "s3_input_bucket": "scedc-pds", "s3_output_bucket": "YOUR_OUTPUT_BUCKET", "s3_key": "continuous_waveforms/2016/2016_123/CIWCS2_BHE___2016123.ms", "decimation_factor": 4 }Make sure to replace "YOUR_OUTPUT_BUCKET" with the name of the bucket your created earlier.
s3_input_bucketis the SCEDC Public Data Set.s3_keyis the key of the waveform from the Public Data Set that will be decimated.Click "Test" to run the Lambda function.
-
Hopefully, you will see the message "Execution result: succeeded." You can open the details to see that the function has returned an output_key in your bucket. This is the decimated file. From the terminal, you can run:
aws s3 ls s3://YOUROUTPUTBUCKET/
to see that it now contains decimated waveforms.
run_lambda.py contains code for invoking your lambda function using the Boto3 library. To run it in Cloud9, you will need to install boto3. You will also need to change the Lambda function name and S3 output bucket in the code.
pip install boto3
python run_lambda.py
In this section, you will create an API for your Lambda function that will let it accept requests over HTTP, and you will test it using curl. An API can be integrated into web services without using AWS libraries.
-
From the console, navigate to API Gateway.
-
Click "Create API."
-
Click "Build" under "HTTP API."
-
Click "Add Integration."
-
Click the down arrow in the text box, and select "Lambda."
-
Click the box with the magnifying glass, and your Lambda function should appear as an option. Select it so that it appears in the box.
-
Choose a name for the API, and click "Next."
-
Choose POST as the route.
-
Enter a resource path. This is what appear after the last '/' in your API's URL: https://your-api/resource_path. Click "Next."
-
Click "Next" on the next screen.
-
Click "Create."
-
On the next screen, you can find your API's URL listed under "Stages."
-
Open
request.jsonin the Cloud9 IDE, and change the value ofs3_output_bucketto your own bucket. To decimate different files, change the value ofs3_key. -
Call your API by running:
curl -X POST -H "Content-Type: application/json" -d @request.json https://your-api-url/decimateReplace
your-api-urlwith the actual URL. This comment sends the contents ofrequest.jsonto the API.
You can confirm that the decimated waveforms are in your S3 bucket by running aws s3 ls s3://YOURBUCKETNAME or navigating to the bucket in the AWS console. You can also download and plot them in this notebook.
Amazon Elastic Container Registry
Building Lambda Functions with Python













