Prerequisites:
- Boto3 installed on your local machine
pip3 install boto3
- AWS CLI installed on your local machine
pip3 install awscli
- AWS account with permissions
Scenario:
Our DevOps engineering team often uses a development lab to test releases of our application. The Managers are complaining about the rising cost of our development lab and need to save money by stopping our (for this example) 3 ec2 instances after all engineers are clocked out.
Use your script in a Lambda function using Python 3.7 or higher runtime. Make sure the lambda runs on a set schedule daily. No one should be working in the Dev environment past 7 pm. (Note: to test you may need to modify the time accordingly so you aren’t waiting for 7 pm)
1. Creating our EC2 Instances
To start this project we will be creating 3 EC2 instances and giving them tags for our Lambda function to refer to later. To create our EC2 Instances we will use the following code: