Start function: Instructions
Stop function: Instructions
a.) Lambda function to Start Instances (start.js):
The Script runs across all AWS regions and makes API Calls
DescribeInstances()
and StartInstances()
, based on applied filters.
- Create a lambda function.
- Add the script to your lambda function.
- Navigate to Event Source.
- Click Add new Event source and Choose Event type as -
Cloudwatch Events- Schedule
. - Add Rule name, Description and Schedule Expression as
cron(30 14 ? * MON-FRI *)
which represents Cron job Schedule to be followed MON-FRI at 14:30 UTC. - Enable the source and this script will start all instances tagged @ 14:30.
- To get your instance retrieved by the Lambda function you need to add tags to it.
- In this script, I've used
Key: 'start'
andValue : 'StartWeekDays'
You can modify it as per your choice.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "MyStatementId",
"Effect": "Allow",
"Action": [
"ec2:DescribeInstances",
"ec2:StopInstances",
"ec2:StartInstances",
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": [
"*"
]
}
]
}
b.) Lambda function to Stop Instances (stop.js):
The stop.js script shuts down all EC2 instances across all regions. Based on requirements cron job can be modified to shut down instances. Instructions are same as Starting Instances, except for the tags.
Key: stop
Value: StopWeekDays
Use Same IAM role for this Lambda function also.