This repository was archived by the owner on Dec 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathlambda_function.py
More file actions
42 lines (32 loc) · 1.45 KB
/
lambda_function.py
File metadata and controls
42 lines (32 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""
A simple AWS Lambda function that helps manage start and stop schedules to save costs on EC2
instances.
Initially based on this:
https://aws.amazon.com/premiumsupport/knowledge-center/start-stop-lambda-cloudwatch/
"""
import boto3, os
def lambda_handler( event, context ):
"""Handler function called directly by Lambda."""
# Disable pylint check for unused args, as Lambda will always try to pass through `context`.
#pylint: disable=unused-argument
# If IP addresses have been provided in the event, check if the source IP is a-ok.
if 'authorised-ips' in event:
if 'source-ip' in event and not event['source-ip'] in event['authorised-ips']:
raise Exception( 'Unauthorised.' )
# If IP addresses have been provided in an environment variable, check if the source IP is a-ok.
if 'AUTHORISED_IPS' in os.environ:
authorised_ips = os.environ['AUTHORISED_IPS'].split( ',' )
if 'source-ip' in event and not event['source-ip'] in authorised_ips:
raise Exception( 'Unauthorised.' )
ec2 = boto3.client( 'ec2', region_name = event['region'] )
# Start or stop the requested instance(s).
if 'start' == event['action']:
ec2.start_instances( InstanceIds = event['instances'] )
elif 'stop' == event['action']:
ec2.stop_instances( InstanceIds = event['instances'] )
print( event['action'] )
print( 'instances: ' + ', '.join( event['instances'] ) )
return {
'action': event['action'],
'instances': event['instances']
}