Code for automated extraction of hydrologically relevant information from images and videos using serverless computing.
This guide will walk you through the process of building a Lambda function with a container image using AWS CLI in the command line. Before proceeding, ensure that you have Docker installed on your local machine and AWS CLI configured with appropriate permissions.
- Docker installed on your local machine
- AWS CLI configured with appropriate permissions
- Dockerfile for building the container image
- Requirements file containing dependencies for your Lambda function
- Lambda function code
-
Write your Lambda function code: Write your Lambda function code and save it in a directory along with your Dockerfile and requirements file.
-
Navigate to the Lambda directory: Open your terminal or command prompt and navigate to the directory where your Lambda function code, Dockerfile, and requirements file are located.
git clone https://github.com/hydrocam/serverlesscompute cd serverlesscompute aws configure AWS Access Key ID [None]: YOUR_ACCESS_KEY_ID AWS Secret Access Key [None]: YOUR_SECRET_ACCESS_KEY Default region name [None]: YOUR_PREFERRED_REGION Default output format [None]: json
-
Write Dockerfile: Create a Dockerfile in the same directory as your Lambda function code. The Dockerfile specifies the environment and dependencies required for your Lambda function.
Example Dockerfile:
# Pull the base image with python 3.10 as a runtime for your Lambda FROM public.ecr.aws/lambda/python:3.10 # Copy the earlier created requirements.txt file to the container COPY requirements.txt ./ # Install the python requirements from requirements.txt RUN python3.10 -m pip install -r requirements.txt # Copy the earlier created app.py file to the container COPY app.py ./ # Set the CMD to your handler CMD ["app.lambda_handler"]
-
Build the Docker image: Use Docker to build the container image for your Lambda function.
docker build -t <image-name> .
-
Tag the Docker image: Tag the Docker image with the Amazon ECR repository URI where you'll push the image.
docker tag <image-name> <aws-account-id>.dkr.ecr.<region>.amazonaws.com/<repository-name>:lambda-function
-
Login to Amazon ECR: Use the AWS CLI to authenticate Docker to your Amazon ECR registry.
aws ecr get-login-password --region <region> | docker login --username AWS --password-stdin <aws-account-id>.dkr.ecr.<region>.amazonaws.com
-
Push the Docker image to Amazon ECR: Push the Docker image to your Amazon ECR repository.
docker push <aws-account-id>.dkr.ecr.<region>.amazonaws.com/<repository-name>:lambda-function
-
Create Lambda function: Use the AWS CLI to create the Lambda function with the container image.
aws lambda create-function \ --function-name <function-name> \ --package-type Image \ --code ImageUri=<aws-account-id>.dkr.ecr.<region>.amazonaws.com/<repository-name>:lambda-function \ --role <role-arn> \ --memory-size <memory-size> \ --timeout <timeout> \ --region <region>
-
Add an S3 Bucket Trigger to Lambda Function: To add an S3 bucket trigger so that your Lambda function is invoked when a new object is added to the bucket, use the aws s3api put-bucket-notification-configuration command
aws s3api put-bucket-notification-configuration \ --bucket <bucket-name> \ --notification-configuration '{ "LambdaFunctionConfigurations": [ { "LambdaFunctionArn": "arn:aws:lambda:<region>:<aws-account-id>:function:<function-name>", "Events": ["s3:ObjectCreated:*"] } ] }'
-
Add Permission for S3 to Invoke Lambda: Finally, you need to grant the S3 bucket permission to invoke your Lambda function. Use the aws lambda add-permission command:
aws lambda add-permission \ --function-name <function-name> \ --principal s3.amazonaws.com \ --statement-id <unique-statement-id> \ --action lambda:InvokeFunction \ --source-arn arn:aws:s3:::<bucket-name> \ --source-account <aws-account-id>
This research was supported by the Cooperative Institute for Research to Operations in Hydrology (CIROH) with joint funding under award NA22NWS4320003 from the NOAA Cooperative Institute Program and the U.S. Geological Survey. The statements, findings, conclusions, and recommendations are those of the author(s) and do not necessarily reflect the opinions of NOAA or USGS. Utah State University is a founding member of CIROH and receives funding under subaward from the University of Alabama. Additional funding and support have been provided by the Utah Water Research laboratory at Utah State University.