Skip to content

Instantly share code, notes, and snippets.

@giuliocalzolari
Last active February 15, 2022 14:28
Show Gist options
  • Save giuliocalzolari/c32f2094b4ec4ec4ea41f12d34445a71 to your computer and use it in GitHub Desktop.
Save giuliocalzolari/c32f2094b4ec4ec4ea41f12d34445a71 to your computer and use it in GitHub Desktop.
Lambda poweroff ec2 across all regions
from datetime import datetime, timedelta, time
import boto3
# last update 2022/02/15
TIME = {
"eu-central-1": {
"description": "Europe(Frankfurt)",
"permitted_ec2_time": "8-19",
"utc_offset": "+1"
},
"eu-north-1": {
"description": "Europe(Stockholm)",
"permitted_ec2_time": "8-19",
"utc_offset": "+1"
},
"eu-west-1": {
"description": "Europe(Ireland)",
"permitted_ec2_time": "8-19",
"utc_offset": "+0"
},
"eu-west-2": {
"description": "Europe(London)",
"permitted_ec2_time": "8-19",
"utc_offset": "+0"
},
"eu-west-3": {
"description": "Europe(Paris)",
"permitted_ec2_time": "8-19",
"utc_offset": "+1"
},
"eu-south-3": {
"description": "Europe(Milan)",
"permitted_ec2_time": "8-19",
"utc_offset": "+1"
},
"me-south-1": {
"description": "Middle East(Bahrain)",
"permitted_ec2_time": "8-19",
"utc_offset": "+3"
},
"ca-central-1": {
"description": "Canada(Central)",
"permitted_ec2_time": "8-19",
"utc_offset": "-5"
},
"us-east-1": {
"description": "US East(N. Virginia)",
"permitted_ec2_time": "8-19",
"utc_offset": "-5"
},
"us-east-2": {
"description": "US East(Ohio)",
"permitted_ec2_time": "8-19",
"utc_offset": "-5"
},
"us-west-1": {
"description": "US West(N. California)",
"permitted_ec2_time": "8-19",
"utc_offset": "-8"
},
"us-west-2": {
"description": "US West(Oregon)",
"permitted_ec2_time": "8-19",
"utc_offset": "-8"
},
"ap-southeast-1": {
"description": "Asia Pacific(Singapore)",
"permitted_ec2_time": "8-19",
"utc_offset": "+8"
},
"ap-southeast-2": {
"description": "Asia Pacific(Sydney)",
"permitted_ec2_time": "8-19",
"utc_offset": "+10"
},
"ap-southeast-3": {
"description": "Asia Pacific(Jakarta)",
"permitted_ec2_time": "8-19",
"utc_offset": "+7"
},
"ap-northeast-1": {
"description": "Asia Pacific(Tokyo)",
"permitted_ec2_time": "8-19",
"utc_offset": "+9"
},
"ap-northeast-2": {
"description": "Asia Pacific(Seoul)",
"permitted_ec2_time": "8-19",
"utc_offset": "+9"
},
"ap-northeast-3": {
"description": "Asia Pacific(Osaka)",
"permitted_ec2_time": "8-19",
"utc_offset": "+9"
},
"ap-east-1": {
"description": "Asia Pacific(Hong Kong)",
"permitted_ec2_time": "8-19",
"utc_offset": "+8"
},
"ap-south-1": {
"description": "Asia Pacific(Mumbai)",
"permitted_ec2_time": "8-19",
"utc_offset": "+5.5"
},
"sa-east-1": {
"description": "South America(Sao Paulo)",
"permitted_ec2_time": "8-19",
"utc_offset": "-3"
},
"af-south-1": {
"description": "Africa(Cape Town)",
"permitted_ec2_time": "8-19",
"utc_offset": "+2"
},
}
def range_region_time(region_name):
# If check time is not given, default to current UTC time
begin_time_s, end_time_s = TIME[region_name]["permitted_ec2_time"].split("-")
tz = TIME[region_name]["utc_offset"]
if tz.startswith("+"):
tz_now = datetime.utcnow() + timedelta(hours=float(tz))
else:
tz_now = datetime.utcnow() - timedelta(hours=float(tz))
begin_time = datetime(tz_now.year, tz_now.month,
tz_now.day, int(begin_time_s), 0)
end_time = datetime(tz_now.year, tz_now.month,
tz_now.day, int(end_time_s), 0)
# print("time {} in region {}".format(tz_now, region_name))
if begin_time < tz_now < end_time:
# print("BETWEEN!")
return True
else:
# print("NOT!")
return False
def lambda_handler(event, context):
client = boto3.client('ec2')
ec2_regions = [region['RegionName'] for region in client.describe_regions()['Regions']]
for region in ec2_regions:
if not range_region_time(region):
print("searching instances in region {} - {}".format(region,TIME[region]["description"]))
ec2 = boto3.resource('ec2',region_name=region)
instances = ec2.instances.filter(Filters=[{'Name': 'instance-state-name', 'Values': ['running']}])
for instance in instances:
print("Stopping instance {} in region {}".format(instance.id, region))
ec2.instances.stop(instance.id)
if __name__ == '__main__':
event = {
'account': '123456789012',
'region': u'us-east-1',
'detail': {},
'detail-type': 'Scheduled Event',
'source': 'aws.events',
'time': '1970-01-01T00:00:00Z',
'id': 'cdc73f9d-aea9-11e3-9d5a-835b769c0d9c',
'resources': ['arn:aws:events:us-east-1:123456789012:rule/my-schedule']
}
lambda_handler(event,"")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment