Skip to content
This repository has been archived by the owner on Jun 27, 2019. It is now read-only.

Commit

Permalink
add cron lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
hoegertn committed Dec 27, 2016
1 parent c137eb0 commit 6a30940
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,20 @@ yo cloudformation:<name>

Generate a CloudFormation template that runs a web application as Docker containers on CoreOS instances.

### cronjob

Generate a CloudFormation template that deploys a AWS Lambda function that is called according to the schedule expression.

## License

Apache-2.0 © [Taimos GmbH](https://www.taimos.de)

## Changelog

### 0.2.0

* add scheduled Lambda generator

### 0.1.0

* Initial version with docker subgenerator
Expand Down
44 changes: 44 additions & 0 deletions generators/cronjob/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict';
var Generator = require('yeoman-generator');
var chalk = require('chalk');
var yosay = require('yosay');

module.exports = Generator.extend({
prompting: function () {
this.log(yosay(
'Welcome to the terrific ' + chalk.red('generator-cloudformation') + ' generator for scheduled lambdas!'
));

var prompts = [{
type: 'input',
name: 'description',
message: 'The description of the lambda?'
}, {
type: 'input',
name: 'expression',
message: 'The schedule expression?',
default: 'rate(1 day)'
}, {
type: 'input',
name: 'timeout',
message: 'The lambda execution timeout in seconds?',
default: '30'
}];

return this.prompt(prompts).then(function (props) {
this.props = props;
}.bind(this));
},

writing: function () {
this.fs.copyTpl(
this.templatePath('cfn.yaml'),
this.destinationPath('cfn.yaml'),
this.props
);
},

install: function () {
//
}
});
61 changes: 61 additions & 0 deletions generators/cronjob/templates/cfn.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
AWSTemplateFormatVersion: '2010-09-09'
Description: '<%= description %>'
Resources:
FunctionRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action: 'sts:AssumeRole'
Path: /
Policies:
- PolicyName: root
PolicyDocument:
Version: '2012-10-17'
Statement:
# TODO Insert needed permissions here
- Effect: Allow
Action:
- 'logs:CreateLogGroup'
- 'logs:CreateLogStream'
- 'logs:PutLogEvents'
Resource: 'arn:aws:logs:*:*:*'
Function:
Type: 'AWS::Lambda::Function'
Properties:
Handler: index.handler
Role: !GetAtt FunctionRole.Arn
Code:
ZipFile: !Sub |
"use strict";
var AWS = require('aws-sdk');
exports.handler = function (event, context, callback) {
CallSomePromiseHere.then(function () {
callback(null, 'Successfully finished');
}).catch(function (err) {
callback(err);
});
};
Runtime: nodejs4.3
Timeout: '<%= timeout %>'
CronRule:
Type: 'AWS::Events::Rule'
Properties:
ScheduleExpression: '<%= expression %>'
Targets:
- Id: LambdaScheduler
Arn: !GetAtt Function.Arn
InvokeLambdaPermission:
Type: 'AWS::Lambda::Permission'
Properties:
FunctionName: !GetAtt Function.Arn
Action: 'lambda:InvokeFunction'
Principal: events.amazonaws.com
SourceArn: !GetAtt CronRule.Arn
18 changes: 18 additions & 0 deletions test/cronjob.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';
var path = require('path');
var assert = require('yeoman-assert');
var helpers = require('yeoman-test');

describe('generator-cloudformation:cronjob', function () {
before(function () {
return helpers.run(path.join(__dirname, '../generators/cronjob'))
.withPrompts({someAnswer: true})
.toPromise();
});

it('creates files', function () {
assert.file([
'cfn.yaml'
]);
});
});

0 comments on commit 6a30940

Please sign in to comment.