forked from alertlogic/al-aws-collector-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathal_aws.js
190 lines (172 loc) · 5.63 KB
/
al_aws.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* -----------------------------------------------------------------------------
* @copyright (C) 2017, Alert Logic, Inc
* @doc
*
* Helper class for lambda function utility and helper methods.
*
* @end
* -----------------------------------------------------------------------------
*/
'use strict';
const AWS = require('aws-sdk');
const moment = require('moment');
const async = require('async');
const AWS_STATISTICS_PERIOD_MINUTES = 15;
const MAX_ERROR_MSG_LEN = 1024;
var selfUpdate = function (callback) {
var params = {
FunctionName: process.env.AWS_LAMBDA_FUNCTION_NAME,
S3Bucket: process.env.aws_lambda_s3_bucket,
S3Key: process.env.aws_lambda_zipfile_name
};
var lambda = new AWS.Lambda();
console.info('Performing lambda self-update with params: ', JSON.stringify(params));
lambda.updateFunctionCode(params, function(err, data) {
if (err) {
console.info('Lambda self-update error: ', err);
} else {
console.info('Lambda self-update successful. Data: ' + data);
}
return callback(err);
});
};
var getS3ConfigChanges = function(callback) {
var s3 = new AWS.S3();
var params = {
Bucket: process.env.aws_lambda_s3_bucket,
Key: process.env.aws_lambda_update_config_name
};
s3.getObject(params, function(err, object) {
if (err) {
return callback(err);
} else {
try {
let config = JSON.parse(object.Body.toString());
return callback(null, config);
} catch(ex) {
return callback('Unable to parse config changes.')
}
}
});
};
var getLambdaConfig = function(callback) {
var lambda = new AWS.Lambda();
var params = {
FunctionName: process.env.AWS_LAMBDA_FUNCTION_NAME
};
lambda.getFunctionConfiguration(params, callback);
};
var updateLambdaConfig = function(config, callback) {
var lambda = new AWS.Lambda();
lambda.updateFunctionConfiguration(config, callback);
};
//DEPRECATED FUNCTION
//please use statistics_templates.js instead
var getMetricStatistics = function (params, statistics, callback) {
var cloudwatch = new AWS.CloudWatch({apiVersion: '2010-08-01'});
cloudwatch.getMetricStatistics(params, function(err, data) {
if (err) {
statistics.push({
Label: params.MetricName,
StatisticsError: JSON.stringify(err).slice(0, MAX_ERROR_MSG_LEN)
});
} else {
statistics.push({
Label: data.Label,
Datapoints: data.Datapoints
});
}
return callback(null, statistics);
});
};
//DEPRECATED FUNCTION
//please use statistics_templates.js instead
var getLambdaMetrics = function (functionName, metricName, statistics, callback) {
var params = {
Dimensions: [
{
Name: 'FunctionName',
Value: functionName
}
],
MetricName: metricName,
Namespace: 'AWS/Lambda',
Statistics: ['Sum'],
StartTime: moment().subtract(AWS_STATISTICS_PERIOD_MINUTES, 'minutes').toISOString(),
EndTime: new Date(),
Period: 60*AWS_STATISTICS_PERIOD_MINUTES /* 15 mins as seconds */
};
return getMetricStatistics(params, statistics, callback);
};
//DEPRECATED FUNCTION
//please use statistics_templates.js instead
var getKinesisMetrics = function (streamName, metricName, statistics, callback) {
var params = {
Dimensions: [
{
Name: 'StreamName',
Value: streamName
}
],
MetricName: metricName,
Namespace: 'AWS/Kinesis',
Statistics: ['Sum'],
StartTime: moment().subtract(AWS_STATISTICS_PERIOD_MINUTES, 'minutes').toISOString(),
EndTime: new Date(),
Period: 60*AWS_STATISTICS_PERIOD_MINUTES /* 15 mins as seconds */
};
return getMetricStatistics(params, statistics, callback);
};
var arnToName = function (arn) {
const parsedArn = arn.split(':');
if (parsedArn.length > 3) {
const parsedId = parsedArn[parsedArn.length-1].split('/');
return parsedId[parsedId.length-1];
} else {
return undefined;
}
};
var arnToAccId = function (arn) {
const parsedArn = arn.split(':');
if (parsedArn.length > 4) {
return parsedArn[4];
} else {
return undefined;
}
};
var setEnv = function(vars, callback) {
const lambda = new AWS.Lambda();
const getConfigParams = {
FunctionName: process.env.AWS_LAMBDA_FUNCTION_NAME
};
lambda.getFunctionConfiguration(getConfigParams, (err, config) => {
if(err){
console.error('Error getting function config, environment variables were not updated', err);
return callback(err);
}
var params = {
FunctionName: process.env.AWS_LAMBDA_FUNCTION_NAME,
Environment : {
Variables : {
...config.Environment.Variables,
...vars
}
}
};
return lambda.updateFunctionConfiguration(params, callback);
});
};
module.exports = {
selfUpdate : selfUpdate,
getS3ConfigChanges : getS3ConfigChanges,
updateLambdaConfig : updateLambdaConfig,
getLambdaConfig : getLambdaConfig,
arnToName : arnToName,
arnToAccId : arnToAccId,
setEnv : setEnv,
//DEPRECATED FUNCTIONS
//please use statistics_templates.js instead
getMetricStatistics : getMetricStatistics,
getLambdaMetrics : getLambdaMetrics,
getKinesisMetrics : getKinesisMetrics,
};