简体   繁体   中英

How do I set up an AWS notification about an instance that is going to be shut down?

We have several AWS EC2 instances for which we set up Lambda functions hooked to Cloudwatch events. One Lambda function starts the instances at 8:00 AM and one shuts them down at 6:00 PM. What I would like to have is a way to notify users that the instances are going to be shut down, maybe 15 minutes before they are scheduled to do so. Is there any way to iterate across the instances and create a message that tells users that instance1 and instance2 are about to shut down? Our code for the Lambda functions is taken directly from the example:

import boto3

# Enter the region your instances are in, e.g. 'us-east-1'
region = 'us-east-1'

# Enter your instances here: ex. ['X-XXXXXXXX', 'X-XXXXXXXX']# 
instances = ['X-XXXXXXXX', 'X-XXXXXXXX']

def lambda_handler(event, context):
    ec2 = boto3.client('ec2', region_name=region)
    ec2.start_instances(InstanceIds=instances)
    print 'started your instances: ' + str(instances)

Any input would be a big help.

By default in EC2, stopping an instance from the inside (eg sudo halt or sudo poweroff ) is exactly the same as stopping them via the API -- they transition to the Stopped state in the console, and you don't have to pay for them, just as if you had stopped them by asking the API to stop them.

The command shutdown -h +15 will ask the system to shut itself down in 15 minutes, and will broadcast a warning to all ttys.

So... you could conceivably invoke EC2 RunCommand from Lambda for each instance, 15 minutes earlier, instead of your shutdown function, running the shutdown command remotely on each system.

Or, if these systems do this every day, just put the shutdown in the crontab.

Unless I'm misunderstanding the question, the code would be nearly identical to the code you already have.

Make a copy of your Lambda function that shuts down your instances. Modify this version to send emails (via SES) instead of actually shutting down the servers. Schedule this new Lambda function to run 15 minutes before the shutdown function runs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM