-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecuteFunction.js
More file actions
183 lines (153 loc) · 6.74 KB
/
executeFunction.js
File metadata and controls
183 lines (153 loc) · 6.74 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
//requieres
var AWS = require("aws-sdk");
var fs = require('fs');
// Configure AWS SDK for JavaScript
AWS.config.update({region: 'us-east-1'});
AWS.config.loadFromPath('./config.json');
//Variables
var results ={};
var params = {};
var lambda = new AWS.Lambda({region: 'us-east-1', apiVersion: '2015-03-31'});
var lambdaParms = {
FunctionName : 'sumFromS3',
InvocationType : 'RequestResponse',
LogType : 'None',
};
//get numbers
var args = process.argv.slice(2);
if(args && args.length!=2){
console.error("You must type two numbers");
return;
}
var number1 = args[0];
var number2 = args[1];
//Create objects at S3 to store number1
var s3 = new AWS.S3();
//Create number1
s3.putObject(
{
Bucket: 'cem.itesm.coding',
Key: 'number1',
Body: number1,
ACL: 'public-read'
}
,
function (resp) {
console.log("Loaded S3 number1");
//Create number1
s3.putObject(
{
Bucket: 'cem.itesm.coding',
Key: 'number2',
Body: number2,
ACL: 'public-read'
}
,
function (resp) {
console.log("Loaded S3 number2");
//Load readFromS3 function
params = {
Code: {
"ZipFile": fs.readFileSync('./readS3.zip')
},
Description: "",
FunctionName: "readS3",
Handler: "readS3.handler", // is of the form of the name of your source file and then name of your function handler
MemorySize: 128,
Publish: true,
Role: "arn:aws:iam::773710499283:role/AESCLambda", // replace with the actual arn of the execution role you created
Runtime: "nodejs6.10",
Timeout: 15,
VpcConfig: {
}
};
lambda.createFunction(params, function(err, data) {
//if (err) console.log(err, err.stack); // an error occurred
console.log("Loaded readS3 Lambda function");
//Load sumFromS3 function
var params = {
Code: {
"ZipFile": fs.readFileSync('./sumFromS3.zip')
},
Description: "",
FunctionName: "sumFromS3",
Handler: "sumFromS3.handler", // is of the form of the name of your source file and then name of your function handler
MemorySize: 128,
Publish: true,
Role: "arn:aws:iam::773710499283:role/AESCLambda", // replace with the actual arn of the execution role you created
Runtime: "nodejs6.10",
Timeout: 15,
VpcConfig: {
}
};
lambda.createFunction(params, function(err, data) {
//if (err) console.log(err, err.stack); // an error occurred
console.log("Loaded sumFromS3 Lambda function");
//INVOKING THE SUM FUNCTION
lambda.invoke(lambdaParms, function(err, data) {
console.log("Called sumFromS3 Lambda function");
if (err) {
console.log("Error:"+ err);
} else {
console.log("result:" + JSON.stringify(data.Payload));
//deleting functions:
lambda.deleteFunction({FunctionName:"readS3"},function(err,data){
if(err)
console.error(err);
});
lambda.deleteFunction({FunctionName:"sumFromS3"},function(err,data){
if(err)
console.error( err);
});
//deleting files
s3.deleteObject({Bucket:"cem.itesm.coding",Key:"number1"},function(err,data){
if(err)
console.error( err);
});
s3.deleteObject({Bucket:"cem.itesm.coding",Key:"number2"},function(err,data){
if(err)
console.error( err);
});
}
});
});
});
}
)
}
);
//Put function first
/*
var s3 = new AWS.S3();
s3.putObject({
Bucket: 'cem.itesm.coding',
Key: 'function1.js',
Body: code,
ACL: 'public-read'
},function (resp) {
console.log(arguments);
//var id = arguments[1].ETag;
console.log('Successfully uploaded package.');
// Prepare to call Lambda function
var lambda = new AWS.Lambda({region: 'us-east-1', apiVersion: '2015-03-31'});
var params = {
Code: {
"ZipFile": fs.readFileSync('./function1.zip')
},
Description: "",
FunctionName: "saveS3_1",
Handler: "function1.handler", // is of the form of the name of your source file and then name of your function handler
MemorySize: 128,
Publish: true,
Role: "arn:aws:iam::773710499283:role/AESCLambda", // replace with the actual arn of the execution role you created
Runtime: "nodejs6.10",
Timeout: 15,
VpcConfig: {
}
};
lambda.createFunction(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
});
*/