forked from andreas-optimizely/lambda-datafile-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
51 lines (42 loc) · 1.21 KB
/
index.js
File metadata and controls
51 lines (42 loc) · 1.21 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
'use strict';
const AWS = require('aws-sdk'),
rp = require('request-promise');
// Setup AWS to write to S3
const config = new AWS.Config({
accessKeyId: process.env.ACCESS_KEY_ID,
secretAccessKey: process.env.SECRET_ACCESS_KEY,
region: process.env.REGION
});
AWS.config.update(config);
const s3 = new AWS.S3();
// Insert your key
let datafileKey = 'TxLUGAK7LV6FVp8BZikMgw';
let dataFileUrl = `https://cdn.optimizely.com/datafiles/${datafileKey}.json`;
let options = {
uri: dataFileUrl,
json: true
}
// Lambda function called when webhook is called
exports.handler = (event, context) => {
// Request datafile
rp(options)
.then((datafile) => {
console.log('Here\'s the datafile: ', datafile);
let params = {
Bucket: process.env.BUCKET,
Key: 'optimizely-datafile.json', // modify with your own key for s3
Body: JSON.stringify(datafile),
ContentType: "application/json"
}
// uploads to s3
s3.upload(params, (err, data) => {
if(err){
console.log('there was an error ', err);
}
return console.log('Finished running lambda');
});
})
.catch(function (err) {
console.log('ERROR ', err);
});
}