Your code should follow the Lambda Function Programmer, and it looks like you will need to modify your code a bit. Your Python code must identify one of its functions as a handler. This is done as follows:
def handler_name(event, context): ... return some_value
From the official guide:
event-AWS Lambda uses this parameter to pass event data to the handler. This parameter is typically of type Python. It can also be listed, str, int, float or NoneType.
context-AWS Lambda uses this parameter to provide runtime information to your handler. This parameter is of type LambdaContext. If desired, the handler can return a value. What happens to the returned value depends on the type of call you use when calling the lambda Function:
If you use the RequestResponse call type (synchronous execution), AWS Lambda returns the result of the Python function call to the client calling the Lambda function (in the HTTP response to the call request serialized in JSON). For example, the AWS Lambda console uses the RequestResponse call type, so when you test the function call using the console, the console will display the return value.
If the handler returns nothing, AWS Lambda returns null.
If you use the type of event call (asynchronous execution), the value is discarded.
With these changes, the first step would be to package the code along with any dependencies in the deployment package. For this you must create
.zip as follows.
First create a directory for your package. In this case, you can call it MailgunScript or something similar. Save all Python source files inside this directory at the root level.
You can use the pip command to install any necessary libraries, such as queries and urllib2 libraries, into the directory of your choice:
pip install requests -t /absolutePathTo/MailgunScript pip install urllib2 -t /absolutePathTo/MailgunScript
Finally, you must create the .zip archive from the contents of this directory, and not from the directory itself.
You are now ready to turn your deployment package into a Lambda feature. Log in to the AWS management console and select Create a Lambda Function . If you are prompted to select a drawing, you can select the default scheme hello-world and continue downloading the deployment package and fill in the remaining fields as needed.
You can then test the function simply by returning to the main AWS management console, selecting the function and clicking test . Alternatively, you can manually call the new Lambda function from the command line interface with the following command:
aws lambda invoke \ --region yourRegion \ --function-name yourFunctionName \ --payload '{"url"}' \ --invocation-type RequestResponse \ /tmp/response
This will execute your function and return a response to /tmp/response for verification.