To save your AWS cloud operational cost, you can schedule EC2 instanes’ start and stop time using Lambda function. When you define a lamda function, you need to setup a trigger, which specifies the event that can invoke the lambda fucntions. In this post, we will use the a specific UTC time as a trigger . For example, you want to start EC2 instances at 06:30 UTC and want to stop the instanes at 20:30 UTC.
EC2 Start schedule setup
Step 1: create a labmda function and specifiy the EC2 instance IDs that you need to start. Paste the following code in the code editor.In this example, I have used python 2.7 and named the function ‘server-start’.
import boto3
region = ‘eu-west-1’
instances = [‘i-0d9c727e8be4f6dfc’, ‘i-0e330be1705e29501’]
def lambda_handler(event, context):
ec2 = boto3.client(‘ec2’, region_name=region)
ec2.start_instances(InstanceIds=instances)
print ‘started ec2 instances: ‘ + str(instances)
Note: you need to create and assign a role to the lambda function to start and stop the instances(ec2:StartInstances,ec2:StopInstance). The easiest way to test the lambda function created above without creating custom role is to assign the AmazonEC2FullAccess permission to the function.
Step 2: create a cloudwatch schedule event and point it to the Lambda function that you created in step 1 as shown below screenshot.
EC2 stop schedule setup
Step 1: You need to follow the similar steps that you used to schedule Ec2 instance. At first create a lambda function and name it (e.g server-stop) and then paste the following code. Remember to replace the instance IDs by your EC2 instance IDs.
import boto3
region = ‘eu-west-1’
instances = [‘i-0d9c727e8be4f6dfc’, ‘i-0e330be1705e29501’]
def lambda_handler(event, context):
ec2 = boto3.client(‘ec2’, region_name=region)
ec2.stop_instances(InstanceIds=instances)
print ‘stopped ec2 instances: ‘ + str(instances)
Step 2: create a cloudwath event rule and select the serve-stop lamba as target as shown below screenshot.
Scale down EC2 instances in a auto scaling group using Lambda function
To scale down the nunber of EC2 instances running under a auto scaling group at a particular time each day, you can create a lambda function and schedule the task inside the lambda as shown below:
import boto3
import datetime
client = boto3.client(‘autoscaling’)
def lambda_handler(event, context):
# TODO implement
response = client.put_scheduled_update_group_action(
AutoScalingGroupName=’WP-Auto-Scaling-Group’,
ScheduledActionName=’Scale-Down’,
Recurrence=’30 20 * * *’,
MinSize=1,
MaxSize=1,
DesiredCapacity=1
)
RDS instance start and stop with Lambda
To stop RDS instance use the following Lamba funciton.Remeber to replace ‘wp’ and ‘search’ by your own RDS instance id.
import boto3
region = ‘eu-west-1’
def lambda_handler(event, context):
rds = boto3.client(‘rds’, region_name=region)
myID= [‘wp’,’search’]
for id in myID:
rds.stop_db_instance(
DBInstanceIdentifier= id
)
To start RDS instance, use the Lambda code below:
import boto3
region = ‘eu-west-1’
def lambda_handler(event, context):
rds = boto3.client(‘rds’, region_name=region)
myID= [‘wp’,’search’]
for id in myID:
rds.start_db_instance(
DBInstanceIdentifier= id
)