Skip to content

Commit d9620ee

Browse files
committed
change filename
1 parent bfbedc8 commit d9620ee

File tree

4 files changed

+92
-102
lines changed

4 files changed

+92
-102
lines changed
File renamed without changes.

src/serverless.js

+11-10
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const {
1212
createOrUpdateMetaRole,
1313
deleteLambdaFunction,
1414
removeAllRoles,
15-
getMetrics,
15+
getMetrics
1616
} = require('./utils')
1717

1818
class AwsLambda extends Component {
@@ -59,8 +59,8 @@ class AwsLambda extends Component {
5959

6060
await Promise.all([
6161
createOrUpdateFunctionRole(this, inputs, clients),
62-
createOrUpdateMetaRole(this, inputs, clients, this.accountId),
63-
]);
62+
createOrUpdateMetaRole(this, inputs, clients, this.accountId)
63+
])
6464

6565
console.log(
6666
`Checking if an AWS Lambda function has already been created with name: ${inputs.name}`
@@ -114,7 +114,6 @@ class AwsLambda extends Component {
114114
* @param {*} inputs
115115
*/
116116
async remove(inputs = {}) {
117-
118117
// this error message assumes that the user is running via the CLI though...
119118
if (Object.keys(this.credentials.aws).length === 0) {
120119
const msg = `Credentials not found. Make sure you have a .env file in the cwd. - Docs: https://git.io/JvArp`
@@ -126,13 +125,15 @@ class AwsLambda extends Component {
126125
return
127126
}
128127

129-
const clients = getClients(this.credentials.aws, this.state.region);
128+
const clients = getClients(this.credentials.aws, this.state.region)
130129

131-
await removeAllRoles(this, clients);
130+
await removeAllRoles(this, clients)
132131

133132
console.log(`Removing lambda ${this.state.name} from the ${this.state.region} region.`)
134133
await deleteLambdaFunction(clients.lambda, this.state.name)
135-
console.log(`Successfully removed lambda ${this.state.name} from the ${this.state.region} region.`)
134+
console.log(
135+
`Successfully removed lambda ${this.state.name} from the ${this.state.region} region.`
136+
)
136137

137138
this.state = {}
138139
return {}
@@ -144,7 +145,7 @@ class AwsLambda extends Component {
144145
async metrics(inputs = {}) {
145146
// Validate
146147
if (!inputs.rangeStart || !inputs.rangeEnd) {
147-
throw new Error('rangeStart and rangeEnd are require inputs');
148+
throw new Error('rangeStart and rangeEnd are require inputs')
148149
}
149150

150151
const result = await getMetrics(
@@ -153,9 +154,9 @@ class AwsLambda extends Component {
153154
this.state.name,
154155
inputs.rangeStart,
155156
inputs.rangeEnd
156-
);
157+
)
157158

158-
return result;
159+
return result
159160
}
160161
}
161162

src/utils.js

+78-87
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
const https = require('https');
1+
const https = require('https')
22
const AWS = require('@serverless/aws-sdk-extra')
33
const { equals, not, pick } = require('ramda')
44
const { readFile } = require('fs-extra')
55

66
const agent = new https.Agent({
7-
keepAlive: true,
8-
});
7+
keepAlive: true
8+
})
99

1010
/**
1111
* Sleep
@@ -28,9 +28,9 @@ const randomId = Math.random()
2828
const getClients = (credentials = {}, region) => {
2929
AWS.config.update({
3030
httpOptions: {
31-
agent,
32-
},
33-
});
31+
agent
32+
}
33+
})
3434

3535
const extras = new AWS.Extras({ credentials, region })
3636
const iam = new AWS.IAM({ credentials, region })
@@ -45,12 +45,12 @@ const getClients = (credentials = {}, region) => {
4545
* @param {*} instance
4646
*/
4747
const prepareInputs = (inputs, instance) => {
48-
4948
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}`,
5250
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}"`,
5454
memory: inputs.memory || 1028,
5555
timeout: inputs.timeout || 10,
5656
src: inputs.src || null,
@@ -76,46 +76,44 @@ const createOrUpdateFunctionRole = async (instance, inputs, clients) => {
7676
if (inputs.roleName) {
7777
console.log(
7878
`Verifying the provided IAM Role with the name: ${inputs.roleName} in the inputs exists...`
79-
);
79+
)
8080

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
8383

8484
// If user role exists, save it to state so it can be used for the create/update lambda logic later
8585
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
8888

8989
// Save AWS Account ID by fetching the role ID
9090
// 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]
9292

9393
// Be sure to delete defaultLambdaRoleArn data, if it exists
9494
if (instance.state.defaultLambdaRoleArn) {
9595
delete instance.state.defaultLambdaRoleArn
9696
}
9797
} 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.`)
10199
}
102100
} else {
103101
// Create a default role with basic Lambda permissions
104102

105-
const defaultLambdaRoleName = `${inputs.name}-lambda-role`;
103+
const defaultLambdaRoleName = `${inputs.name}-lambda-role`
106104
console.log(
107105
`IAM Role not found. Creating or updating a default role with the name: ${defaultLambdaRoleName}`
108-
);
106+
)
109107

110108
const result = await clients.extras.deployRole({
111109
roleName: defaultLambdaRoleName,
112110
service: ['lambda.amazonaws.com'],
113-
policy: 'arn:aws:iam::aws:policy/AWSLambdaFullAccess',
114-
});
111+
policy: 'arn:aws:iam::aws:policy/AWSLambdaFullAccess'
112+
})
115113

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]
119117

120118
// Be sure to delete userRole data, if it exists
121119
if (instance.state.userRoleArn) {
@@ -124,30 +122,30 @@ const createOrUpdateFunctionRole = async (instance, inputs, clients) => {
124122

125123
console.log(
126124
`Default Lambda IAM Role created or updated with ARN ${instance.state.defaultLambdaRoleArn}`
127-
);
125+
)
128126
}
129-
};
127+
}
130128

131129
/*
132130
* Ensure the Meta IAM Role exists
133131
*/
134132
const createOrUpdateMetaRole = async (instance, inputs, clients, serverlessAccountId) => {
135133
// Create or update Meta Role for monitoring and more, if option is enabled. It's enabled by default.
136134
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...')
138136

139-
const roleName = `${instance.name}-meta-role`;
137+
const roleName = `${instance.name}-meta-role`
140138

141139
const assumeRolePolicyDocument = {
142140
Version: '2012-10-17',
143141
Statement: {
144142
Effect: 'Allow',
145143
Principal: {
146-
AWS: `arn:aws:iam::${serverlessAccountId}:root`, // Serverless's Components account
144+
AWS: `arn:aws:iam::${serverlessAccountId}:root` // Serverless's Components account
147145
},
148-
Action: 'sts:AssumeRole',
149-
},
150-
};
146+
Action: 'sts:AssumeRole'
147+
}
148+
}
151149

152150
// Create a policy that only can access APIGateway and Lambda metrics, logs from CloudWatch...
153151
const policy = {
@@ -164,8 +162,8 @@ const createOrUpdateMetaRole = async (instance, inputs, clients, serverlessAccou
164162
'logs:List*',
165163
'logs:Describe*',
166164
'logs:TestMetricFilter',
167-
'logs:FilterLogEvents',
168-
],
165+
'logs:FilterLogEvents'
166+
]
169167
// TODO: Finish this. Haven't been able to get this to work. Perhaps there is a missing service (Cloudfront?)
170168
// Condition: {
171169
// StringEquals: {
@@ -175,25 +173,25 @@ const createOrUpdateMetaRole = async (instance, inputs, clients, serverlessAccou
175173
// ]
176174
// }
177175
// }
178-
},
179-
],
180-
};
176+
}
177+
]
178+
}
181179

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}`
183181

184182
const result = await clients.extras.deployRole({
185183
roleName,
186184
roleDescription,
187185
policy,
188-
assumeRolePolicyDocument,
189-
});
186+
assumeRolePolicyDocument
187+
})
190188

191-
instance.state.metaRoleName = roleName;
192-
instance.state.metaRoleArn = result.roleArn;
189+
instance.state.metaRoleName = roleName
190+
instance.state.metaRoleArn = result.roleArn
193191

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}`)
195193
}
196-
};
194+
}
197195

198196
/**
199197
* Create a new lambda function
@@ -258,17 +256,17 @@ const updateLambdaFunctionConfig = async (instance, lambda, inputs) => {
258256
},
259257
...(inputs.securityGroupIds
260258
? {
261-
VpcConfig: {
262-
SecurityGroupIds: inputs.securityGroupIds,
263-
SubnetIds: inputs.subnetIds
259+
VpcConfig: {
260+
SecurityGroupIds: inputs.securityGroupIds,
261+
SubnetIds: inputs.subnetIds
262+
}
264263
}
265-
}
266264
: {
267-
VpcConfig: {
268-
SecurityGroupIds: [],
269-
SubnetIds: []
270-
}
271-
})
265+
VpcConfig: {
266+
SecurityGroupIds: [],
267+
SubnetIds: []
268+
}
269+
})
272270
}
273271

274272
const res = await lambda.updateFunctionConfiguration(functionConfigParams).promise()
@@ -399,75 +397,68 @@ const inputsChanged = (prevLambda, lambda) => {
399397
const removeAllRoles = async (instance, clients) => {
400398
// Delete Function Role
401399
if (instance.state.defaultLambdaRoleName) {
402-
console.log('Deleting the default Function Role...');
400+
console.log('Deleting the default Function Role...')
403401
await clients.extras.removeRole({
404-
roleName: instance.state.defaultLambdaRoleName,
405-
});
402+
roleName: instance.state.defaultLambdaRoleName
403+
})
406404
}
407405

408406
// Delete Meta Role
409407
if (instance.state.metaRoleName) {
410-
console.log('Deleting the Meta Role...');
408+
console.log('Deleting the Meta Role...')
411409
await clients.extras.removeRole({
412-
roleName: instance.state.metaRoleName,
413-
});
410+
roleName: instance.state.metaRoleName
411+
})
414412
}
415-
};
416-
413+
}
417414

418415
/**
419416
* Get metrics from cloudwatch
420417
* @param {*} clients
421418
* @param {*} rangeStart MUST be a moment() object
422419
* @param {*} rangeEnd MUST be a moment() object
423420
*/
424-
const getMetrics = async (
425-
region,
426-
metaRoleArn,
427-
functionName,
428-
rangeStart,
429-
rangeEnd
430-
) => {
421+
const getMetrics = async (region, metaRoleArn, functionName, rangeStart, rangeEnd) => {
431422
/**
432423
* Create AWS STS Token via the meta role that is deployed with the Express Component
433424
*/
434425

435426
// 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
440431

441432
const sts = new AWS.STS({ region })
442-
const resAssume = await sts.assumeRole(assumeParams).promise();
433+
const resAssume = await sts.assumeRole(assumeParams).promise()
443434

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
448439

449440
/**
450441
* Instantiate a new Extras instance w/ the temporary credentials
451442
*/
452443

453444
const extras = new AWS.Extras({
454445
credentials: roleCreds,
455-
region,
446+
region
456447
})
457448

458449
const resources = [
459450
{
460451
type: 'aws_lambda',
461-
functionName,
462-
},
463-
];
452+
functionName
453+
}
454+
]
464455

465456
return await extras.getMetrics({
466457
rangeStart,
467458
rangeEnd,
468-
resources,
469-
});
470-
};
459+
resources
460+
})
461+
}
471462

472463
/**
473464
* Exports
@@ -484,5 +475,5 @@ module.exports = {
484475
inputsChanged,
485476
deleteLambdaFunction,
486477
removeAllRoles,
487-
getMetrics,
478+
getMetrics
488479
}

0 commit comments

Comments
 (0)