forked from super-easy-forms/super-easy-forms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-lambda.js
More file actions
220 lines (213 loc) · 6.6 KB
/
create-lambda.js
File metadata and controls
220 lines (213 loc) · 6.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//Import AWS SDK
var AWS = require('aws-sdk');
//IAM
var iam = new AWS.IAM({apiVersion: '2010-05-08'});
//Lambda
var lambda = new AWS.Lambda({apiVersion: '2015-03-31'});
//package to use the file system
var fs = require("fs");
//pazkage to zip files
var JSZip = require("jszip");
//import the createApi function
const createApi = require('./create-api');
const uniqNow = new Date().toISOString().replace(":","-").replace(":","-").replace(".","-");
exports.script = function createLambda(itemString, tableName) {
var json = `{${itemString}}`;
console.log('\x1b[33m', json, '\x1b[0m');
let rawdata = fs.readFileSync('variables.json');
obj = JSON.parse(rawdata);
var source = obj.source;
var mailId = obj.emailArn;
var tableId = obj.tableArn;
const lambdaFunc =
`//Import AWS SDK
var AWS = require('aws-sdk');
//Declare SES
var ses = new AWS.SES({apiVersion: '2010-12-01'});
//Declare Dynamo DB
var dynamodb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
//Main function
exports.handler = (event, context, callback) => {
//goes inside lambda function
var jsonBase = ${json};
let uniqNow = Math.floor(Math.random() * 900000000000000000).toString(28) + new Date().toISOString().replace(":","-").replace(":","-").replace(".","-") + Math.floor(Math.random() * 90000000).toString(28);
Object.keys(event).map(function(key, index) {
jsonBase[key] = {S:event[key]};
});
jsonBase['id'] = {S:uniqNow};
var params = {
Item: jsonBase,
TableName: "${tableName}",
};
var contactInfo = '';
for(let item in event){
contactInfo += '<span><b>' + item + ': </b>' + event[item] + '</span><br>';
}
const emailMessage = '<h4>New Contact</h4><br><p>Someone has just filled out your super easy form! bellow are their details: <br>' + contactInfo;
dynamodb.putItem(params, function(err, data) {
if (err) {
console.log(err, err.stack); // an error occurred
}
else {
console.log(event);
console.log(data);
//SES SEND EMAIl
var params = {
Destination: {
ToAddresses: [
"${source}",
]
},
Message: {
Body: {
Html: {
Charset: "UTF-8",
Data: emailMessage
},
Text: {
Charset: "UTF-8",
Data: "This is the message body in text format."
}
},
Subject: {
Charset: "UTF-8",
Data: "Super Easy Forms - New Contact"
}
},
ReplyToAddresses: [
],
Source: "${source}",
};
ses.sendEmail(params, function(err, data) {
if (err) {
console.log(err); // an error occurred
return err;
}
else {
console.log(data);
return data;
}
});
}
});
callback(null, 'All Done!');
};`;
var zip = new JSZip();
zip.file("lambdaFunc.js", lambdaFunc);
zip
.generateNodeStream({type:'nodebuffer',streamFiles:true})
.pipe(fs.createWriteStream('lambda.zip'))
.on('finish', function () {
// JSZip generates a readable stream with a "end" event,
// but is piped here in a writable stream which emits a "finish" event.
console.log('\x1b[32m', 'Succesfully created and zipped your lambda function.', '\x1b[0m');
lambdaScript(mailId, tableId);
});
}
function lambdaScript(sesarn, tablearn){
const trustRel =`{"Version": "2012-10-17","Statement": [{"Effect": "Allow","Principal": {"Service": "lambda.amazonaws.com"},"Action": "sts:AssumeRole"}]}`;
const contactPolicy = (
`{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"dynamodb:GetItem",
"dynamodb:PutItem",
"dynamodb:UpdateItem"
],
"Resource": "${tablearn}"
},
{
"Effect": "Allow",
"Resource": "${sesarn}",
"Action": [
"SES:SendEmail",
"SES:SendRawEmail"
]
}
]
}`
);
// CREATE THE IAM POLICY
var policyParams = {
PolicyDocument: contactPolicy, /* required */
PolicyName: `easyContactPolicy${uniqNow}`, /* required */
Description: 'the IAM policy that allows the role to communicate with the dynamo DB table'
};
iam.createPolicy(policyParams, function(err, data) {
if (err){
console.log(err, err.stack);
}
else {
console.log('\x1b[32m', 'Succesfully created the IAM policy: ', data.Policy.PolicyName, '\x1b[0m');
var policyArn = data.Policy.Arn;
// CREATE THE IAM ROLE
var rolParams = {
AssumeRolePolicyDocument: trustRel, /* required */
RoleName: `easyContactRole${uniqNow}`, /* required */
Description: 'The Role that allows the Lambda function to interact with the IAM policy',
Tags: [
{
Key: 'name', /* required */
Value: `easyContactRole${uniqNow}` /* required */
},
]
};
iam.createRole(rolParams, function(err, data) {
if (err) {
console.log(err, err.stack);
}
else {
console.log('\x1b[32m', 'Succesfully created the IAM Role: ', data.Role.RoleName, '\x1b[0m');
const rolArn = data.Role.Arn;
const rolName = data.Role.RoleName;
// ATTACH THE IAM POLICY TO THE NEW ROLE
var attachParams = {
PolicyArn: policyArn,
RoleName: rolName
};
iam.attachRolePolicy(attachParams, function(err, data) {
if (err) {
console.log(err, err.stack);
}
else {
console.log('Please wait 10 seconds ...');
// WAIT 10 SECONDS
setTimeout(
function createCopyFunc(){
// CREATE THE LAMBDA COPY BUCKET FUNCTION
var funcParams = {
Code: {
ZipFile: fs.readFileSync('lambda.zip')
},
Description: "This Lambda Function Adds your contact info. to a Dynamo DB table and then sends you an email.",
FunctionName: `superEasyFunction${uniqNow}`,
Handler: "lambdaFunc.handler",
MemorySize: 128,
Publish: true,
Role: rolArn,
Runtime: "nodejs8.10",
Timeout: 30,
};
lambda.createFunction(funcParams, function(err, data) {
if (err) {
console.log(err, err.stack);
}
else {
console.log('\x1b[32m', 'Succesfully created your Lambda function: ', data.FunctionName, '\x1b[0m');
const functionArn = data.FunctionArn;
const functionName = data.FunctionName;
// CALL THE CREATE API FUNCTION
createApi.script(functionArn, functionName, uniqNow);
}
});
}, 10000);
}
});
}
})
}
})
}