This is a Serverless Framework project which will automatically start and stop EC2 instances.
It creates two scheduled events. One event will start a list of EC2 instances and the other will stop a list of EC2 instances. The list of instances is defined in the AWS Systems Manager Parameter Store. The project also sets up the necessary IAM policies. No manual AWS configuration is required!
The important files are:
serverless.yml: creates the Lambda functions, defines the IAM roles and defines the schedule for running the start and stop scripts
ec2_stop.py: python scripts to start and stop the instances
parameter.py: defines the
ParameterStorethat provides an easy way to interact with AWS SSM Parameter Store.
- Defines the IAM role to use:
Ec2StartStopRole. The role is defined further down in
- Creates an environment variable
API_ENVand sets the value to the stage.
events section of each function define when the function will run, using cron syntax: in this case daily at 19:00.
Resources section sets the policy to be associated with the IAM role.
This allows the function to write Cloudwatch log files to the relevant log group. The variables will be substituted so for a dev deployment for example this will result in
This allows the function to get parameters from AWS Systems Manager Parameter Store. The parameter store is read at run-time to determine which instances to start and stop. Note that the stage is used to determine which parameters to access so it is possible to have separate lists for each environment e.g. dev, prod.
This allow the function to start and stop instances provided the instance is tag with a
Type equal to the stage e.g.
This code loads parameters from SSM Parameter Store for the region and the instances to start and stop.
This reads the
API_ENV environment variable which will be set to the name of the stage e.g.
This takes the parameter key that has been passed in to the function e.g.
/ec2_instances, prepends it with the stage e.g.
prod and returns the value stored in that parameter.
These instructions are for Linux (specifically Ubuntu). For other environments, a quick web search will help!
To get a list of instance ids:
Note the values of the
InstanceId fields in the returned response.
Create two parameters with the
String. The name of the parameters will be as follows:
stage is a Serverless concept that allows deployments to be grouped. In the following examples we use a
dev. To create a schedule for a separate set of instances just use the name of that
Set the value of each to be the relevant region and the list of instances to start and stop. For example to have the script start and stop 2
dev instances in the
Against each instance type, create a
Type tag with the value of the stage. This tag is checked by the IAM policy to ensure that the event has permissions for the instances in the list. The tag should correspond to the
stage name. For example, for your development EC2 instances, set
schedule for the
ec2_stop functions in the
serverless.yml file. Use either the AWS Cloudwatch events
rate syntax. For example the following will run the function every day at 1900:
Use serverless to deploy the code to AWS which:
- Creates two Lambda functions
- Creates an IAM role with the necessary permissions
- Creates two scheduled Cloudwatch events
For example to deploy to the