Skip to content

Commit

Permalink
Fix bug and change default behavior (#41)
Browse files Browse the repository at this point in the history
* Format according to prettier

* Fix bug where zero forces defaults

* Include test

* Change scaleInCooldown to zero

* Update version and readme.
  • Loading branch information
claydanford authored Mar 11, 2022
1 parent d13699f commit d05621c
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 32 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ functions:
maximum: 10
minimum: 1
usage: 0.75
scaleInCooldown: 120
scaleInCooldown: 0
scaleOutCooldown: 0
customMetric:
statistic: maximum
Expand Down Expand Up @@ -103,10 +103,12 @@ alias: provisioned
maximum: 10
minimum: 1
usage: 0.75
scaleInCooldown: 120
scaleInCooldown: 0
scaleOutCooldown: 0
```

**Change in Default Behavior:** Starting in v1.7.0, the default scaleInCooldown is zero, not 120. This is backwards compatible, but different default behavior. This is in line with AWS' default scaleInCooldown.

### Scheduled Actions

For more details on Scheduled Actions formats see
Expand Down
2 changes: 1 addition & 1 deletion __tests__/aws/policy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('Policy', () => {
const policy = new Policy(options, {
function: 'foo',
name: 'foo-svc-dev-foo',
scaleInCooldown: 120,
scaleInCooldown: 0,
scaleOutCooldown: 0,
usage: 0.75,
})
Expand Down
7 changes: 6 additions & 1 deletion __tests__/helpers/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@ export const configPartial: AutoscalingConfig = {
usage: 0.92,
}

export const configZero: AutoscalingConfig = {
...configMin,
scaleInCooldown: 0
}

export const configDefault: AutoscalingConfig = {
...configMin,
minimum: 1,
maximum: 10,
usage: 0.75,
scaleInCooldown: 120,
scaleInCooldown: 0,
scaleOutCooldown: 0,
alias: 'provisioned',
}
Expand Down
2 changes: 1 addition & 1 deletion __tests__/helpers/policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const expectedPolicy = {
PredefinedMetricSpecification: {
PredefinedMetricType: 'LambdaProvisionedConcurrencyUtilization',
},
ScaleInCooldown: 120,
ScaleInCooldown: 0,
ScaleOutCooldown: 0,
TargetValue: 0.75,
},
Expand Down
22 changes: 15 additions & 7 deletions __tests__/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import {
configPartial,
configCustomMetricDefault,
configCustomMetricMin,
configScheduledActions
configScheduledActions,
configZero,
} from './helpers/config'
import { serverless } from './helpers/serverless'
import { options } from './helpers/options'
import { expectedPolicy } from './helpers/policy'
import {
expectedTarget,
expectedTargetWithSingleScheduledAction
expectedTargetWithSingleScheduledAction,
} from './helpers/target'
import { ConcurrencyFunction } from 'src/@types'

Expand All @@ -37,6 +38,10 @@ describe('Defaults', () => {
})
})

it('should allow zero', () => {
expect(plugin.defaults(configZero).scaleInCooldown).toEqual(0)
})

it('should set custom metric config defaults', () => {
expect(plugin.defaults(configCustomMetricMin)).toEqual(
configCustomMetricDefault,
Expand Down Expand Up @@ -154,20 +159,22 @@ describe('Process', () => {
it('Process for cloudformation object', () => {
plugin.process([configDefault])
expect(
plugin.serverless.service.provider.compiledCloudFormationTemplate.Resources
plugin.serverless.service.provider.compiledCloudFormationTemplate
.Resources,
).toEqual({
...expectedPolicy,
...expectedTarget
...expectedTarget,
})
})

it('Process for CloudFormation with Scheduled Actions', (): void => {
plugin.process([configScheduledActions])
expect(
plugin.serverless.service.provider.compiledCloudFormationTemplate.Resources
plugin.serverless.service.provider.compiledCloudFormationTemplate
.Resources,
).toEqual({
...expectedPolicy,
...expectedTargetWithSingleScheduledAction
...expectedTargetWithSingleScheduledAction,
})
})
})
Expand All @@ -178,7 +185,8 @@ describe('BeforeDeployResources', () => {

plugin.beforeDeployResources()
expect(
plugin.serverless.service.provider.compiledCloudFormationTemplate.Resources,
plugin.serverless.service.provider.compiledCloudFormationTemplate
.Resources,
).toEqual({
...expectedPolicy,
...expectedTarget,
Expand Down
5 changes: 3 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "serverless-provisioned-concurrency-autoscaling",
"version": "1.6.0",
"version": "1.7.0",
"description": "Serverless Plugin for AWS Lambdas Provisioned Concurrency Auto Scaling Configuration.",
"main": "./lib/index.js",
"files": [
Expand Down
6 changes: 1 addition & 5 deletions src/aws/policy.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import Name from '../name'
import {
Options,
AutoscalingConfig,
CustomMetricConfig,
} from 'src/@types'
import { Options, AutoscalingConfig, CustomMetricConfig } from 'src/@types'
import { ucfirst } from '../utility'

export default class Policy {
Expand Down
15 changes: 9 additions & 6 deletions src/aws/target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@ export default class Target {
}

private getSchedulesActions(): unknown[] {
return this.data.scheduledActions?.map(scheduledAction => { return {
return this.data.scheduledActions?.map((scheduledAction) => {
return {
EndTime: scheduledAction.endTime,
StartTime: scheduledAction.startTime,
Timezone: scheduledAction.timezone,
ScalableTargetAction: {
MaxCapacity: scheduledAction.action.maximum,
MinCapacity: scheduledAction.action.minimum
MinCapacity: scheduledAction.action.minimum,
},
ScheduledActionName: scheduledAction.name, // todo: names cannot be duplicated; add validation
Schedule: scheduledAction.schedule
}}) as unknown[]
Schedule: scheduledAction.schedule,
}
}) as unknown[]
}

toJSON(): Record<string, unknown> {
Expand All @@ -46,11 +48,12 @@ export default class Target {
ScalableDimension: 'lambda:function:ProvisionedConcurrency',
ServiceNamespace: 'lambda',
RoleARN: {
'Fn::Sub': 'arn:aws:iam::${AWS::AccountId}:role/aws-service-role/lambda.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_LambdaConcurrency',
'Fn::Sub':
'arn:aws:iam::${AWS::AccountId}:role/aws-service-role/lambda.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_LambdaConcurrency',
},
ScheduledActions: this.data.scheduledActions
? this.getSchedulesActions()
: undefined
: undefined,
},
Type: 'AWS::ApplicationAutoScaling::ScalableTarget',
},
Expand Down
18 changes: 12 additions & 6 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,19 @@ export default class Plugin {
alias,
name: config.name,
function: config.function,
usage: config.usage || 0.75,
minimum: config.minimum || 1,
maximum: config.maximum || 10,
scaleInCooldown: config.scaleInCooldown || 120,
scaleOutCooldown: config.scaleOutCooldown || 0,
usage: typeof config.usage !== 'undefined' ? config.usage : 0.75,
minimum: typeof config.minimum !== 'undefined' ? config.minimum : 1,
maximum: typeof config.maximum !== 'undefined' ? config.maximum : 10,
scaleInCooldown:
typeof config.scaleInCooldown !== 'undefined'
? config.scaleInCooldown
: 0,
scaleOutCooldown:
typeof config.scaleOutCooldown !== 'undefined'
? config.scaleOutCooldown
: 0,
customMetric: config.customMetric ? customMetricConfig : undefined,
scheduledActions: config.scheduledActions
scheduledActions: config.scheduledActions,
}
}

Expand Down

0 comments on commit d05621c

Please sign in to comment.