-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.js
executable file
·163 lines (146 loc) · 4.07 KB
/
handler.js
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
'use strict';
var AWSXRay = require('aws-xray-sdk');
var AWS = AWSXRay.captureAWS(require('aws-sdk'));
//var AWS = require('aws-sdk');
AWS.config.update({
region: 'eu-central-1'
});
const fetch = require('node-fetch');
const lambda = new AWS.Lambda();
const s3 = new AWS.S3();
const dynamodb = new AWS.DynamoDB();
const default_url = 'cutom_url';
// This function is for test scenario 1. it downloads an image from a s3 bucket, saves it into
// s3 bukcet under a different name, and then records an item in dynamodb. Under high load, this functions
// times out thanks to default limitations of dynamodb table
module.exports.save = (event, context, callback) => {
let id = context.awsRequestId;
fetch(default_url)
.then((response) => {
if (response.ok) {
return response;
}
return Promise.reject(new Error(
`Failed to fetch ${response.url}: ${response.status} ${response.statusText}`));
})
.then(response => {
return response.buffer()
})
.then(buffer => {
return s3.putObject({
Bucket: process.env.BUCKET,
Key: context.awsRequestId,
Body: buffer,
}).promise()
}).then(() => {
console.log('successfully put object in s3');
let params = {
Item: {
"id": {
S: id
}
},
ConditionExpression: 'attribute_not_exists(id)',
TableName: process.env.DB_TABLE
};
return dynamodb.putItem(params).promise();
})
.then(data => {
console.log('successfully wrote into dynamodb. retunrning result and finishing');
callback(null, {
"statusCode": 201,
"body": JSON.stringify({
"message": "succeed"
})
})
})
.catch(error => {
console.log('error is ', error);
callback(null, {
"statusCode": 500,
"body": JSON.stringify(error)
})
});
}
// this function is for test scenario 2. Output format of the function doesnt comply with AWS Prxoy Integration:
// The body porperty of the repsonse isnt stringified. This makes the end user to recieve error.
module.exports.badFormat = (event, context, callback) => {
let id = context.awsRequestId;
fetch(default_url)
.then((response) => {
if (response.ok) {
return response;
}
return Promise.reject(new Error(
`Failed to fetch ${response.url}: ${response.status} ${response.statusText}`));
})
.then(response => {
return response.buffer()
})
.then(buffer => {
return s3.putObject({
Bucket: process.env.BUCKET,
Key: context.awsRequestId,
Body: buffer,
}).promise()
}).then(() => {
console.log('successfully put object in s3');
let params = {
Item: {
"id": {
S: id
}
},
ConditionExpression: 'attribute_not_exists(id)',
TableName: process.env.DB_TABLE
};
return dynamodb.putItem(params).promise();
})
.then(data => {
console.log('successfully wrote into dynamodb. retunrning result and finishing');
callback(null, {
"statusCode": 201,
"body": {
message: "I have succeeded"
}
})
})
.catch(error => {
console.log('error is ', error);
callback(null, {
"statusCode": 500,
"body": {
message: "I have failed"
}
})
});
}
// this function is for test scenairo 3. It tries to record an item in DynamoDB with duplicate partition key
// and subsequnetly it throws an excpetion.
module.exports.dbError = (event, context, callback) => {
let id = 'three';
let params = {
Item: {
"id": {
S: id
}
},
ConditionExpression: 'attribute_not_exists(id)',
TableName: process.env.DB_TABLE
};
dynamodb.putItem(params).promise().then(() => {
callback(null, {
"statusCode": 200,
"body": JSON.stringify({
"message": "succeed"
})
})
}).catch(error => {
callback(null, {
"statusCode": 500,
"body": JSON.stringify({
"message": error
})
})
})
}