AWS EC2 Instance Scheduler – Part 2
j

Prasiddha Bista

December 12, 2020

AWS | Cloud | EC2 | L200

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,

import boto3

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')

 

def get_inst():

pages = paginator.paginate(

Filters=[

     {

        'Name': 'tag-key',

        'Values': [

             'ppc-scheduler',

        ]

    },

])

for page in pages:

return page

 

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']:

instance_ids.append(instance['InstanceId'])

 

MY_SNS_TOPIC_ARN = 'arn:aws:sns:ap-southeast-2:2222222222:pras-test'

sns_client = boto3.client('sns')

for instance_id in instance_ids:

sns_client.publish(

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.

Prasiddha Bista

Software Engineer, Cloud and DevOps at Versent

Get connected with Prasiddha :

2 Comments

  1. Pianino Klasyczne

    An impressive share, I just given this onto a co-worker who was doing a little analysis on this. And he in fact bought me breakfast because I found it for him. smile. So let me reword that: Thank you for the treat! But yeah Thanks for spending the time to discuss this, I feel strongly about it and enjoy learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more info? It is extremely helpful for me. Two thumb up for this post!

    Reply
  2. Links.M106.COM

    hi people I are afflicted by mass confusion so thanks for keeping the web good. Decent reading. Nice blog design too. continue your great work.

    Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

Related Articles