I have this function working as expected in python. How do I convert it to AWS Lambda function?
def mymailgun(url):
import urllib2
myfile=urllib2.urlopen(url)
import requests
print requests.post("https://api.mailgun.net/v3/XXX.mailgun.org/messages",
auth=("api", "key-XXX"),
files=[("attachment", myfile)
#("attachment", open("files/test.txt"))
],
data={"from": "Excited User <[email protected]>",
"to": "[email protected]",
"cc": "[email protected]",
"bcc": "[email protected]",
"subject": "Hello",
"text": "Testing some awesomness with attachments!",
"html": myfile})
Your code must follow the Lambda Function Programming Model and it appears you'll need to slightly modify your code to fit. Your Python code must identify one of it's functions as the handler. This is done as follows:
def handler_name(event, context):
...
return some_value
From the official manual:
event—AWS Lambda uses this parameter to pass in event data to the handler. This parameter is usually of the Python dict type. It can also be list, str, int, float, or NoneType type.
context—AWS Lambda uses this parameter to provide runtime information to your handler. This parameter is of the LambdaContext type. Optionally, the handler can return a value. What happens to the returned value depends on the invocation type you use when invoking the Lambda function:
If you use the RequestResponse invocation type (synchronous execution), AWS Lambda returns the result of the Python function call to the client invoking the Lambda function (in the HTTP response to the invocation request, serialized into JSON). For example, AWS Lambda console uses the RequestResponse invocation type, so when you test invoke the function using the console, the console will display the returned value.
If the handler does not return anything, AWS Lambda returns null.
If you use the Event invocation type (asynchronous execution), the value is discarded.
With those changes made the first step would be to package the code, alongside any dependencies, into a deployment package. To do this you must create a
.zip
file in the following fashion.
First, create a directory for your package. In this case you could call it MailgunScript
or something similar. Save all your Python source files inside this directory at the root level.
You can use the pip command to install any required libraries, like the requests and urllib2 libraries, into a directory of your choosing:
pip install requests -t /absolutePathTo/MailgunScript
pip install urllib2 -t /absolutePathTo/MailgunScript
Finally, you must create a .zip
archive from the content of this directory, not from the directory itself.
You're now ready to turn your deployment package into a Lambda function. Log into your AWS management console and choose Create a Lambda Function
. If prompted to select a blueprint, you may select the default hello-world
blueprint and the proceed to upload your deployment package and fill out the rest of the fields as required.
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 invoke the new Lambda function from the command line interface with a command such as the following:
aws lambda invoke \
--region yourRegion \
--function-name yourFunctionName \
--payload '{"url"}' \
--invocation-type RequestResponse \
/tmp/response
This will execute your function and output the response to /tmp/response
for inspection.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With