-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
45 lines (43 loc) · 1.18 KB
/
index.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
'use strict'
const failureLambda = require('failure-lambda')
const AWS = require('aws-sdk')
const dynamoDb = new AWS.DynamoDB.DocumentClient()
let response
exports.handler = failureLambda(async (event, context) => {
function random(low, high) {
return Math.round(Math.random() * (high - low) + low)
}
const ddbParams = {
TableName: process.env.FAILURE_INJECTION_TABLE,
Key: {
id: random(1, 16) //Math.floor(Math.random() * 16) + 1,
},
}
console.log(ddbParams)
try {
dynamoDb.get(ddbParams, (error, result) => {
if (error) {
console.error(error);
const errorResponse = {
statusCode: error.statusCode || 501,
headers: { 'Content-Type': 'text/plain' },
body: 'Couldn\'t fetch the item.',
};
return errorResponse;
}
result.Item.Duration = context.getRemainingTimeInMillis()
response = {
'statusCode': 200,
'headers': {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
'body': JSON.stringify(result.Item)
}
})
} catch (err) {
console.log(err)
return err
}
return response
})