Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Enable serverless deployment
  • Loading branch information
bertpareyn committed May 30, 2019
commit 367bcc2efd649624212f59b0d21f55631ecd5522
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export MONGO_URL="mongodb://<user>:<password>@<host>:<port>/<database>"
export S3_PATH="<s3bucket>/<folder>/...etc"
8 changes: 6 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
.idea
node_modules
# package directories
node_modules
jspm_packages

# Serverless directories
.serverless
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
This is simple aws lambda function that uses `mongodump` to backup mongodb database.
This is a simple AWS Lambda function that uses `mongodump` to backup a MongoDB database to S3.
It uses [Serverless](https://serverless.com/) to zip up the directory and deploy to AWS.

It zips the file and uploads it to an S3 bucket.
## Deployment instructions ##

Use the following environment variables in your Lambda setup:
Create a `.env` file in the root directory and add the following environment variables:
```
export MONGO_URL = mongodb://<user>:<password>@<host>:<port>/<database>
export S3_PATH = <s3bucket>/<folder>/...etc
```

Source the environment file so Serverless can access the values:
```
MONGO_URL = mongodb://<user>:<password>@<host>:<port>/<database>
S3_PATH = <s3bucket>/<folder>/...etc
. ./.env
```

Deploy the lambda function with Serverless:
```
serverless deploy --aws-profile profilename
```
Your function is now deployed and will run every 2 hours. Change the execution frequency in `serverless.yml`.
_____________________________

## Instructions ##


- Clone this repository.
- Run - npm install.
- Zip all the files.
- Upload the zip to a new aws lambda (upload to s3 and then to the lambda, the file is to big to upload directly)
- Add S3 Policy to the lambda role to give the lambda permission to write to s3.



#### Region ####
This lambda function compiled in EU (Ireland) AMI .
## Lambda configuration ##
The `serverless.yml` file contains a set of default configuration for the lambda. You can change these settings to fit your needs.



25 changes: 25 additions & 0 deletions serverless.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
service: lambda-mongodb-s3-backup

provider:
name: aws
runtime: nodejs10.x
region: eu-west-1
timeout: 30
memorySize: 512
stage: dev

environment:
MONGO_URL: ${env:MONGO_URL}
S3_PATH: ${env:S3_PATH}

iamRoleStatements:
- Effect: "Allow"
Action:
- "s3:*Object"
Resource:
- "arn:aws:s3:::${env:S3_PATH}/*"
functions:
createBackup:
handler: index.handler
events:
- schedule: cron(* 0/2 * * ? *) # Runs a backup every 2 hours. https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html#CronExpressions
145 changes: 145 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


archiver@^0.11.0:
version "0.11.0"
resolved "https://registry.yarnpkg.com/archiver/-/archiver-0.11.0.tgz#98177da7a6c0192b7f2798f30cd6eab8abd76690"
dependencies:
async "~0.9.0"
buffer-crc32 "~0.2.1"
glob "~3.2.6"
lazystream "~0.1.0"
lodash "~2.4.1"
readable-stream "~1.0.26"
tar-stream "~0.4.0"
zip-stream "~0.4.0"

async@~0.9.0:
version "0.9.2"
resolved "https://registry.yarnpkg.com/async/-/async-0.9.2.tgz#aea74d5e61c1f899613bf64bda66d4c78f2fd17d"

bl@^0.9.0:
version "0.9.5"
resolved "https://registry.yarnpkg.com/bl/-/bl-0.9.5.tgz#c06b797af085ea00bc527afc8efcf11de2232054"
dependencies:
readable-stream "~1.0.26"

buffer-crc32@~0.2.1:
version "0.2.13"
resolved "https://registry.yarnpkg.com/buffer-crc32/-/buffer-crc32-0.2.13.tgz#0d333e3f00eac50aa1454abd30ef8c2a5d9a7242"

compress-commons@~0.1.0:
version "0.1.6"
resolved "https://registry.yarnpkg.com/compress-commons/-/compress-commons-0.1.6.tgz#0c740870fde58cba516f0ac0c822e33a0b85dfa3"
dependencies:
buffer-crc32 "~0.2.1"
crc32-stream "~0.3.1"
readable-stream "~1.0.26"

core-util-is@~1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"

crc32-stream@~0.3.1:
version "0.3.4"
resolved "https://registry.yarnpkg.com/crc32-stream/-/crc32-stream-0.3.4.tgz#73bc25b45fac1db6632231a7bfce8927e9f06552"
dependencies:
buffer-crc32 "~0.2.1"
readable-stream "~1.0.24"

end-of-stream@^1.0.0:
version "1.4.1"
resolved "https://registry.yarnpkg.com/end-of-stream/-/end-of-stream-1.4.1.tgz#ed29634d19baba463b6ce6b80a37213eab71ec43"
dependencies:
once "^1.4.0"

glob@~3.2.6:
version "3.2.11"
resolved "https://registry.yarnpkg.com/glob/-/glob-3.2.11.tgz#4a973f635b9190f715d10987d5c00fd2815ebe3d"
dependencies:
inherits "2"
minimatch "0.3"

inherits@2, inherits@~2.0.1:
version "2.0.3"
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de"

isarray@0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/isarray/-/isarray-0.0.1.tgz#8a18acfca9a8f4177e09abfc6038939b05d1eedf"

lazystream@~0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/lazystream/-/lazystream-0.1.0.tgz#1b25d63c772a4c20f0a5ed0a9d77f484b6e16920"
dependencies:
readable-stream "~1.0.2"

lodash@~2.4.1:
version "2.4.2"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-2.4.2.tgz#fadd834b9683073da179b3eae6d9c0d15053f73e"

lru-cache@2:
version "2.7.3"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-2.7.3.tgz#6d4524e8b955f95d4f5b58851ce21dd72fb4e952"

minimatch@0.3:
version "0.3.0"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-0.3.0.tgz#275d8edaac4f1bb3326472089e7949c8394699dd"
dependencies:
lru-cache "2"
sigmund "~1.0.0"

once@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/once/-/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1"
dependencies:
wrappy "1"

readable-stream@^1.0.27-1, readable-stream@~1.0.2, readable-stream@~1.0.24, readable-stream@~1.0.26:
version "1.0.34"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-1.0.34.tgz#125820e34bc842d2f2aaafafe4c2916ee32c157c"
dependencies:
core-util-is "~1.0.0"
inherits "~2.0.1"
isarray "0.0.1"
string_decoder "~0.10.x"

sigmund@~1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/sigmund/-/sigmund-1.0.1.tgz#3ff21f198cad2175f9f3b781853fd94d0d19b590"

string_decoder@~0.10.x:
version "0.10.31"
resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-0.10.31.tgz#62e203bc41766c6c28c9fc84301dab1c5310fa94"

tar-stream@~0.4.0:
version "0.4.7"
resolved "https://registry.yarnpkg.com/tar-stream/-/tar-stream-0.4.7.tgz#1f1d2ce9ebc7b42765243ca0e8f1b7bfda0aadcd"
dependencies:
bl "^0.9.0"
end-of-stream "^1.0.0"
readable-stream "^1.0.27-1"
xtend "^4.0.0"

wrappy@1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"

xtend@^4.0.0:
version "4.0.1"
resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.1.tgz#a5c6d532be656e23db820efb943a1f04998d63af"

zip-folder@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/zip-folder/-/zip-folder-1.0.0.tgz#70a7744fd1789a2feb41ad3419b32e9fd87957b2"
dependencies:
archiver "^0.11.0"

zip-stream@~0.4.0:
version "0.4.1"
resolved "https://registry.yarnpkg.com/zip-stream/-/zip-stream-0.4.1.tgz#4ea795a8ce19e9fab49a31d1d0877214159f03a3"
dependencies:
compress-commons "~0.1.0"
lodash "~2.4.1"
readable-stream "~1.0.26"