As an administrator, you often have several operational tasks that you have to perform on a regular basis. A vast array of these tasks can be performed through automation. This can be used for both ongoing processes that you need to perform or project-related. In this video, we explain a use case for automating the start and stop of EC2 Instances using both AWS Lambda and Amazon EventBridge. This is often useful when you are working with On-Demand EC2 Instances where perhaps the instances need not be in the running state 24/7. You can automate the state and stop of your EC2 instances only when required and thus save on usage costs as part of your overall cost optimization pillar of the AWS Well-Architected Framework.
AWS Lambda is a serverless compute offering designed to help you build serverless and event-driven applications. AWS Lambda functions can be triggered by a wide range of AWS services to perform some compute action on your AWS resources. Amazon EventBridge is a serverless event bus solution that can deliver a stream of data from an event source to targets such as AWS Lambda, Amazon SNS and SQS among others. AWS EventBridge can also help you schedule triggers for your targets as is the case in this hands-on demonstration using Cron schedules.
This video demonstrates how to set up and configure AWS Lambda and EventBridge to automatically start and stop your EC2 Instances at designated times, Mondays to Friday and fulfils a typical use case where we wish to ensure that On-Demand instances are only placed in the running state when needed for daily operational tasks.
This video also comes with a sample IAM Policy to help you define your Lambda Execution Role as well as the actual python code to help you build your Lambda function to automatically start and stop your EC2 instances.
Link: https://github.com/iaasacademy/aws-how-to-guide/tree/main/LambdaEC2StartStop