Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import boto3
import os
import json

region = 'us-east-1'
ec2 = boto3.client('ec2', region_name=region)

def lambda_handler(event, context):
instances=os.environ['EC2_INSTANCES'].split(",")
ec2.stop_instances(InstanceIds=instances)
print('stopped instances: ' + str(instances))

import boto3

region = "us-east-1"
ec2 = boto3.client("ec2", region_name=region)


def lambda_handler(event, context):
instances = os.environ["EC2_INSTANCES"].split(",")
ec2.stop_instances(InstanceIds=instances)
print("stopped instances: " + str(instances))
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import boto3
import os
import json

region = 'us-east-1'
ec2 = boto3.client('ec2', region_name=region)
import boto3

region = "us-east-1"
ec2 = boto3.client("ec2", region_name=region)


def lambda_handler(event, context):
instances=os.environ['EC2_INSTANCES'].split(",")
instances = os.environ["EC2_INSTANCES"].split(",")
ec2.start_instances(InstanceIds=instances)
print('started instances: ' + str(instances))
print("started instances: " + str(instances))
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import boto3
import os
import json

region = 'us-east-1'
ec2 = boto3.client('ec2', region_name=region)
import boto3

region = "us-east-1"
ec2 = boto3.client("ec2", region_name=region)


def lambda_handler(event, context):
print("Received event: " + json.dumps(event))
instances=[ event['detail']['instance-id'] ]
instances = [event["detail"]["instance-id"]]
ec2.start_instances(InstanceIds=instances)
print ('Protected instance stopped - starting up instance: '+str(instances))
print("Protected instance stopped - starting up instance: " + str(instances))
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import boto3, os, json
import json

FROM_EMAIL_ADDRESS = 'REPLACE_ME'
import boto3

FROM_EMAIL_ADDRESS = "REPLACE_ME"

ses = boto3.client("ses")

ses = boto3.client('ses')

def lambda_handler(event, context):
# Print event data to logs ..
# Print event data to logs ..
print("Received event: " + json.dumps(event))
# Publish message directly to email, provided by EmailOnly or EmailPar TASK
ses.send_email( Source=FROM_EMAIL_ADDRESS,
Destination={ 'ToAddresses': [ event['Input']['email'] ] },
Message={ 'Subject': {'Data': 'Whiskers Commands You to attend!'},
'Body': {'Text': {'Data': event['Input']['message']}}
}
ses.send_email(
Source=FROM_EMAIL_ADDRESS,
Destination={"ToAddresses": [event["Input"]["email"]]},
Message={
"Subject": {"Data": "Whiskers Commands You to attend!"},
"Body": {"Text": {"Data": event["Input"]["message"]}},
},
)
return 'Success!'
return "Success!"
Original file line number Diff line number Diff line change
@@ -1,58 +1,68 @@

# This code is a bit ...messy and includes some workarounds
# It functions fine, but needs some cleanup
# Checked the DecimalEncoder and Checks workarounds 20200402 and no progression towards fix

import boto3, json, os, decimal
import decimal
import json

import boto3

SM_ARN = "YOUR_STATEMACHINE_ARN"

SM_ARN = 'YOUR_STATEMACHINE_ARN'
sm = boto3.client("stepfunctions")

sm = boto3.client('stepfunctions')

def lambda_handler(event, context):
# Print event data to logs ..
# Print event data to logs ..
print("Received event: " + json.dumps(event))

# Load data coming from APIGateway
data = json.loads(event['body'])
data['waitSeconds'] = int(data['waitSeconds'])
# Sanity check that all of the parameters we need have come through from API gateway
data = json.loads(event["body"])
data["waitSeconds"] = int(data["waitSeconds"])

# Sanity check that all the parameters we need have come through from API gateway
# Mixture of optional and mandatory ones
checks = []
checks.append('waitSeconds' in data)
checks.append(type(data['waitSeconds']) == int)
checks.append('preference' in data)
checks.append('message' in data)
if data.get('preference') == 'sms':
checks.append('phone' in data)
if data.get('preference') == 'email':
checks.append('email' in data)
checks = [
"waitSeconds" in data,
type(data["waitSeconds"]) == int,
"preference" in data,
"message" in data,
]
if data.get("preference") == "sms":
checks.append("phone" in data)
if data.get("preference") == "email":
checks.append("email" in data)

# if any checks fail, return error to API Gateway to return to client
if False in checks:
response = {
"statusCode": 400,
"headers": {"Access-Control-Allow-Origin":"*"},
"body": json.dumps( { "Status": "Success", "Reason": "Input failed validation" }, cls=DecimalEncoder )
"headers": {"Access-Control-Allow-Origin": "*"},
"body": json.dumps(
{"Status": "Success", "Reason": "Input failed validation"},
cls=DecimalEncoder,
),
}
# If none, start the state machine execution and inform client of 2XX success :)
else:
sm.start_execution( stateMachineArn=SM_ARN, input=json.dumps(data, cls=DecimalEncoder) )
else:
sm.start_execution(
stateMachineArn=SM_ARN, input=json.dumps(data, cls=DecimalEncoder)
)
response = {
"statusCode": 200,
"headers": {"Access-Control-Allow-Origin":"*"},
"body": json.dumps( {"Status": "Success"}, cls=DecimalEncoder )
"headers": {"Access-Control-Allow-Origin": "*"},
"body": json.dumps({"Status": "Success"}, cls=DecimalEncoder),
}
return response


# This is a workaround for: http://bugs.python.org/issue16535
# Solution discussed on this thread https://stackoverflow.com/questions/11942364/typeerror-integer-is-not-json-serializable-when-serializing-json-in-python
# Solution discussed on this thread:
# https://stackoverflow.com/questions/11942364/typeerror-integer-is-not-json-serializable-when-serializing-json-in-python
# https://stackoverflow.com/questions/1960516/python-json-serialize-a-decimal-object
# Credit goes to the group :)
class DecimalEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, decimal.Decimal):
return int(obj)
return super(DecimalEncoder, self).default(obj)