Python Function on AWS Lambda with API Gateway Endpoint
AWS Lambda is a “run and forget” service offered by AWS that gets you out of all the server provisioning, configuration steps which are abstracted and the service scales on demand by triggering through events.
In this post, I will walk you through following steps:
- Write a simple Python Function
- Create AWS Lambda function through AWS Console
- Test the function
- Create and Configure API Gateway connecting to AWS Lambda
Let’s dive into each step:
- Time to write few lines of python code
This simple function returns a text when invoked
def main_handler(event, context):
"body": json.dumps('Cheers from AWS Lambda!!')
Going through few things about structure in Lambda, we need a handler which gets invoked when an event is sent. Here, I have used
main_handler but it could be any name.
event parameter passes event data to the handler
context provides various information about the lambda function during the execution like: time remaining for function termination, CloudWatch log group and stream etc. The details can be accessed with
context.get_remaining_time_in_millis() etc. You can get more idea about context object from this aws doc.
return type is optional for the function
2. Time to Create Lambda Function
Login to AWS Console and head over to Lambda Service[Link] and Click on “Create a function”
Choose first option “Author from scratch” and fill the details:
Name: Give our first function a name. I gave
Runtime: Let’s go with Python 3.6
Role: “Create a new role from one or more templates”
Role name: Give the role a name of your choice. I gave
Policy templates: Select “Simple microservice permissions”
Now, create the new function.
Paste our simple function to the text editor and give handler
3. Testing Lambda Function
Click on “Test” button at top right corner where we need to configure test event. As we are not sending any events, just give event a name, keep “Hello World” template as it is and “Create” it.
Now, when you hit “Test” button again, it runs through testing the function we created earlier and returns the configured value which can be seen on drop down and below the text editor.
The test result shows not only the response of the function but also its exit status, memory used, time taken for execution, Billed duration etc.
4. Create and Configure API Gateway connecting to AWS Lambda
We are done with creating Lambda Function but how to invoke the function from external world? We need an endpoint, right? Here comes Amazon API Gateway. The gateway is also fully managed service which acts as “front door” for applications sitting behind on Lambda, EC2. It abstracts traffic management, authorization and access control, monitoring, and API version management providing a https gateway URL that make it easy to serve applications in few minutes.
Head over to API Gateway and click on “Get Started” and agree on creating an Example API but we don’t use the example API but we create “New API”. Give it a good name keeping “Endpoint Type” regional for now.
Create the API and you will land on the “resources” page of the created API Gateway. Go through the following steps:
- On the “Actions” dropdown, click on “Create Method”. For now, as we are using only “GET” method for our function, select it. Then, click on “Tick Mark” on the right side of “GET” to go through its setup.
- Choose Lambda Function as “Integration Type”
- Choose the region where we created the function earlier
- Write the name of the Lambda Function we created
- Save the method where you it will ask for confirmation of “Add Permission to Lambda Function”. Agree on that and we are done.
- Time to test the setups. Click on TEST and start testing on next step. It should give the response text we had on the above function.
Now, time to get endpoint. We need to deploy the API. On the Actions dropdown, click on Deploy API under API Actions
Fill in the new pop with details of deployment and hit Deploy
In few seconds, we have nice HTTPS endpoint.
On this stage editor, few things like cache setting, throttling, logging enable/disable, canary deployment can be configured. Save the changes and browse the Invoke URL from which we can get response on our browser. Also, if the response is not returned, the function is triggered through the URL.
That’s all for now. In next posts, we will go through access control of the endpoint, canary deployment and more. Stay tuned and feel free to drop comments if any confusion, suggestions, anything constructive.