-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Expand file tree
/
Copy pathhandler.js
More file actions
64 lines (51 loc) · 1.64 KB
/
handler.js
File metadata and controls
64 lines (51 loc) · 1.64 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
'use strict';
const AWS = require('aws-sdk');
const fetch = require('node-fetch');
const eventbridge = new AWS.EventBridge({ apiVersion: '2015-10-07' });
function putEvent(event) {
const detail = { name: 'Message',
messageSentToAlice: event.body,
};
const params = {
Entries: [
{
EventBusName: 'custom-decouple-events',
Detail: JSON.stringify(detail),
DetailType: 'Triggering_Bob',
Source: 'alice.trigger',
},
],
};
return eventbridge.putEvents(params).promise();
}
module.exports.alice = async (event) => {
console.log(`event:${JSON.stringify(event)}`);
const data = await putEvent(event);
console.log(data);
return {
statusCode: 200,
body: JSON.stringify({ message: 'Alice was called' }),
};
};
module.exports.bob = async (event) => {
console.log(`event:${JSON.stringify(event)}`);
const eventDetail = event.detail.messageSentToAlice;
const messageSentFromAlice = JSON.parse(eventDetail).messageSentToAlice;
console.log(`messageSentFromAlice:${messageSentFromAlice}`);
};
function getRandomIntInclusive(min, max) {
const mini = Math.ceil(min);
const maxi = Math.floor(max);
return Math.floor((Math.random() * ((maxi - mini) + 1)) + mini);
}
module.exports.eve = async (event) => {
console.log(`event:${JSON.stringify(event)}`);
};
module.exports.dave = async (event) => {
console.log(`event:${JSON.stringify(event)}`);
const pokemonNumber = getRandomIntInclusive(1, 151);
const res = await fetch(`https://pokeapi.co/api/v2/pokemon/${pokemonNumber}`);
const json = await res.json();
console.log('responseJSON:', JSON.stringify(json));
return json;
};