From 41e8d30911b55bc1869678bfa05d6010d3c33786 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Sat, 22 Dec 2018 20:07:09 +0100 Subject: [PATCH] initial version --- .env.sample | 7 ++++ .gitignore | 7 ++++ .prettierrc | 5 +++ README.md | 19 +++++++++++ gulpfile.js | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 20 ++++++++++++ package.json | 23 ++++++++++++++ 7 files changed, 171 insertions(+) create mode 100644 .env.sample create mode 100644 .gitignore create mode 100644 .prettierrc create mode 100644 README.md create mode 100644 gulpfile.js create mode 100644 index.js create mode 100644 package.json diff --git a/.env.sample b/.env.sample new file mode 100644 index 0000000..78dbcfe --- /dev/null +++ b/.env.sample @@ -0,0 +1,7 @@ +FORWARD_EMAIL="" +EMAIL_BUCKET="" +EMAIL_BUCKET_PREFIX="" + +# needed only for deployment +AWS_REGION="" +LAMBDA_FUNCTION_NAME="" \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6a3874f --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +node_modules +package-lock.json +dist/ +dist.zip +archive.zip +.env +.env.production \ No newline at end of file diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..c522042 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,5 @@ +{ + "semi": false, + "singleQuote": true, + "trailingComma": "none" +} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..f03c714 --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +# aws-email-forwarder + +> Serverless email forwarding service with AWS Lambda and Simple Email Service, built and deployed with gulp. Extending from [arithmetric/aws-lambda-ses-forwarder](https://github.com/arithmetric/aws-lambda-ses-forwarder). + +## Usage + +```bash +npm i + +# fill in environment variables +cp .env.sample .env + +# package and deploy +gulp +``` + +## Setup + +Follow initial AWS setup instructions on [arithmetric/aws-lambda-ses-forwarder](https://github.com/arithmetric/aws-lambda-ses-forwarder). diff --git a/gulpfile.js b/gulpfile.js new file mode 100644 index 0000000..b28b1a7 --- /dev/null +++ b/gulpfile.js @@ -0,0 +1,90 @@ +const fs = require('fs') +const gulp = require('gulp') +const del = require('del') +const install = require('gulp-install') +const zip = require('gulp-zip') +const AWS = require('aws-sdk') + +require('dotenv').config() + +const clean = del.bind(null, ['dist/**/*', 'dist.zip']) + +const js = done => { + gulp.src('index.js').pipe(gulp.dest('dist')) + done() +} + +const npm = done => { + gulp + .src('package.json') + .pipe(gulp.dest('dist')) + .pipe(install({ production: true }, () => done())) +} + +const env = done => { + gulp.src('.env').pipe(gulp.dest('dist')) + done() +} + +const package = done => { + gulp + .src('dist/**/*') + .pipe(zip('dist.zip')) + .pipe(gulp.dest('.')) + done() +} + +const upload = done => { + AWS.config.region = process.env.AWS_REGION + const lambda = new AWS.Lambda() + const functionName = process.env.LAMBDA_FUNCTION_NAME + + lambda.getFunction({ FunctionName: functionName }, (err, data) => { + if (err) { + if (err.statusCode === 404) { + let warning = 'Unable to find lambda function ' + deploy_function + '. ' + warning += 'Verify the lambda function name and AWS region are correct.' + console.log(warning) + } else { + let warning = 'AWS API request failed. ' + warning += 'Check your AWS credentials and permissions.' + console.log(warning) + } + } + + const paramsCode = { + FunctionName: functionName + } + + const paramsConfig = { + FunctionName: functionName, + Environment: { + Variables: { + FORWARD_EMAIL: process.env.FORWARD_EMAIL, + EMAIL_BUCKET: process.env.EMAIL_BUCKET, + EMAIL_BUCKET_PREFIX: process.env.EMAIL_BUCKET_PREFIX + } + } + } + + fs.readFile('dist.zip', (err, data) => { + if (err) console.log(err) + + paramsCode['ZipFile'] = data + + lambda.updateFunctionCode(paramsCode, (err, data) => { + if (err) console.log(err) + + lambda.updateFunctionConfiguration(paramsConfig, (err, data) => { + if (err) console.log(err) + + done() + }) + }) + }) + }) +} + +gulp.task('default', gulp.series(clean, js, env, npm, package)) + +gulp.task('deploy', upload) diff --git a/index.js b/index.js new file mode 100644 index 0000000..eebaa42 --- /dev/null +++ b/index.js @@ -0,0 +1,20 @@ +var LambdaForwarder = require('aws-lambda-ses-forwarder') + +require('dotenv').config() + +exports.handler = function(event, context, callback) { + const overrides = { + config: { + // fromEmail: "noreply@example.com", + subjectPrefix: '', + emailBucket: process.env.EMAIL_BUCKET, + emailKeyPrefix: process.env.EMAIL_BUCKET_PREFIX, + forwardMapping: { + desk: [process.env.FORWARD_EMAIL], + hi: [process.env.FORWARD_EMAIL] + } + } + } + + LambdaForwarder.handler(event, context, callback, overrides) +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..890820f --- /dev/null +++ b/package.json @@ -0,0 +1,23 @@ +{ + "name": "aws-email-forwarder", + "version": "1.0.0", + "description": "Serverless email forwarding service with AWS Lambda and Simple Email Service. Built and deployed with gulp.", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "Matthias Kretschmamnn (https://matthiaskretschmann.com/)", + "license": "MIT", + "dependencies": { + "aws-lambda-ses-forwarder": "^4.2.0", + "dotenv": "^6.2.0" + }, + "devDependencies": { + "aws-sdk": "^2.382.0", + "del": "^3.0.0", + "gulp": "^4.0.0", + "gulp-install": "^1.1.0", + "gulp-zip": "^4.2.0", + "prettier": "^1.15.3" + } +}