Skip to content

Instantly share code, notes, and snippets.

@luketn
Created September 14, 2022 07:14
Show Gist options
  • Save luketn/8f71ca5f44cdd4d532b1d3cbbe49cec1 to your computer and use it in GitHub Desktop.
Save luketn/8f71ca5f44cdd4d532b1d3cbbe49cec1 to your computer and use it in GitHub Desktop.
Serverless MongoDB Docker Lambda Import/Export Sync
#!/bin/sh
set -euo pipefail
# Initialization - load function handler
source $LAMBDA_TASK_ROOT/"$(echo $_HANDLER | cut -d. -f1).sh"
# Processing
while true
do
HEADERS="$(mktemp)"
# Get an event. The HTTP request will block until one is received
EVENT_DATA=$(curl -s --show-error -LD "$HEADERS" -X GET "http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/next")
# Extract request ID by scraping response headers received above
REQUEST_ID=$(grep -Fi Lambda-Runtime-Aws-Request-Id "$HEADERS" | tr -d '[:space:]' | cut -d: -f2)
# Run the handler function from the script
RESPONSE=$($(echo "$_HANDLER" | cut -d. -f2) "$EVENT_DATA")
# Send the response
curl -s --show-error -X POST "http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/$REQUEST_ID/response" -d "$RESPONSE"
done
# Ref: https://gallery.ecr.aws/lambda/provided
# + https://docs.aws.amazon.com/lambda/latest/dg/runtimes-images.html
# + https://aws.amazon.com/blogs/aws/new-for-aws-lambda-container-image-support/
FROM public.ecr.aws/lambda/provided:al2
COPY bootstrap.sh ${LAMBDA_RUNTIME_DIR}/bootstrap
COPY function.sh ${LAMBDA_TASK_ROOT}
# Ref https://www.mongodb.com/docs/v6.0/tutorial/install-mongodb-on-amazon/
SHELL ["/bin/bash", "-c"]
RUN echo $'[mongodb-org-6.0] \n\
name=MongoDB Repository \n\
baseurl=https://repo.mongodb.org/yum/amazon/2/mongodb-org/6.0/x86_64/ \n\
gpgcheck=1 \n\
enabled=1 \n\
gpgkey=https://www.mongodb.org/static/pgp/server-6.0.asc \n\
' > /etc/yum.repos.d/mongodb-org-6.0.repo
RUN yum install -y mongodb-mongosh mongodb-org-tools
CMD ["function.handler"]
#!/bin/sh
function export_import_collections() {
for collection_name in ${collection_names}
do
file="/tmp/${collection_name}.json"
echo "Exporting ${source_database_name}.${collection_name} to ${file}..."
mongoexport --db "${source_database_name}" --collection "${collection_name}" --uri "${source_connection_url}" --out $file
ls -lah "${file}"
echo "Importing from file ${file} to ${target_database_name}.${collection_name}..."
mongoimport --db "${target_database_name}" --collection "${collection_name}" --mode "upsert" --uri "${target_connection_url}" --file $file
echo "Finished collection ${collection_name}."
done
}
function handler () {
EVENT_DATA=$1
echo "$EVENT_DATA" 1>&2;
export_import_collections
RESPONSE="Success!"
echo $RESPONSE
}
# Ref: https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml
# + https://www.serverless.com/blog/container-support-for-lambda
service: serverless-docker
frameworkVersion: '3'
provider:
name: aws
stage: prod
region: eu-west-1
# architecture: arm64
ecr:
images:
some-image:
path: ./some-image
functions:
some-function:
events: # 5 minutes past midnight UTC every day
- schedule: cron(05 00 * * ? *)
environment:
source_connection_url: "mongodb://source"
source_database_name: "sourceDb"
collection_names: "col1 col2"
target_connection_url: "mongodb://target"
target_database_name: "targetDb"
image:
name: some-image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment