install python packages in AWS Lambda
xxxxxxxxxx
'''
You can use external dependencies by uploading a zip file with all your
required libraries and your lambda function.
You can either follow the steps here: https://docs.aws.amazon.com/lambda/latest/dg/python-package.html#python-package-create-package-with-dependency
Or this steps paraphrased from the link above:
1. Create a folder where you are going to store all your packages and lambda func
2. Inside that new folder, create another folder called packages
3. Now it's time to install the dependencies your lambda function needs:
- For this example I want to install flask and boto3 so that I can use it
in my lambda function.
- On the terminal you are going to type:
pip install flask boto3 -t <the path of your packages folder>
- the command above will install the flask and boto3 dependecies to the
packages folder you created previously
4. On the root directory where the packages folder is, you are going to create a
lambda_function.py file in which you should have the code for the
lambda function
Here's how your new folder should look like:
<name of the new folder>
|
|____ packages
| |___ soem file
| |___ some file
| .
| .
| .
| |___ some file
|
|____ lambda_function.py
- The packages folder should have a bunch of files and other folders
5. Zip all the contents of the packages folder. Go inside the folder,
select everything inside of it > right click > compress to zip
6. Name the zip file something like "deployment"
7. Drag the lambda_function.py inside the zip file.
8. Open the zip file, you should see all your libraries and your lambda function
in the same place.
9. Go to the AWS Lambda Console. Select your lambda function or create one if
you don't already have it.
10. On the "Code" tab, you should see button that says "Upload from", click on it
11. Select .zip file
12. Click on upload and look for your deployment zip file. Open it and click on
save.
13. Your lambda function should now be working if everything went well.
IMPORTANT!! Remember to check the documentation! It has a more detailed explanation.
https://docs.aws.amazon.com/lambda/latest/dg/python-package.html#python-package-create-package-with-dependency
'''
'''
Your lambda_function.py should have all the code that will execute when the
lambda function is triggered by any event.
'''
'''
Here's an example code a for lambda function that triggers when a user signs up
through AWS cognito. The purpose of this lambda function is to find out
whether an email already exists in the user pool or not. We want to avoid
having multiple users with the same email. Therefore, this lambda function
will prevent a user from creating an account if the email is already in use by
another user.
'''
import boto3
# initialize cognito client to use cognito API methods
auth = boto3.client("cognito-idp", aws_access_key_id="<aws access key id>", aws_secret_access_key="<secret access key>")
# Regardless of what your lambda function does, you must have the function below
# with the exact same name and parameters. Aka., your lambda function file MUST
# have the function: def lambda_handler(event, context)
# whatever you put inside of it is up to you.
def lambda_handler(event, context):
address = event['request']['userAttributes']['email']
# Getting all the users from my user pool based on the specified params
user_list = auth.list_users(
UserPoolId="<your userPoolId>",
AttributesToGet=[
'email',
],
Limit=20,
# filter user pool my email and only return the user(s) with the specified email
Filter= f"email='{address}'"
)
# checks if the list of users in the response is bigger than zero
if len(user_list["Users"]) > 0:
# if we find a user with the desired email then we throw an exception
# because we do not want to have more than 1 user with the same email
raise Exception("Email already exists!")
# if the email was not found in the user pool, then we let the user create
# an account
return event
'''
Again, for more information visit:
https://docs.aws.amazon.com/lambda/latest/dg/python-package.html#python-package-create-package-with-dependency
'''