Skip to content

Create version that can be directly uploaded to AWS as a zip file #4

@sgbaird

Description

@sgbaird

I.e., without chalice dependency. Just the normal process.

https://github.com/ACC-HelloWorld/5-data-logging#aws-lambda-function

AccelerationConsortium/ac-dev-lab#231
AccelerationConsortium/ac-dev-lab#271

https://docs.aws.amazon.com/lambda/latest/dg/python-package.html

copying some code from the notebook mentioned in https://github.com/ACC-HelloWorld/5-data-logging#aws-lambda-function

# -*- coding: utf-8 -*-
"""1.5.2-mongodb-aws-lambda-prep.ipynb

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/github/AccelerationConsortium/ac-microcourses/blob/main/docs/courses/hello-world/1.5.2-mongodb-aws-lambda-prep.ipynb

# Preparing the Python code and dependencies for AWS Lambda

[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/AccelerationConsortium/ac-microcourses/blob/main/docs/courses/hello-world/1.5.2-mongodb-aws-lambda-prep.ipynb)

For simplicity, we'll package `pymongo`, our Python dependency, along with our `lambda_function.py` script per the [guidelines in the AWS docs](https://docs.aws.amazon.com/lambda/latest/dg/python-package.html). See also the [How to Use PyMongo to Connect MongoDB Atlas with AWS Lambda](https://www.mongodb.com/developer/products/atlas/awslambda-pymongo/) MongoDB tutorial and the [Manage Connections with AWS Lambda](https://www.mongodb.com/docs/atlas/manage-connections-aws-lambda/) MongoDB documentation page. This notebook is intended to be run on Google Colab.
"""

# Commented out IPython magic to ensure Python compatibility.
# %mkdir dependencies

# Commented out IPython magic to ensure Python compatibility.
# %pip install --target ./dependencies pymongo

# Commented out IPython magic to ensure Python compatibility.
# %%writefile lambda_function.py
# import os
# import json
# from pymongo import MongoClient
# 
# atlas_uri = os.environ['ATLAS_URI']
# client = MongoClient(atlas_uri)
# 
# def lambda_handler(event, context):
#     try:
#         payload = json.loads(event["body"])
#         document = payload["document"]
#         db_name = payload["database"]
#         collection_name = payload["collection"]
#         db = client[db_name]
#         collection = db[collection_name]
#         result = collection.insert_one(document)
#         return {
#             "statusCode": 200,
#             "body": json.dumps({"message": "Document inserted successfully"}),
#         }
#     except Exception as e:
#         return {
#             "statusCode": 500,
#             "body": json.dumps({"error": str(e)}),
#         }
#

# Commented out IPython magic to ensure Python compatibility.
# %cd dependencies
!zip -r ../deployment.zip *
# %cd ..
!zip deployment.zip lambda_function.py

"""Download the file:"""

import sys

if 'google.colab' in sys.modules:
    from google.colab import files
    files.download('deployment.zip')

"""NOTE: If the above cell doesn't work in Google Colab, you can download `deployment.zip` using the left navigation bar by **hovering over the file**, clicking the three vertical dots, and clicking "download", as shown below. If you don't see the file, you may need to click the refresh icon near the top of the sidebar. You will then upload this `.zip` file to AWS Lambda.
"""

In our case, we don't need to package pymongo. However, we do need the dependencies in https://github.com/AccelerationConsortium/streamingLambda/blob/main/requirements.txt, except for chalice. Write a GitHub action that automatically creates a new deployment.zip for new release versions.

Keep in mind the dependency file within chalicelib/ folder.

The main overall goal is for people outside the AC organization to be able to easily and quickly create a lambda function for their own organization/research group/etc.

See also https://ac-training-lab.readthedocs.io/en/latest/devices/picam.html, which is sort of the entry point. The whole goal is to get a new, fully functioning LAMBDA_FUNCTION_URL (which also means dealing with the YouTube token.pickle and S3)

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions