This is the second part of Instance Scheduler where I am going to explain how a lambda function which when invoked by CloudWatch rule, triggers a SNS notification which sends me an email. Every time the specified instance changes state (start, stop, pending), CloudWatch rule triggers the lambda function which then sends SNS notification via email to me.
Here is my lambda function,
from multiprocessing import Process
ec2_client = boto3.client('ec2')
ec2_resource = boto3.resource('ec2')
state = ec2_resource.Instance('id')
paginator = ec2_client.get_paginator('describe_instances')
pages = paginator.paginate(
for page in pages:
def lambda_handler(event, context):
instances = get_inst() # gets the instances with specified tag
instance_ids = 
for reservation in instances['Reservations']:
for instance in reservation['Instances']:
MY_SNS_TOPIC_ARN = 'arn:aws:sns:ap-southeast-2:2222222222:pras-test'
sns_client = boto3.client('sns')
for instance_id in instance_ids:
TopicArn = MY_SNS_TOPIC_ARN,
Subject = 'Instance Change State: ' + instance_id,
Message = 'Instance ' + instance_id + ' has changed state.\n' +
'State is: ' + instance['State']['Name']
Here is a snippet of my SNS notification sent via email,
As you can see that I got an email when the Instance was stopped. This concludes the second part of the AWS Instance Scheduler. Let me know if you have tried this and have any feedback for me.
An aviation enthusiast turned IT geek, Prasiddha comes from a hospitality background where he learnt valuable lessons of life in terms of customer engagements. He recently started his career in IT, working as a Software Engineer focused on Cloud and DevOps. Prasiddha values work ethics and customer relationship and always puts customers first.
Get connected with Prasiddha :