aws cloudformation start stop instance

AWSTemplateFormatVersion: ‘2010-09-09’
Description: ‘Lambda function to start and stop an instance based on a schedule’
Parameters:
InstanceIds:
Type: ‘List<AWS::EC2::Instance::Id>’
Description: ‘The instances to start/stop on a schedule’
StartCron:
Type: String
Description: ‘The schedule expression to start the instance’
StopCron:
Type: String
Description: ‘The schedule expression to stop the instance’
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:
– Effect: Allow
Action:
– ‘logs:*’
Resource: ‘arn:aws:logs:*:*:*’
– Effect: Allow
Action:
– ‘ec2:StopInstances’
– ‘ec2:StartInstances’
Resource: !Sub ‘arn:aws:ec2:${AWS::Region}:${AWS::AccountId}:instance/*’
StartFunction:
Type: ‘AWS::Lambda::Function’
Properties:
Handler: index.handler
Role: !GetAtt [FunctionRole, Arn]
Code:
ZipFile: !Sub
– |
‘use strict’;

var AWS = require(‘aws-sdk’);
var ec2 = new AWS.EC2({region: process.env.AWS_REGION});

exports.handler = function (event, context, callback) {
var params = {
InstanceIds: ‘${Instances}’.split(‘,’)
};
ec2.startInstances(params, function (err, data) {
if (err) {
callback(err, err.stack);
} else {
callback(null, data);
}
});
};
– Instances: !Join [“,”, !Ref InstanceIds]
Runtime: nodejs4.3
Timeout: ’30’
StartRule:
Type: ‘AWS::Events::Rule’
Properties:
ScheduleExpression:
Ref: StartCron
Targets:
– Id: StartInstanceScheduler
Arn: !GetAtt [StartFunction, Arn]
StartInvokeLambdaPermission:
Type: ‘AWS::Lambda::Permission’
Properties:
FunctionName: !GetAtt [StartFunction, Arn]
Action: ‘lambda:InvokeFunction’
Principal: events.amazonaws.com
SourceArn: !GetAtt [StartRule, Arn]
StopFunction:
Type: ‘AWS::Lambda::Function’
Properties:
Handler: index.handler
Role: !GetAtt [FunctionRole, Arn]
Code:
ZipFile: !Sub
– |
‘use strict’;

var AWS = require(‘aws-sdk’);
var ec2 = new AWS.EC2({region: process.env.AWS_REGION});

exports.handler = function (event, context, callback) {
var params = {
InstanceIds: ‘${Instances}’.split(‘,’)
};
ec2.stopInstances(params, function (err, data) {
if (err) {
callback(err, err.stack);
} else {
callback(null, data);
}
});
};
– Instances: !Join [“,”, !Ref InstanceIds]
Runtime: nodejs4.3
Timeout: ’30’
StopRule:
Type: ‘AWS::Events::Rule’
Properties:
ScheduleExpression:
Ref: StopCron
Targets:
– Id: StopInstanceScheduler
Arn: !GetAtt [StopFunction, Arn]
StopInvokeLambdaPermission:
Type: ‘AWS::Lambda::Permission’
Properties:
FunctionName: !GetAtt [StopFunction, Arn]
Action: ‘lambda:InvokeFunction’
Principal: events.amazonaws.com
SourceArn: !GetAtt [StopRule, Arn]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s