initial version

This commit is contained in:
Matthias Kretschmann 2018-12-22 20:07:09 +01:00
commit 41e8d30911
Signed by: m
GPG Key ID: 606EEEF3C479A91F
7 changed files with 171 additions and 0 deletions

7
.env.sample Normal file
View File

@ -0,0 +1,7 @@
FORWARD_EMAIL=""
EMAIL_BUCKET=""
EMAIL_BUCKET_PREFIX=""
# needed only for deployment
AWS_REGION=""
LAMBDA_FUNCTION_NAME=""

7
.gitignore vendored Normal file
View File

@ -0,0 +1,7 @@
node_modules
package-lock.json
dist/
dist.zip
archive.zip
.env
.env.production

5
.prettierrc Normal file
View File

@ -0,0 +1,5 @@
{
"semi": false,
"singleQuote": true,
"trailingComma": "none"
}

19
README.md Normal file
View File

@ -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).

90
gulpfile.js Normal file
View File

@ -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)

20
index.js Normal file
View File

@ -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)
}

23
package.json Normal file
View File

@ -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 <m@kretschmann.io> (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"
}
}