0

Actually i want to reduce AWS bill. I am using some of the instances in my project. I want a notification after certain time period(every 3rd/4th hour) on my login email id and shutdown on a particular time duration. So, how can i do this?

if current time - instance start time >= 3 hr (send a mail)
if current time - instance start time >= 4 hr (send a mail)
if current time - instance start time >= 5 hr (shut down that instance)

7
  • There is no built-in functionality to perform what you request, but you could code it yourself using an AWS Lambda function. See also: Use Amazon CloudWatch to Detect and Shut Down Unused Amazon EC2 Instances May 4, 2020 at 8:33
  • Thank you, i have edited, you can see for better understanding. May 4, 2020 at 8:41
  • I am getting an error when i am adding destination of my lambda function to SNS. Your function's execution role doesn't have permission to send result to the destination. By clicking save we'll attempt to add permission to the role for you. . I have given two policy (EC2FullAccess and SNSFullAccess). Why is it showing you don't have permission? What are the more policies it required? May 21, 2020 at 7:10
  • Why are you defining a Destination for the Lambda function? May 21, 2020 at 7:17
  • FYI, I'm writing a few Stopinators based on your request. Stopinator Type 2 handles "stop after", but does not have notifications yet. May 21, 2020 at 7:18

3 Answers 3

1

For your use-case, I have coded a Stopinator that can stop/terminate instances after a given duration.

In addition, it can send notifications to an Amazon SNS topic after given running durations.

See: GutHub: Simple Lambda Stopinator: Type 2

It is controlled via tags:

Tag Names (in priority order):

  • Terminate-After: Terminate instance
  • Stop-After: Stop instance
  • Notify-After: Send SNS notification if still running (For multiple notifications, use Notify-After1, Notify-After2, etc)

Tag Value: Indicates running duration (eg '30m', '1.5h', 24h')

Schedule the Lambda function to run at regular intervals (eg every 5 minutes) using Amazon CloudWatch Events.

For more details, see: Simple EC2 Stopinator in Lambda - DEV Community

0

Generally you could setup CloudWatch Schedule Expressions for Rules which would periodically trigger actions that you require. You setup perdiodic tasks by defining Cron Expressions in the CloudWatch Event rules.

These actions can be SNS to send you email, or execute EC2 StopInstances API call to stop your an instance automatically.

0
0

you can do it by setting up cloud watch event and schedule that with cron expression. Goto: AWS console -> cloudwatch -> Events -> Rules -> create rule -> schedule -> enter your desired crone expression -> target (your lambda function which would do the task). in your lambda function you can use

ec2_client = boto3.client('ec2')
response = ec2_client.describe_instance_status()
#response can give detail about other and particular instance also
instance_status_details = response['InstanceStatuses'][0]['InstanceStatus']['Details']
#there is impairedSince parameter (inside instance_status_detail) to figure out when you launched this instance
#take from this link  https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html

crone expression: https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html

2
  • How will cloudwatch trigger lambda function after 3hr(or 4hr, etc) from start of instances? May 4, 2020 at 19:38
  • your cloudwatch event does not know the time when instance was started so suppose you trigger it at every half hour or some other preferable time interval. Your target lambda function will know the start time of EC2 instance. After figuring out start time you can take action that you want to run it or stop the instance. May 5, 2020 at 1:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.