1
- const https = require ( 'https' ) ;
1
+ const https = require ( 'https' )
2
2
const AWS = require ( '@serverless/aws-sdk-extra' )
3
3
const { equals, not, pick } = require ( 'ramda' )
4
4
const { readFile } = require ( 'fs-extra' )
5
5
6
6
const agent = new https . Agent ( {
7
- keepAlive : true ,
8
- } ) ;
7
+ keepAlive : true
8
+ } )
9
9
10
10
/**
11
11
* Sleep
@@ -28,9 +28,9 @@ const randomId = Math.random()
28
28
const getClients = ( credentials = { } , region ) => {
29
29
AWS . config . update ( {
30
30
httpOptions : {
31
- agent,
32
- } ,
33
- } ) ;
31
+ agent
32
+ }
33
+ } )
34
34
35
35
const extras = new AWS . Extras ( { credentials, region } )
36
36
const iam = new AWS . IAM ( { credentials, region } )
@@ -45,12 +45,12 @@ const getClients = (credentials = {}, region) => {
45
45
* @param {* } instance
46
46
*/
47
47
const prepareInputs = ( inputs , instance ) => {
48
-
49
48
return {
50
- name :
51
- inputs . name || instance . state . name || `${ instance . name } -${ instance . stage } -${ randomId } ` ,
49
+ name : inputs . name || instance . state . name || `${ instance . name } -${ instance . stage } -${ randomId } ` ,
52
50
roleName : inputs . roleName ,
53
- description : inputs . description || `An AWS Lambda function from the AWS Lambda Serverless Framework Component. Name: "${ instance . name } " Stage: "${ instance . stage } "` ,
51
+ description :
52
+ inputs . description ||
53
+ `An AWS Lambda function from the AWS Lambda Serverless Framework Component. Name: "${ instance . name } " Stage: "${ instance . stage } "` ,
54
54
memory : inputs . memory || 1028 ,
55
55
timeout : inputs . timeout || 10 ,
56
56
src : inputs . src || null ,
@@ -76,46 +76,44 @@ const createOrUpdateFunctionRole = async (instance, inputs, clients) => {
76
76
if ( inputs . roleName ) {
77
77
console . log (
78
78
`Verifying the provided IAM Role with the name: ${ inputs . roleName } in the inputs exists...`
79
- ) ;
79
+ )
80
80
81
- const userRole = await clients . extras . getRole ( { roleName : inputs . roleName } ) ;
82
- const userRoleArn = userRole && userRole . Role && userRole . Role . Arn ? userRole . Role . Arn : null ; // Don't save user provided role to state, always reference it as an input, in case it changes
81
+ const userRole = await clients . extras . getRole ( { roleName : inputs . roleName } )
82
+ const userRoleArn = userRole && userRole . Role && userRole . Role . Arn ? userRole . Role . Arn : null // Don't save user provided role to state, always reference it as an input, in case it changes
83
83
84
84
// If user role exists, save it to state so it can be used for the create/update lambda logic later
85
85
if ( userRoleArn ) {
86
- console . log ( `The provided IAM Role with the name: ${ inputs . roleName } in the inputs exists.` ) ;
87
- instance . state . userRoleArn = userRoleArn ;
86
+ console . log ( `The provided IAM Role with the name: ${ inputs . roleName } in the inputs exists.` )
87
+ instance . state . userRoleArn = userRoleArn
88
88
89
89
// Save AWS Account ID by fetching the role ID
90
90
// TODO: This may not work with cross-account roles.
91
- instance . state . awsAccountId = instance . state . userRoleArn . split ( ':' ) [ 4 ] ;
91
+ instance . state . awsAccountId = instance . state . userRoleArn . split ( ':' ) [ 4 ]
92
92
93
93
// Be sure to delete defaultLambdaRoleArn data, if it exists
94
94
if ( instance . state . defaultLambdaRoleArn ) {
95
95
delete instance . state . defaultLambdaRoleArn
96
96
}
97
97
} else {
98
- throw new Error (
99
- `The provided IAM Role with the name: ${ inputs . roleName } could not be found.`
100
- ) ;
98
+ throw new Error ( `The provided IAM Role with the name: ${ inputs . roleName } could not be found.` )
101
99
}
102
100
} else {
103
101
// Create a default role with basic Lambda permissions
104
102
105
- const defaultLambdaRoleName = `${ inputs . name } -lambda-role` ;
103
+ const defaultLambdaRoleName = `${ inputs . name } -lambda-role`
106
104
console . log (
107
105
`IAM Role not found. Creating or updating a default role with the name: ${ defaultLambdaRoleName } `
108
- ) ;
106
+ )
109
107
110
108
const result = await clients . extras . deployRole ( {
111
109
roleName : defaultLambdaRoleName ,
112
110
service : [ 'lambda.amazonaws.com' ] ,
113
- policy : 'arn:aws:iam::aws:policy/AWSLambdaFullAccess' ,
114
- } ) ;
111
+ policy : 'arn:aws:iam::aws:policy/AWSLambdaFullAccess'
112
+ } )
115
113
116
- instance . state . defaultLambdaRoleName = defaultLambdaRoleName ;
117
- instance . state . defaultLambdaRoleArn = result . roleArn ;
118
- instance . state . awsAccountId = instance . state . defaultLambdaRoleArn . split ( ':' ) [ 4 ] ;
114
+ instance . state . defaultLambdaRoleName = defaultLambdaRoleName
115
+ instance . state . defaultLambdaRoleArn = result . roleArn
116
+ instance . state . awsAccountId = instance . state . defaultLambdaRoleArn . split ( ':' ) [ 4 ]
119
117
120
118
// Be sure to delete userRole data, if it exists
121
119
if ( instance . state . userRoleArn ) {
@@ -124,30 +122,30 @@ const createOrUpdateFunctionRole = async (instance, inputs, clients) => {
124
122
125
123
console . log (
126
124
`Default Lambda IAM Role created or updated with ARN ${ instance . state . defaultLambdaRoleArn } `
127
- ) ;
125
+ )
128
126
}
129
- } ;
127
+ }
130
128
131
129
/*
132
130
* Ensure the Meta IAM Role exists
133
131
*/
134
132
const createOrUpdateMetaRole = async ( instance , inputs , clients , serverlessAccountId ) => {
135
133
// Create or update Meta Role for monitoring and more, if option is enabled. It's enabled by default.
136
134
if ( inputs . monitoring || typeof inputs . monitoring === 'undefined' ) {
137
- console . log ( 'Creating or updating the meta IAM Role...' ) ;
135
+ console . log ( 'Creating or updating the meta IAM Role...' )
138
136
139
- const roleName = `${ instance . name } -meta-role` ;
137
+ const roleName = `${ instance . name } -meta-role`
140
138
141
139
const assumeRolePolicyDocument = {
142
140
Version : '2012-10-17' ,
143
141
Statement : {
144
142
Effect : 'Allow' ,
145
143
Principal : {
146
- AWS : `arn:aws:iam::${ serverlessAccountId } :root` , // Serverless's Components account
144
+ AWS : `arn:aws:iam::${ serverlessAccountId } :root` // Serverless's Components account
147
145
} ,
148
- Action : 'sts:AssumeRole' ,
149
- } ,
150
- } ;
146
+ Action : 'sts:AssumeRole'
147
+ }
148
+ }
151
149
152
150
// Create a policy that only can access APIGateway and Lambda metrics, logs from CloudWatch...
153
151
const policy = {
@@ -164,8 +162,8 @@ const createOrUpdateMetaRole = async (instance, inputs, clients, serverlessAccou
164
162
'logs:List*' ,
165
163
'logs:Describe*' ,
166
164
'logs:TestMetricFilter' ,
167
- 'logs:FilterLogEvents' ,
168
- ] ,
165
+ 'logs:FilterLogEvents'
166
+ ]
169
167
// TODO: Finish this. Haven't been able to get this to work. Perhaps there is a missing service (Cloudfront?)
170
168
// Condition: {
171
169
// StringEquals: {
@@ -175,25 +173,25 @@ const createOrUpdateMetaRole = async (instance, inputs, clients, serverlessAccou
175
173
// ]
176
174
// }
177
175
// }
178
- } ,
179
- ] ,
180
- } ;
176
+ }
177
+ ]
178
+ }
181
179
182
- const roleDescription = `The Meta Role for the Serverless Framework App: ${ instance . name } Stage: ${ instance . stage } ` ;
180
+ const roleDescription = `The Meta Role for the Serverless Framework App: ${ instance . name } Stage: ${ instance . stage } `
183
181
184
182
const result = await clients . extras . deployRole ( {
185
183
roleName,
186
184
roleDescription,
187
185
policy,
188
- assumeRolePolicyDocument,
189
- } ) ;
186
+ assumeRolePolicyDocument
187
+ } )
190
188
191
- instance . state . metaRoleName = roleName ;
192
- instance . state . metaRoleArn = result . roleArn ;
189
+ instance . state . metaRoleName = roleName
190
+ instance . state . metaRoleArn = result . roleArn
193
191
194
- console . log ( `Meta IAM Role created or updated with ARN ${ instance . state . metaRoleArn } ` ) ;
192
+ console . log ( `Meta IAM Role created or updated with ARN ${ instance . state . metaRoleArn } ` )
195
193
}
196
- } ;
194
+ }
197
195
198
196
/**
199
197
* Create a new lambda function
@@ -258,17 +256,17 @@ const updateLambdaFunctionConfig = async (instance, lambda, inputs) => {
258
256
} ,
259
257
...( inputs . securityGroupIds
260
258
? {
261
- VpcConfig : {
262
- SecurityGroupIds : inputs . securityGroupIds ,
263
- SubnetIds : inputs . subnetIds
259
+ VpcConfig : {
260
+ SecurityGroupIds : inputs . securityGroupIds ,
261
+ SubnetIds : inputs . subnetIds
262
+ }
264
263
}
265
- }
266
264
: {
267
- VpcConfig : {
268
- SecurityGroupIds : [ ] ,
269
- SubnetIds : [ ]
270
- }
271
- } )
265
+ VpcConfig : {
266
+ SecurityGroupIds : [ ] ,
267
+ SubnetIds : [ ]
268
+ }
269
+ } )
272
270
}
273
271
274
272
const res = await lambda . updateFunctionConfiguration ( functionConfigParams ) . promise ( )
@@ -399,75 +397,68 @@ const inputsChanged = (prevLambda, lambda) => {
399
397
const removeAllRoles = async ( instance , clients ) => {
400
398
// Delete Function Role
401
399
if ( instance . state . defaultLambdaRoleName ) {
402
- console . log ( 'Deleting the default Function Role...' ) ;
400
+ console . log ( 'Deleting the default Function Role...' )
403
401
await clients . extras . removeRole ( {
404
- roleName : instance . state . defaultLambdaRoleName ,
405
- } ) ;
402
+ roleName : instance . state . defaultLambdaRoleName
403
+ } )
406
404
}
407
405
408
406
// Delete Meta Role
409
407
if ( instance . state . metaRoleName ) {
410
- console . log ( 'Deleting the Meta Role...' ) ;
408
+ console . log ( 'Deleting the Meta Role...' )
411
409
await clients . extras . removeRole ( {
412
- roleName : instance . state . metaRoleName ,
413
- } ) ;
410
+ roleName : instance . state . metaRoleName
411
+ } )
414
412
}
415
- } ;
416
-
413
+ }
417
414
418
415
/**
419
416
* Get metrics from cloudwatch
420
417
* @param {* } clients
421
418
* @param {* } rangeStart MUST be a moment() object
422
419
* @param {* } rangeEnd MUST be a moment() object
423
420
*/
424
- const getMetrics = async (
425
- region ,
426
- metaRoleArn ,
427
- functionName ,
428
- rangeStart ,
429
- rangeEnd
430
- ) => {
421
+ const getMetrics = async ( region , metaRoleArn , functionName , rangeStart , rangeEnd ) => {
431
422
/**
432
423
* Create AWS STS Token via the meta role that is deployed with the Express Component
433
424
*/
434
425
435
426
// Assume Role
436
- const assumeParams = { } ;
437
- assumeParams . RoleSessionName = `session${ Date . now ( ) } ` ;
438
- assumeParams . RoleArn = metaRoleArn ;
439
- assumeParams . DurationSeconds = 900 ;
427
+ const assumeParams = { }
428
+ assumeParams . RoleSessionName = `session${ Date . now ( ) } `
429
+ assumeParams . RoleArn = metaRoleArn
430
+ assumeParams . DurationSeconds = 900
440
431
441
432
const sts = new AWS . STS ( { region } )
442
- const resAssume = await sts . assumeRole ( assumeParams ) . promise ( ) ;
433
+ const resAssume = await sts . assumeRole ( assumeParams ) . promise ( )
443
434
444
- const roleCreds = { } ;
445
- roleCreds . accessKeyId = resAssume . Credentials . AccessKeyId ;
446
- roleCreds . secretAccessKey = resAssume . Credentials . SecretAccessKey ;
447
- roleCreds . sessionToken = resAssume . Credentials . SessionToken ;
435
+ const roleCreds = { }
436
+ roleCreds . accessKeyId = resAssume . Credentials . AccessKeyId
437
+ roleCreds . secretAccessKey = resAssume . Credentials . SecretAccessKey
438
+ roleCreds . sessionToken = resAssume . Credentials . SessionToken
448
439
449
440
/**
450
441
* Instantiate a new Extras instance w/ the temporary credentials
451
442
*/
452
443
453
444
const extras = new AWS . Extras ( {
454
445
credentials : roleCreds ,
455
- region,
446
+ region
456
447
} )
457
448
458
449
const resources = [
459
450
{
460
451
type : 'aws_lambda' ,
461
- functionName,
462
- } ,
463
- ] ;
452
+ functionName
453
+ }
454
+ ]
464
455
465
456
return await extras . getMetrics ( {
466
457
rangeStart,
467
458
rangeEnd,
468
- resources,
469
- } ) ;
470
- } ;
459
+ resources
460
+ } )
461
+ }
471
462
472
463
/**
473
464
* Exports
@@ -484,5 +475,5 @@ module.exports = {
484
475
inputsChanged,
485
476
deleteLambdaFunction,
486
477
removeAllRoles,
487
- getMetrics,
478
+ getMetrics
488
479
}
0 commit comments