Hello I designed a python script that works locally however I would like to push it to AWS Lambda, I’m having some issues specifically with creating the handler within a class. I have figured out how to get rid of the ‘handler error’ in lambda by creating the handler function outside of the class but unfortunately that doesn’t run the rest of my code. My goal is to place the “lambda_handler” function either inside my class or have the function call the class. Any advice is really appreciated!
#!/usr/bin/python
import sys
import os
import json
import time
from datetime import datetime, timedelta
key = 'OKTA_AUTH'
### key = os.environ['OKTA_AUTH'] #####
outcome = 'outcome.result eq "FAILURE"'
event_type = 'eventType eq "application.provision.user.deactivate"'
app_id = 'target.id eq "SOME OKTA APP ID"'
all_params = f'{event_type} and {app_id} and {outcome}'
api_url = 'https://domain.okta.com/api/v1/logs'
slack_url = "SLACK URL"
last_hour_date_time = datetime.utcnow() - timedelta(days=10)
since = str(last_hour_date_time.strftime('%Y-%m-%dT%H:%M:%S.000Z'))
actor_list=[]
unique_list=[]
class Events:
def lambda_handler(event, context):
okta_auth()
def okta_auth(self):
event_list=[]
url = api_url.format()
params = {
'filter': all_params,
'since': since
}
response = requests.get(url, params=params,
headers={'Accept': 'application/json',
'authorization': key})
response_json = response.json()
for event_data in response_json:
events = event_data['outcome']['reason']
event_list.append(events)
actors = event_data['actor']['alternateId']
actor_list.append(actors)
unique_set = set(actor_list)
unique_list.append(unique_set)
if event_list != []:
self.post_slack()
else:
sys.exit(0)
def post_slack(self):
url = slack_url.format()
payload = "{"text": " Twillio Flex provisioing failure. Please check the following users %s "}" % (unique_list)
requests.post(url, headers={'Accept': 'application/json'}, data=payload)
### newly added code
if __name__ == "__main__":
Events().lambda_handler()
### end
####ORIGINAL CODE USED TO BE
#if __name__ == "__main__":
# Events().okta_auth()
2
Answers
After some solid studying, I discovered I was running into two issues with my code and how AWS Lambda works. The first issue was how I was calling the class in Lambda. I though that you had to have the function inside the class, but instead I created a function to run the class.
My second issue was deployment via inline code. Lambda does not have the requests module installed by default, so I created a local directory, where I then pip3 installed requests, and moved the python script to. You can then zip the folder contents and upload to aws lambda.
heres the final code below for reference.
Your function
only prints the event and does not execute anything else. I guess that’s why the lambda is not doing anything. The lambda_handler is the entry point of your lambda.