@incdevco/framework
Version:
node.js lambda framework
1,566 lines (941 loc) • 35.8 kB
JavaScript
var Util = require('util');
var AWS = require('aws-sdk');
var Promise = require('bluebird');
var Utilities = require('../../utilities');
var Base = require('../index');
function Lambda(config) {
'use strict';
Base.call(this, config);
this.apiGateway = config.apiGateway || new AWS.APIGateway();
this.cloudwatchlogs = config.cloudwatchlogs || new AWS.CloudWatchLogs();
this.cognito = config.cognito || new AWS.CognitoIdentity();
this.cognitoSync = config.cognitoSync || new AWS.CognitoSync();
this.firehose = config.firehose || new AWS.Firehose();
this.https = config.https || require('https');
this.lambda = config.lambda || new AWS.Lambda();
this.s3 = config.s3 || new AWS.S3();
this.ses = config.ses || new AWS.SES();
this.waitForDelay = config.waitForDelay || 30000;
this.warmUpDelay = config.warmUpDelay || 60000;
}
Util.inherits(Lambda, Base);
Lambda.prototype.ApiGatewayGetApiKey = function (event, response) {
'use strict';
var options, params = {}, promise, self = this;
options = event.ResourceProperties.Options || event.ResourceProperties;
promise = Promise.resolve(options);
params.apiKey = options.ApiKeyId;
if (event.RequestType === 'Create'
|| event.RequestType === 'Update') {
params.includeValue = true;
promise = this.apiGateway.getApiKey(params).promise();
} else if (event.RequestType === 'Delete') {
// do nothing
} else {
promise = Promise.reject(new Error('RequestType Not Implemented'));
}
return promise
.then(function (result) {
self.log('getApiKey', result);
response.Status = 'SUCCESS';
response.PhysicalResourceId = 'ApiGatewayGetApiKey';
response.Data.ApiKey = result.value;
return true;
})
.catch(function (exception) {
self.log('expection', exception, exception.stack);
response.Status = 'FAILED';
response.Reason = exception.message;
return false;
});
};
Lambda.prototype.ApiGatewayCustomDomainName = function (event, response) {
'use strict';
var options, params = {}, promise, self = this;
options = event.ResourceProperties.Options || event.ResourceProperties;
promise = Promise.resolve(options);
params.domainName = options.domainName;
if (event.RequestType === 'Create') {
// create custom domain name
params.certificateArn = options.certificateArn;
this.log('createDomainName', JSON.stringify(params, null, 2));
promise = this.apiGateway.createDomainName(params).promise();
} else if (event.RequestType === 'Delete') {
// delete custom domain name
this.log('deleteDomainName', JSON.stringify(params, null, 2));
promise = this.apiGateway.deleteDomainName(params).promise()
.catch(function (exception) {
if (exception.code === 'NotFoundException') {
return false;
}
throw exception;
});
} else if (event.RequestType === 'Update') {
// do nothing
} else {
promise = Promise.reject(new Error('RequestType Not Implemented'));
}
return promise
.then(function (result) {
response.Status = 'SUCCESS';
response.PhysicalResourceId = result.distributionDomainName
|| event.PhysicalResourceId || 'ApiGatewayCustomDomainName';
response.Data.DomainName = result.distributionDomainName;
return true;
})
.catch(function (exception) {
self.log('expection', exception, exception.stack);
response.Status = 'FAILED';
response.PhysicalResourceId = 'ApiGatewayCustomDomainName';
response.Reason = exception.message;
return false;
});
};
Lambda.prototype.ApiGatewayDeployment = function (event, response) {
'use strict';
var methodSettings, options, params = {}, promise, self = this;
options = event.ResourceProperties.Options || event.ResourceProperties;
promise = Promise.resolve(options);
params = Utilities.copy(options);
if (params.cacheClusterEnabled === 'true') {
params.cacheClusterEnabled = true;
} else {
params.cacheClusterEnabled = false;
}
if (event.RequestType === 'Create'
|| event.RequestType === 'Update') {
if (params.methodSettings) {
methodSettings = Utilities.copy(params.methodSettings);
delete params.methodSettings;
}
promise = this.apiGateway.createDeployment(params).promise()
.catch(function (exception) {
console.error('Deployment', exception);
if (exception.code === 'TooManyRequestsException') {
return Promise.delay(30000)
.then(function () {
return self.apiGateway.createDeployment(params).promise();
});
}
throw exception;
})
.then(function (result) {
if (methodSettings) {
var stageParams = {
restApiId: params.restApiId,
stageName: params.stageName,
patchOperations: []
};
Object.keys(methodSettings).forEach(function (path) {
if (typeof methodSettings[path].dataTraceEnabled ) {
stageParams.patchOperations.push({
op: 'replace',
path: '/' + path + '/logging/dataTrace',
value: methodSettings[path].dataTraceEnabled
});
}
if (typeof methodSettings[path].throttlingRateLimit) {
stageParams.patchOperations.push({
op: 'replace',
path: '/' + path + '/throttling/rateLimit',
value: methodSettings[path].throttlingRateLimit
});
}
if (typeof methodSettings[path].metricsEnabled) {
stageParams.patchOperations.push({
op: 'replace',
path: '/' + path + '/metrics/enabled',
value: methodSettings[path].metricsEnabled
});
}
if (typeof methodSettings[path].throttlingBurstLimit) {
stageParams.patchOperations.push({
op: 'replace',
path: '/' + path + '/throttling/burstLimit',
value: methodSettings[path].throttlingBurstLimit
});
}
if (typeof methodSettings[path].cachingEnabled) {
stageParams.patchOperations.push({
op: 'replace',
path: '/' + path + '/caching/enabled',
value: methodSettings[path].cachingEnabled
});
}
if (typeof methodSettings[path].cacheClusterSize === 'string') {
stageParams.patchOperations.push({
op: 'replace',
path: '/cacheClusterSize',
value: methodSettings[path].cacheClusterSize
});
}
if (typeof methodSettings[path].loggingLevel === 'string') {
stageParams.patchOperations.push({
op: 'replace',
path: '/' + path + '/logging/loglevel',
value: methodSettings[path].loggingLevel
});
}
});
self.log('apiGateway.updateStage', JSON.stringify(stageParams, null, 2));
return self.apiGateway.updateStage(stageParams).promise()
.then(function (stageResult) {
self.log('apiGateway.updateStage', JSON.stringify(stageResult, null, 2));
return result;
});
}
return result;
});
} else if (event.RequestType === 'Delete') {
// do nothing
promise = Promise.resolve({
id: event.PhysicalResourceId
});
} else {
promise = Promise.reject(new Error('RequestType Not Implemented'));
}
return promise
.then(function (result) {
//console.log('result', result);
response.Status = 'SUCCESS';
response.PhysicalResourceId = result.id;
response.Data.CreatedDate = result.CreatedDate;
response.Data.Description = result.description;
return true;
})
.catch(function (exception) {
self.log('expection', exception, exception.stack);
response.Status = 'FAILED';
response.PhysicalResourceId = 'ApiGatewayDeployment';
response.Reason = exception.message;
return false;
});
};
Lambda.prototype.ApiGatewayMethodRequestValidator = function (event, response) {
'use strict';
var options, params = {}, promise, self = this;
options = event.ResourceProperties.Options || event.ResourceProperties;
promise = Promise.resolve(options);
params.httpMethod = options.httpMethod;
params.restApiId = options.restApiId;
params.resourceId = options.resourceId;
if (event.RequestType === 'Create'
|| event.RequestType === 'Update') {
// update method with request validator id
params.patchOperations = [
{
op: 'replace',
path: '/requestValidatorId',
value: options.requestValidatorId
}
];
this.log('apiGateway.updateMethod', JSON.stringify(params, null, 2));
promise = this.apiGateway.updateMethod(params).promise();
} else if (event.RequestType === 'Delete') {
// do nothing
} else {
promise = Promise.reject(new Error('RequestType Not Implemented'));
}
response.PhysicalResourceId = event.LogicalResourceId + '-ApiGatewayMethodRequestValidator';
return promise
.then(function (result) {
response.Status = 'SUCCESS';
return true;
})
.catch(function (exception) {
self.log('expection', exception, exception.stack);
response.Status = 'FAILED';
response.Reason = exception.message;
return false;
});
};
Lambda.prototype.ApiGatewayRequestValidator = function (event, response) {
'use strict';
var options, params = {}, promise, self = this;
options = event.ResourceProperties.Options || event.ResourceProperties;
promise = Promise.resolve(options);
params.restApiId = options.restApiId;
if (event.RequestType === 'Create') {
// create request validator
params.name = options.name;
params.validateRequestBody = (options.validateRequestBody == 'true');
params.validateRequestParameters = (options.validateRequestParameters == 'true');
promise = this.apiGateway.createRequestValidator(params).promise();
} else if (event.RequestType === 'Delete') {
// delete request validator
params.requestValidatorId = event.PhysicalResourceId;
promise = this.apiGateway.deleteRequestValidator(params).promise()
.catch(function (exception) {
if (exception.code === 'NotFoundException') {
return false;
}
throw exception
});
} else if (event.RequestType === 'Update') {
// update request validator
params.requestValidatorId = event.PhysicalResourceId;
params.patchOperations = [
{
op: 'replace',
path: '/validateRequestBody',
value: options.validateRequestBody
},
{
op: 'replace',
path: '/validateRequestParameters',
value: options.validateRequestParameters
}
];
promise = this.apiGateway.updateRequestValidator(params).promise();
} else {
promise = Promise.reject(new Error('RequestType Not Implemented'));
}
return promise
.then(function (result) {
response.Status = 'SUCCESS';
response.PhysicalResourceId = result.id || event.PhysicalResourceId;
response.Data.id = response.PhysicalResourceId;
response.Data.name = result.name;
return true;
})
.catch(function (exception) {
self.log('expection', exception, exception.stack);
response.Status = 'FAILED';
response.PhysicalResourceId = event.PhysicalResourceId || 'ApiGatewayRequestValidator';
response.Reason = exception.message;
return false;
});
};
Lambda.prototype.ApiGatewayUsagePlanKey = function (event, response) {
'use strict';
var options, params = {}, promise, self = this;
options = event.ResourceProperties.Options || event.ResourceProperties;
promise = Promise.resolve(options);
params.keyId = options.ApiKeyId;
params.usagePlanId = options.UsagePlanId;
if (event.RequestType === 'Create') {
// create usage plan api key
params.keyType = 'API_KEY';
promise = this.apiGateway.createUsagePlanKey(params).promise();
} else if (event.RequestType === 'Delete') {
// delete usage plan api key
promise = this.apiGateway.deleteUsagePlanKey(params).promise()
.catch(function (exception) {
if (exception.code === 'NotFoundException') {
return false;
}
throw exception;
});
} else if (event.RequestType === 'Update') {
// do nothing
} else {
promise = Promise.reject(new Error('RequestType Not Implemented'));
}
return promise
.then(function (result) {
response.Status = 'SUCCESS';
response.PhysicalResourceId = 'ApiGatewayUsagePlanKey';
return true;
})
.catch(function (exception) {
self.log('expection', exception, exception.stack);
response.Status = 'FAILED';
response.Reason = exception.message;
return false;
});
};
Lambda.prototype.ApiGatewayUsagePlanRemoveApiStagesBeforeDelete = function (event, response) {
'use strict';
var options, params = {}, promise, self = this;
options = event.ResourceProperties.Options || event.ResourceProperties;
promise = Promise.resolve(options);
if (event.RequestType === 'Create'
|| event.RequestType === 'Update') {
// do nothing
promise = Promise.resolve({});
} else if (event.RequestType === 'Delete') {
params.usagePlanId = options.usagePlanId;
params.patchOperations = [];
options.apiStages.forEach(function (stage) {
params.patchOperations.push({
op: 'remove',
path: '/apiStages',
value: stage.apiId + ':' + stage.stageName
});
});
self.log('updateUsagePlan', JSON.stringify(params, null, 2));
promise = this.apiGateway.updateUsagePlan(params).promise();
} else {
promise = Promise.reject(new Error('RequestType Not Implemented'));
}
response.PhysicalResourceId = options.Bucket + '-S3BucketEmptyBeforeDelete';
return promise
.then(function (result) {
self.log('result', JSON.stringify(result));
response.Status = 'SUCCESS';
return true;
})
.catch(function (exception) {
self.log('expection', exception, exception.stack);
response.Status = 'FAILED';
response.Reason = exception.message;
return false;
});
};
Lambda.prototype.CloudWatchSubscriptionFilter = function (event, response) {
'use strict';
var options, params, promise, self = this;
options = event.ResourceProperties.Options || event.ResourceProperties;
promise = Promise.resolve(options);
if (event.RequestType === 'Create'
|| event.RequestType === 'Update') {
params = options;
// if firehose is destination check it is active before creating subscription
if (params.destinationArn.indexOf('firehose') > 0) {
promise = this.waitForFirehose(params.destinationArn);
}
promise = promise
.then(function () {
self.log('cloudwatchlogs.putSubscriptionFilter', JSON.stringify(params, null, 2));
return self.cloudwatchlogs.putSubscriptionFilter(params).promise();
})
.then(function (result) {
self.log('result', JSON.stringify(result, null, 2));
return Promise.delay(self.warmUpDelay)
.then(function () {
return result;
});
});
} else if (event.RequestType === 'Delete') {
params = {
filterName: options.filterName,
logGroupName: options.logGroupName
};
this.log('deleteSubscriptionFilter', JSON.stringify(params, null, 2));
promise = this.cloudwatchlogs.deleteSubscriptionFilter(params).promise()
.then(function (result) {
self.log('result', JSON.stringify(result, null, 2));
return result;
}, function (exception) {
if (exception.code === 'ResourceNotFoundException') {
self.log('resource not found');
return true;
}
throw exception;
});
} else {
promise = Promise.reject(new Error('RequestType Not Implemented'));
}
return promise
.then(function (result) {
response.Status = 'SUCCESS';
response.PhysicalResourceId = options.logGroupName + ':' + options.filterName;
return true;
})
.catch(function (exception) {
self.log('expection', exception, exception.stack);
response.Status = 'FAILED';
response.Reason = exception.message;
return false;
});
};
Lambda.prototype.CognitoIdentityPool = function (event, response) {
'use strict';
var options, params, promise, self = this;
options = event.ResourceProperties.Options || event.ResourceProperties;
if (options.AllowUnauthenticatedIdentities === 'true') {
options.AllowUnauthenticatedIdentities = true;
} else {
options.AllowUnauthenticatedIdentities = false;
}
promise = Promise.resolve(options);
if (event.RequestType === 'Create') {
params = {
AllowUnauthenticatedIdentities: options.AllowUnauthenticatedIdentities,
IdentityPoolName: options.IdentityPoolName,
CognitoIdentityProviders: options.CognitoIdentityProviders,
DeveloperProviderName: options.DeveloperProviderName,
OpenIdConnectProviderARNs: options.OpenIdConnectProviderARNs,
SamlProviderARNs: options.SamlProviderARNs,
SupportedLoginProviders: options.SupportedLoginProviders
};
this.log('createIdentityPool', JSON.stringify(params, null, 2));
promise = this.cognito.createIdentityPool(params).promise()
.then(function (result) {
self.log('result', JSON.stringify(result, null, 2));
return result;
});
} else if (event.RequestType === 'Delete') {
if (event.PhysicalResourceId) {
params = {
IdentityPoolId: event.PhysicalResourceId
};
this.log('deleteIdentityPool', JSON.stringify(params, null, 2));
promise = this.cognito.deleteIdentityPool(params).promise()
.then(function (result) {
self.log('result', JSON.stringify(result, null, 2));
return {
IdentityPoolId: event.PhysicalResourceId
};
})
.catch(function (exception) {
if (exception.message === "IdentityPool '" + event.PhysicalResourceId + "' not found.") {
return {
IdentityPoolId: event.PhysicalResourceId
};
}
throw exception;
});
} else {
options.IdentityPoolId = 'unknown';
}
} else if (event.RequestType === 'Update') {
params = {
AllowUnauthenticatedIdentities: options.AllowUnauthenticatedIdentities,
IdentityPoolId: options.IdentityPoolId,
IdentityPoolName: options.IdentityPoolName,
CognitoIdentityProviders: options.CognitoIdentityProviders,
DeveloperProviderName: options.DeveloperProviderName,
OpenIdConnectProviderARNs: options.OpenIdConnectProviderARNs,
SamlProviderARNs: options.SamlProviderARNs,
SupportedLoginProviders: options.SupportedLoginProviders
};
this.log('updateIdentityPool', JSON.stringify(params, null, 2));
promise = this.cognito.updateIdentityPool(params).promise()
.then(function (result) {
self.log('result', JSON.stringify(result, null, 2));
return result;
});
} else {
promise = Promise.reject(new Error('RequestType Not Implemented'));
}
return promise
.then(function (result) {
response.Status = 'SUCCESS';
response.PhysicalResourceId = result.IdentityPoolId;
response.Data.IdentityPoolId = result.IdentityPoolId;
return true;
})
.catch(function (exception) {
self.log('expection', exception, exception.stack);
response.Status = 'FAILED';
response.Reason = exception.message;
return false;
});
};
Lambda.prototype.CognitoIdentityPoolRoles = function (event, response) {
'use strict';
var options = event.ResourceProperties.Options, promise, self = this;
if (event.RequestType === 'Create'
|| event.RequestType === 'Update') {
var params = {
IdentityPoolId: options.IdentityPoolId,
Roles: options.Roles
};
this.log('setIdentityPoolRoles', JSON.stringify(params, null, 2));
promise = this.cognito.setIdentityPoolRoles(params).promise()
.then(function (result) {
self.log('result', JSON.stringify(result, null, 2));
return result;
});
} else {
promise = Promise.resolve({
IdentityPoolId: options.IdentityPoolId || 'unknown'
});
}
return promise
.then(function (result) {
response.Status = 'SUCCESS';
response.PhysicalResourceId = 'CognitoIdentityPoolRoles';
response.Data.IdentityPoolId = result.IdentityPoolId;
return true;
})
.catch(function (exception) {
self.log('exception', exception, exception.stack);
response.Status = 'FAILED';
response.Reason = exception.message;
return false;
});
};
Lambda.prototype.CognitoIdentityPoolSyncTrigger = function (event, response) {
'use strict';
var options = event.ResourceProperties.Options, promise, self = this;
if (event.RequestType === 'Create'
|| event.RequestType === 'Update') {
var params = {
IdentityPoolId: options.IdentityPoolId,
Events: {
'SyncTrigger': options.FunctionARN
}
};
this.log('setCognitoEvents', JSON.stringify(params, null, 2));
promise = this.cognitoSync.setCognitoEvents({
IdentityPoolId: options.IdentityPoolId,
Events: {
'SyncTrigger': options.FunctionARN
}
}).promise()
.then(function (result) {
self.log('result', JSON.stringify(result, null, 2));
return result;
});
} else {
promise = Promise.resolve(true);
}
return promise
.then(function (result) {
response.Status = 'SUCCESS';
response.PhysicalResourceId = 'CognitoSyncTrigger';
response.Data.IdentityPoolId = options.IdentityPoolId;
return true;
})
.catch(function (exception) {
self.log('exception', exception, exception.stack);
response.Status = 'FAILED';
response.Reason = exception.message;
return false;
});
};
Lambda.prototype.waitForFirehose = function (arn) {
'use strict';
var name = arn.split('/')[1];
var p = {
DeliveryStreamName: name
};
var self = this;
this.log('firehose.describeDeliveryStream', JSON.stringify(p, null, 2));
return this.firehose.describeDeliveryStream(p).promise()
.then(function (result) {
self.log('result', JSON.stringify(result, null, 2));
if (result.DeliveryStreamDescription.DeliveryStreamStatus === 'ACTIVE') {
return true;
} else if (result.DeliveryStreamDescription.DeliveryStreamStatus === 'CREATING') {
return Promise.delay(self.waitForDelay)
.then(function () {
return self.waitForFirehose(arn);
});
} else {
throw new Error('Firehose status is ' + result.DeliveryStreamDescription.DeliveryStreamStatus);
}
});
};
Lambda.prototype.handler = function (event, context) {
'use strict';
var promise, resourceType, response = {
Data: {}
}, self = this;
this.log('original-event', JSON.stringify(event, null, 2));
resourceType = event.ResourceType.replace(/^Custom::/, '');
//promise = this.passThrough(event, response);
this.log('resourceType', resourceType);
if (typeof this[resourceType] === 'function') {
promise = this[resourceType](event, response);
} else {
response.Status = 'FAILED';
if (event.RequestType === 'Delete') {
response.Status = 'SUCCESS';
}
response.PhysicalResourceId = resourceType;
response.Reason = 'Resource Type Not Implemented';
promise = Promise.resolve(false);
}
promise
.then(function () {
return self.sendCFNResponse(event, response);
})
.then(function () {
self.log('response', JSON.stringify(response, null, 2));
context.succeed(response);
})
.catch(function (exception) {
context.fail(exception);
});
};
Lambda.prototype.LambdaEnvironment = function (event, response) {
'use strict';
var options, params = {}, promise, self = this;
options = event.ResourceProperties.Options || event.ResourceProperties;
promise = Promise.resolve(options);
if (event.RequestType === 'Create'
|| event.RequestType === 'Update') {
params.FunctionName = options.FunctionName;
params.Environment = options.Environment;
this.log('updateFunctionConfiguration', JSON.stringify(params, null, 2));
promise = this.lambda.updateFunctionConfiguration(params).promise();
} else if (event.RequestType === 'Delete') {
// do nothing
promise = Promise.resolve(true);
} else {
promise = Promise.reject(new Error('RequestType Not Implemented'));
}
return promise
.then(function (result) {
self.log('result', result);
response.Status = 'SUCCESS';
response.PhysicalResourceId = 'LambdaEnvironment';
return true;
})
.catch(function (exception) {
self.log('expection', exception, exception.stack);
response.Status = 'FAILED';
response.PhysicalResourceId = 'LambdaEnvironment';
response.Reason = exception.message;
return false;
});
};
Lambda.prototype.LambdaFunctionVersion = function (event, response) {
'use strict';
var options, params = {}, promise, self = this;
options = event.ResourceProperties.Options || event.ResourceProperties;
promise = Promise.resolve(options);
params = Utilities.copy(options);
if (event.RequestType === 'Create'
|| event.RequestType === 'Update') {
/*
{
CodeSha256: "",
Description: "",
FunctionName: ""
}
*/
promise = this.lambda.publishVersion(params).promise();
} else if (event.RequestType === 'Delete') {
// do nothing
promise = Promise.resolve({
id: event.PhysicalResourceId
});
} else {
promise = Promise.reject(new Error('RequestType Not Implemented'));
}
return promise
.then(function (result) {
self.log('result', result);
response.Status = 'SUCCESS';
response.PhysicalResourceId = result.Version;
response.Data.CodeSha256 = result.CodeSha256;
return true;
})
.catch(function (exception) {
self.log('expection', exception, exception.stack);
response.Status = 'FAILED';
response.PhysicalResourceId = 'LambdaFunctionVersion';
response.Reason = exception.message;
return false;
});
};
Lambda.prototype.passThrough = function (event, response) {
'use strict';
response.Status = 'SUCCESS';
response.PhysicalResourceId = 'passThrough';
return Promise.resolve(true);
};
Lambda.prototype.sendCFNResponse = function (event, cfnResponse) {
'use strict';
var self = this,
url = require('url').parse(event.ResponseURL);
cfnResponse.StackId = event.StackId;
cfnResponse.RequestId = event.RequestId;
cfnResponse.LogicalResourceId = event.LogicalResourceId;
return new Promise(function (resolve, reject) {
var body = JSON.stringify(cfnResponse);
var options = {
protocol: url.protocol,
hostname: url.hostname,
path: url.path,
port: 443,
method: 'PUT',
headers: {
'Content-Type': '',
'Content-Length': body.length
}
};
var request = self.https.request(options);
request.on('error', reject);
request.on('response', function (response) {
response.on('error', reject);
response.on('data', function (chunk) {
self.log('data', chunk.toString('utf8'));
});
response.on('end', function() {
resolve(response);
});
});
request.write(body);
request.end();
});
};
Lambda.prototype.s3DeleteObjectsFromBucket = function (bucket, continuationToken) {
'use strict';
var self = this;
var nextContinuationToken;
var params = {
Bucket: bucket,
ContinuationToken: continuationToken
};
this.log('listObjectsV2', JSON.stringify(params, null, 2));
return this.s3.listObjectsV2(params).promise()
.then(function (result) {
params = {
Bucket: bucket,
Delete: {
Objects: []
}
};
result.Contents.forEach(function (obj) {
params.Delete.Objects.push({
Key: obj.Key
});
});
nextContinuationToken = result.NextContinuationToken;
self.log('deleteObjects', JSON.stringify(params, null, 2));
if (params.Delete.Objects.length) {
return self.s3.deleteObjects(params).promise();
} else {
return {};
}
})
.then(function (result) {
self.log('result', JSON.stringify(result, null, 2));
if (nextContinuationToken) {
return self.s3DeleteObjectsFromBucket(bucket, nextContinuationToken);
} else {
return result;
}
});
};
Lambda.prototype.S3BucketEmptyBeforeDelete = function (event, response) {
'use strict';
var options, promise, self = this;
options = event.ResourceProperties.Options || event.ResourceProperties;
promise = Promise.resolve(options);
if (event.RequestType === 'Create'
|| event.RequestType === 'Update') {
// do nothing
promise = Promise.resolve({});
} else if (event.RequestType === 'Delete') {
promise = this.s3DeleteObjectsFromBucket(options.Bucket)
.catch(function (exception) {
if (exception.code === 'NoSuchBucket') {
return false;
}
throw exception;
});
} else {
promise = Promise.reject(new Error('RequestType Not Implemented'));
}
response.PhysicalResourceId = options.Bucket + '-S3BucketEmptyBeforeDelete';
return promise
.then(function (result) {
self.log('result', JSON.stringify(result));
response.Status = 'SUCCESS';
return true;
})
.catch(function (exception) {
self.log('expection', exception, exception.stack);
response.Status = 'FAILED';
response.Reason = exception.message;
return false;
});
};
Lambda.prototype.SESReceiptRule = function (event, response) {
'use strict';
var options, promise, self = this;
options = event.ResourceProperties.Options;
if (event.RequestType === 'Create') {
promise = this.ses.createReceiptRule({
Rule: options.Rule,
RuleSetName: options.RuleSetName
}).promise();
} else if (event.RequestType === 'Update') {
promise = this.ses.updateReceiptRule({
Rule: options.Rule,
RuleSetName: options.RuleSetName
}).promise();
} else if (event.RequestType === 'Delete') {
promise = this.ses.deleteReceiptRule({
RuleName: options.Rule.Name,
RuleSetName: options.RuleSetName
}).promise();
} else {
promise = Promise.reject(new Error('RequestType Not Implemented'));
}
return promise
.then(function (result) {
response.Status = 'SUCCESS';
response.PhysicalResourceId = options.Rule.Name;
response.Data.RuleSetName = options.RuleSetName;
return true;
})
.catch(function (exception) {
self.log('exception', JSON.stringify(exception));
response.Status = 'FAILED';
response.Reason = exception.message;
return false;
});
};
Lambda.prototype.SESReceiptRuleSet = function (event, response) {
'use strict';
var options, promise, self = this;
options = event.ResourceProperties.Options;
if (event.RequestType === 'Create') {
promise = this.ses.createReceiptRuleSet({
RuleSetName: options.RuleSetName
}).promise();
} else if (event.RequestType === 'Update') {
promise = Promise.resolve({
RuleSetName: options.RuleSetName
});
} else if (event.RequestType === 'Delete') {
promise = this.ses.deleteReceiptRuleSet({
RuleSetName: options.RuleSetName
}).promise();
} else {
promise = Promise.reject(new Error('RequestType Not Implemented'));
}
return promise
.then(function (result) {
response.Status = 'SUCCESS';
response.PhysicalResourceId = result.RuleSetName;
response.Data.RuleSetName = result.RuleSetName;
return true;
})
.catch(function (exception) {
self.log('exception', JSON.stringify(exception));
response.Status = 'FAILED';
response.Reason = exception.message;
return false;
});
};
Lambda.prototype.WarmUpFirehose = function (event, response) {
'use strict';
var options, promise, self = this;
options = event.ResourceProperties.Options || event.ResourceProperties;
if (event.RequestType === 'Create') {
promise = this.firehose.putRecord({
DeliveryStreamName: options.DeliveryStreamName,
Record: {
Data: 'Warming Up Firehose'
}
}).promise();
} else if (event.RequestType === 'Delete'
|| event.RequestType === 'Update') {
// do nothing
promise = Promise.resolve(options);
} else {
promise = Promise.reject(new Error('RequestType Not Implemented'));
}
response.PhysicalResourceId = options.DeliveryStreamName + '-WarmUpFirehose';
return promise
.then(function (result) {
self.log('result', JSON.stringify(result));
response.Status = 'SUCCESS';
return true;
})
.catch(function (exception) {
self.log('expection', exception, exception.stack);
response.Status = 'FAILED';
response.Reason = exception.message;
return false;
});
};
module.exports = Lambda;