Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom cloudformation resource for scale up adustment #249

Merged
merged 12 commits into from
Jul 9, 2018
59 changes: 58 additions & 1 deletion lib/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ module.exports = (options = {}) => {
MetricAggregationType: 'Average',
StepAdjustments: [
{
ScalingAdjustment: Math.ceil(options.maxSize / 10),
ScalingAdjustment: cf.getAtt(prefixed('CustomScalingResource'), 'ScalingAdjustment'),
MetricIntervalLowerBound: 0.0
}
]
Expand Down Expand Up @@ -664,6 +664,63 @@ module.exports = (options = {}) => {
}
};

Resources[prefixed('ScalingRole')] = {
Type: 'AWS::IAM::Role',
Properties: {
AssumeRolePolicyDocument: {
Statement: [
{
Effect: 'Allow',
Principal: { Service: ['lambda.amazonaws.com'] },
Action: ['sts:AssumeRole']
}
]
},
Policies: [{
PolicyName: 'CustomcfnScalingLambdaLogs',
PolicyDocument: {
Statement: [
{
Effect: 'Allow',
Action: [
'logs:*'
],
Resource: 'arn:aws:logs:*:*:*'
}
]
}
}]
}
},

Resources[prefixed('ScalingLambda')] = {
Type: 'AWS::Lambda::Function',
Properties: {
Handler: 'index.handler',
Role: cf.getAtt(prefixed('ScalingRole'), 'Arn'),
Code: {
ZipFile: cf.sub(`
var response = require('cfn-response');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oooooh good find.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick var => const?

exports.handler = function(event,context){
var result = Math.round(Math.max(Math.min(parseInt(event.ResourceProperties.maxSize) / 10, 100), 1));
response.send(event, context, response.SUCCESS, { ScalingAdjustment: result });
}
`)
},
Runtime: 'nodejs6.10'
}
};

Resources[prefixed('CustomScalingResource')] = {
Type: 'AWS::CloudFormation::CustomResource',
Properties: {
ServiceToken: cf.getAtt(prefixed('ScalingLambda'), 'Arn'),
maxSize: options.maxSize
}
};



const ref = {
logGroup: cf.ref(prefixed('LogGroup')),
topic: cf.ref(prefixed('Topic')),
Expand Down
Loading