This repository has been archived by the owner on Dec 11, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
137 lines (122 loc) · 3.52 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
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
'use strict';
const https = require('https');
const url = require('url');
const CognitoIdentity = require('aws-sdk/clients/cognitoidentity');
const cognitoidentity = new CognitoIdentity();
const SUCCESS = 'SUCCESS';
const FAILED = 'FAILED';
const UNKNOWN = {
Error: 'Unknown operation'
};
const validRequestTypes = new Set([
'Create',
'Update',
'Delete'
]);
/**
* The Lambda function handler
* See also: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/crpg-ref.html
*
* @param {!Object} event
* @param {!string} event.RequestType
* @param {!string} event.ServiceToken
* @param {!string} event.ResponseURL
* @param {!string} event.StackId
* @param {!string} event.RequestId
* @param {!string} event.LogicalResourceId
* @param {!string} event.PhysicalResourceId
* @param {!Object} event.ResourceProperties
* @param {!Object} event.ResourceProperties.Options
* @param {!Object} context
* @param {!Requester~requestCallback} callback
*/
exports.handler = (event, context, callback) => {
console.log(event, context);
const {
StackId,
RequestId,
LogicalResourceId,
PhysicalResourceId,
RequestType,
ResponseURL,
ResourceProperties
} = event;
const {logStreamName} = context;
function respond(Status, Data, PhysicalResourceId=logStreamName) {
const responseBody = JSON.stringify({
Status,
Reason: `See the details in CloudWatch Log Stream: ${logStreamName}`,
PhysicalResourceId,
StackId,
RequestId,
LogicalResourceId,
Data
});
console.log('Response body:\n', responseBody);
const {hostname, path} = url.parse(ResponseURL);
const options = {
hostname,
port: 443,
path,
method: 'PUT',
headers: {
'content-type': '',
'content-length': responseBody.length
}
};
return new Promise((resolve, reject) => {
const request = https
.request(options, resolve);
request.on('error', error => reject(`send(..) failed executing https.request(..): ${error}`));
request.write(responseBody);
request.end();
})
.then(() => callback(Status === FAILED ? Status : null, Data))
.catch(callback);
}
if (!validRequestTypes.has(RequestType))
return respond(FAILED, UNKNOWN);
const params = RequestType === 'Delete' ? {} : Object.assign({}, ResourceProperties.Options);
switch (LogicalResourceId) {
case 'CognitoIdentityPool':
if (RequestType !== 'Create')
params.IdentityPoolId = PhysicalResourceId;
// CloudFormation passes the value as a string, so it must be converted to a valid javascript object
if (RequestType !== 'Delete') {
const parseParams = [
'AllowUnauthenticatedIdentities',
'CognitoIdentityProviders',
'OpenIdConnectProviderARNs',
'SamlProviderARNs',
'SupportedLoginProviders'
];
try {
for (let param of parseParams) {
if (params[param])
params[param] = JSON.parse(params[param]);
}
} catch (message) {
return respond(FAILED, {message});
}
}
return cognitoidentity[`${RequestType.toLowerCase()}IdentityPool`](params)
.promise()
.then(data => respond(SUCCESS, data, data.IdentityPoolId))
.catch(message => respond(FAILED, {message}));
case 'CognitoIdentityPoolRoles':
switch (RequestType) {
case 'Create':
case 'Update':
return cognitoidentity
.setIdentityPoolRoles(params)
.promise()
.then(data => respond(SUCCESS, data))
.catch(message => respond(FAILED, {message}));
case 'Delete':
return respond(SUCCESS);
}
break;
default:
return respond(FAILED, UNKNOWN);
}
};