Start/stop instance on AWS using Lambda

prerequisites

Since the free tier is going to end at the end of Janauray, to save the cost for myself, I decided to only start instance on business hours(09:00-18:00) of the day.

Component

  • Lambda
  • EvenBus Scheduler
  • SNS
  • IAM Role

Details

The architecture is simple:

What I need to prepare in advance is the lambda code bundle and the IAM role which has enough access to EC2

The code repo is at https://github.com/chenliu1993/aws-lambda-updown, build the binary and bundle it into a zip file.

The role policy is needed to have default lamba policy

After you create the lambda and upload the bundle, please make sure vpc is not used(still dont know why if VPC is attached then lambda fails) and the handler name shoule be same as the binary name.

After above is finished, create the cron schedule using the event in JP timezone

This is just what I use for my instance. And also dont forget to set the scheduler’s event text therefore lambda will read from the text to parse the information as input to handler. Something like this part:

The input part is what needs to be set to keep the format as the lambda binary’s input format. What I use in test is like:

About what input should be like, please check my code above. Users can define by themselves

Finally you can add SNS to sent fail/success message to any ends like email or phone

And also lambda has a test section so that the lambda can be tested

Leave a Reply

Your email address will not be published. Required fields are marked *