AWS Lambda function that will stop servers that have a tag with the key
StopGroupand the value the name of the lambda function.
Download the zip file and deploy it as a lambda function.
After you deployed the lambda function, you navigate to Event sources and choose for the Scheduled Event. After naming your event, you can provide
the following schedule expression.
cron(15 17 ? * MON-FRI *)
This expression wil stop the servers every weekday, from monday to friday, at 17:15 PM.
Tagging your instance is very important in order for the lambda function to work properly. The lambda function will retrieve all the instances that have a tag with a key StopGroup,
and a value being the name of the lambda function you provided.
For instance, if you named your lambda function OfficeHoursTerminator, make sure you add a tag with key-value StopGroup=OfficeHoursTerminator to all the instances you want to stop
at the provided schedule.
Make sure your lambda function is able to describe and stop instances.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "MyStatementId",
"Effect": "Allow",
"Action": [
"ec2:DescribeInstances",
"ec2:StopInstances"
],
"Resource": [
"*"
]
}
]
}- aws-lambda-start-server - The same function but for starting the server.
MIT © Sam Verschueren