• Company
    • About Us
    • Organization Belief
    • Awards & Achievements
    • IP Protection and Data Security
    • FAQ
    • Testimonial
  • Microsoft Expertise
    • Microsoft Dot Net Technologies
      • ASP.NET (Web Applications, MVC, Web API)
  • Services
  • Ecommerce
  • Artificial Intelligence
  • Industry
  • Portfolio
  • Contacts
    • [Column]
      • Contact UDS Systems
      • [Custom]
    • [Column]
      • Map Image
    • [Column]
      • [Custom]
Menu
  • Company
    • About Us
    • Organization Belief
    • Awards & Achievements
    • IP Protection and Data Security
    • FAQ
    • Testimonial
  • Microsoft Expertise
    • Microsoft Dot Net Technologies
      • ASP.NET (Web Applications, MVC, Web API)
  • Services
  • Ecommerce
  • Artificial Intelligence
  • Industry
  • Portfolio
  • Contacts
    • [Column]
      • Contact UDS Systems
      • [Custom]
    • [Column]
      • Map Image
    • [Column]
      • [Custom]
Book A Call

How to Create a Python Web Framework with Lambda, Serverless, & DynamoDB?

by Shishir Dubey May 10, 2019

Welcome to the tutorial page of ChromeInfotech. Now in this guide, I will tell you how to develop a Python Web Framework with Serverless, Lambda, & DynamoDB.

Let me give you a small intro to our company! ChromeInfotech is a leading Python Web Framework Development organization and having expertise in PHP, Python, Django, Mean-Stack, SharePoint, BlockChain App Development, Ruby-On-Rails, Dot.NET, ASP.NET, React Native, AngularJS and a lot more.

We have delivered more than 160 application using python Web Framework. If you are looking for the best python development company or looking to hire a Python Web Framework developer, contact us.

We have a deal for you!

Now let me start the Python Web Framework tutorial, here we go!

Our current python web framework tooling can work flawlessly with the Serverless Framework. How about we go over how to utilize the Python web framework Flask to deploy a Serverless REST API.

In this stroll through, we will:

Deploy a straightforward Python Web Framework API endpoint

  • Add a DynamoDB table and two parameters to make and recover a User object
  • Set up way explicit routing for increasingly granular measurements and checking
  • Configure your condition for local development for quicker development experience.

In case you as of now have a Flask application that you need to change over to Serverless, jump to the Converting a current Flask application segment beneath.

If you need to skirt the walkthrough and begin with a wholly configured format, look at the Using the Quick Start Template area beneath.

Let’s get started | Python Web Framework

To begin, you’ll need the Serverless Framework installed. You’ll additionally require your condition configured with AWS credentials.

  • Creating and deploying a solitary endpoint
  • Let’s begin by using a single parameter.

To start with, make another directory with a package.json document:

Python-image

At that point, install a couple of dependencies. We’re going to utilize the serverless-WSGI module for arranging the API Gateway occasion type into the WSGI design that Flask anticipates. We’ll additionally use the serverless-python-prerequisites module for dealing with our Python Web Framework packages on deployment.

Python-image-1

In case you need a more profound plunge on the serverless-python-necessities module, look at our past post on taking care of Python Web Framework packages with Serverless.

With our libraries installed, how about we compose our Flask application. Make a document app.py with the accompanying content:

Python-image-2

This is an extremely straightforward application that profits “Hi World!” when a solicitation comes in on the root way/. It’s the model application appeared on Flask’s greeting page without any adjustments.

To get this application deployed,

Python Web Framework: Make a serverless.yml in the working directory:

Python-image-3

Note

A past variant of this post set dockerizePip: valid rather than dockerizePip: non-Linux. You’ll require serverless-python-necessities v3.0.5 or higher for this alternative.

This is a really fundamental arrangement. We’ve made one functional application. The handler will be handler work from the WSGI module. Note that this module will be added to our deployment package by the serverless-wsgi module. We design our application’s entry point in the custom square under the wsgi area.

We pronounce that our application’s entry point is app.app, which implies the application object in the app.py module.

For our function’s occasions set up, we’ve utilized an expansive way coordinating with the goal that all solicitations on this space are steered to this capacity. The majority of the HTTP routing rationale will be done inside the Flask application.

The exact opposite thing we have to do is handle our Python Web Framework packages. The serverless-python-necessities module searches for a requirements.txt record in our working directory and installs them into our deployment package. How about we build that requirements.txt record.

To begin with, make a practical situation and actuate it. I’m utilizing Python3 in my serverless.yml, so I’m indicating that here too:

Python-image-4

If you need a walkthrough on using Python Web Framework virtual environment, look at Kenneth Reitz’s walkthrough.

At that point, install the Flask package with pip, and spare your conditions in requirements.txt:

Python-image-5

Python Web Framework | Deploy the functions:

Python-image-6

After a moment, the console will demonstrate your endpoints in the Service Information area. Explore to that course in your program:

hello-word

You nailed it—a genuine, live application on the Web!

Including a DynamoDB table along with the REST-like endpoints

Completing a “Welcome World!” is fun. However, your application should continue a type of state to be helpful. How about we include a DynamoDB table as our support store.

For this essential precedent, suppose we’re putting away Users in a database. We need to store them by userId, which is an exceptional identifier for a specific client.

To begin with, we’ll have to arrange our serverless.yml to arrangement the table. This includes three sections:

  • Provisioning the table into the resources area;
  • Including the proper permissions of IAM; and
  • Passing the name if the table as the environment variable so the functions not able to use it.

Python Web Framework | Customize your serverless.yml to look as given here:

Python-image-8

We provisioned the table in the assets segment utilizing CloudFormation punctuation. We additionally included IAM consents for our capacities under the iamRoleStatements bit of the supplier square.

At last, we passed the table name as the earth variable USERS_TABLE in the earth bit of the supplier square.

Presently, how about we update our application to utilize the table. We’ll actualize two endpoints: POST/clients to make another client, and GET/clients/{userId} to get data on a specific client.

Python Web Framework | Update your app.py as pursues:

Python-image-9

Also, the base “Hello World” endpoint, we presently have two new endpoints:

  • GET/clients/:userId for getting a User
  • POST/clients for making another User

We’ve included a boto3 reliance, so we should install that into our virtual condition and update our necessities in requirements.txt:

Python-image-10

Python Web Framework | Now, deploy the service as well as test it!

Python-image-11

We can utilize the curl for the given examples. Configure the BASE_DOMAIN variable to the domain and base path. It can be easier to reuse:

Python-image-12

Then, now make a user:

Python-image-13

Friendly—we have created a new user! Then, let’s retrieve a user along with GET /users/:userId` endpoint:

Python-image-14

This is undoubtedly not an undeniable REST API, and you’ll need to include things like mistake dealing with, confirmation, and extra business rationale. This gives a framework in which you can work to set up those things.

Python Web Framework | Path-specific routing

We should look again at our capacity arrangement in serverless.yml:

Python-image-15

We’re sending all traffic on the area to our application and giving Flask a chance to deal with the aggregate of the routing rationale. There is an advantage to this—you don’t need to hang every one of my courses and capacities physically. You can likewise restrain the effect of cold-begins on delicately utilized classes.

But, We’re sending all traffic on the area to our application and giving Flask a chance to deal with the aggregate of the routing rationale. There is an advantage to this—you don’t need to hang every one of my courses and capacities physically. You can likewise restrain the effect of cold-begins on delicately utilized classes.

When each route is taken care of by an alternate Lambda work –

At that point you can see:

  • How ordinarily each route has invoked
  • How much issues and errors you have for every route
  • What amount of time each route takes (and the amount of money you can save if you made the route quicker)

Fortunately, you can, in any case, get these things on the off chance that you need them! You can arrange your serverless.yml with the goal that various courses are steered to multiple instances of your capacity.

Each function case will have similar code, yet they’ll be sectioned for measurements purposes:

Python-image-16

Presently, all solicitations to GET/clients/:userId will be dealt with by the getUser occurrence of your application, and all tenders to POST/clients/will be taken care of by the createUser occasion.

For some other solicitations, they’ll be dealt with by the principle application occurrence of your function. Once more, none of this is required, and it’s a touch of an overweight arrangement; every particular endpoint will incorporate the full application code for your different parameters.

In any case, it’s a decent harmony between speed of development by utilizing the devices you’re used to, alongside the per-endpoint granularity that serverless application designs give.

Local development configuration along with the Serverless offline plugin

When building up an application, it’s pleasant to quickly emphasize by creating and testing locally as opposed to completing a full deploy between changes. In this area, we’ll spread how to arrange your condition for local development.

The incredible thing about the serverless-wsgi module is that it incorporates a worked in the answer for local development. To begin the local server, run SLS wsgi serve:

image-17

Then move to the root page on localhost:5000 in the browser:

hello-world-1

It is working! If you roll out an improvement in your app.py document, it will be refreshed whenever you hit your endpoint. This quickly improves development time.

While this works effectively for a stateless endpoint like “Hi World!”, it’s somewhat trickier for our/clients endpoints that collaborate with a database.

Fortunately, there’s a module for doing local development with a local DynamoDB emulator. We’ll utilize the serverless-dynamodb-local module for this.

Python Web Framework | Now, you need to install the plugin:

image-18

At that point, we should add the module to our serverless.yml. We’ll likewise include some config in the custom square with the goal that it locally makes our tables characterized in the resources block:

image-19

Now, run the command to install the DynamoDB local:

image-20

At last, we have to roll out some little improvements to our application code. While instantiating our DynamoDB customer, we’ll include some unique design in case we’re in a local, disconnected condition.

When growing locally, the serverless-WSGI module sets a situation variable of IS_OFFLINE to genuine, so we’ll utilize that to deal with our config. Change the start of app.py to the accompanying:

image-21

Presently, our DynamoDB customer is configured to use DynamoDB local. In case we’re running locally, or utilize the default alternatives if running in Lambda.

Now see if it works.

You’ll require two different terminal windows now. In your first window, start-up DynamoDB local:

image-22

In the next window, you need to start up the local WSGI server:

Image-23

Now, run the curl command from before hit our local endpoint and make a user:

image-24

And after that retrieve a user:

image-25

Yeah, it works same as it did on the Lambda.

This local setup can genuinely accelerate your work process while as yet enabling you to imitate a nearby guess of the Lambda environment.

Converting the current Flask application

In case you as of now have a current Flask application, it’s anything but difficult to change over to a Serverless-accommodating application. Do the accompanying steps:

  • Install serverless-wsgi & serverless-python-requirements packages — npm install –save serverless-wsgi serverless-python-requirements
  • Configure the serverless.yml:

You must have the serverless.yml it can look like this:

image-26

Ensure that the value for the app under custom. wsgi square is configured for your application. It ought to be <module.instance>, where the module is the name of the Python Web Framework record with your Flask example, and the case is the name of the variable with your Flask application.

Deploy the function along with SLS deploy!

Note:

If you utilize different resources (databases, credentials, and so on.), you’ll have to ensure those make it into your application. Look at our other material on overseeing insider facts & API keys with Serverless.

Utilizing a Quick Start Template

In case you don’t have a current Flask application to change over, yet you need a well-organized beginning stage for a request, you can look at our serverless-flask application format.

To utilize it, you’ll need the Serverless Framework installed. You’ll additionally require your condition configured with AWS credentials.

With the Framework installed, utilize the SLS install order to clone the layout venture. At that point, change into the directory and run a post-setup content to arrange it as expected:

image-27

Then, run SLS deploy and hit the main web page to view your starter application:

Congratulations! You have successfully used your flask app. There are lots of things that you can build with Serverless &Python. The limit is only your imagination.

I hope this tutorial will help you to guide the best ways to create a REST API along with Lambda, Serverless, & DynamoDB. In case you need expert assistance for your app, you can get in touch with us.

As a leading Python Web Framework and Mobile App Development company, we always encourage our client to get the best product. We have quality software development team that can give you a great shot to your application. They also perform integration with Chat SDK, Map SDK, Cloud Server, App Analytics, Payment Gateway and so on.

If you found this post helpful, do let us know in the comment section. Keep connecting, keep sharing the knowledge and keep coding.

Good Luck!

Posted in:

About Shishir Dubey

Shishir Dubey is the founder and CEO of ChromeInfotech, making mobile apps affordable and simple for small and enterprise businesses. I have been a technology geek since school days and was always busy experimenting with new technologies which later turned into my passion. With the start of Mobile Apps era, I decided to start an organization which can design and develop mobile apps as per recent technology trends and offers most innovative and stylish solutions to people around the world. In this journey, I have been working as a brain behind some of the successful projects which got Million $ Funding. I helped them in designing the architecture of their apps in scalable manner.

Most Popular Posts

  • Save 60% Of Your App Development Cost
  • 5 How To Research Techniques
  • A Short & Quick 8 Step Guide
  • Top Mobile App Analytics Tools 
  • Ingredients of  Blockchain And its working
  • Best Chat SDK Tools for 2019
  • Build Web Apps using AngularJS
  • Trending Map Integration Platform in 2019
  • Real Life Ways to find App Developer
  • React Native for Quick App Development
  • What Are Location-Based Apps?
  •  5 Modern Cloud Server Hosting provides  
  • Build Your Own Payment Gateway  
  • Best Wireframe Tools
  • Hire Tableau Developers
  • Firebase Development Platform
  • Progressive Web Apps

Connect

  • Follow us on Facebook
  • Follow us on Twitter
  • Follow us on LinkedIn

Posts by Category

  • Android App Development
  • Angular js
  • App Development
  • App Market Research
  • ColdFusion
  • Crystal Reports
  • Data Visualization
  • Django Framework
  • General
  • Indiana
  • Infographics
  • iPhone App Development
  • Microsoft Programming
  • Microsoft SharePoint
  • Mobile App Development
  • Node JS
  • PHP
  • Php Technology
  • Python framework
  • Resource
  • Ruby on Rails
  • SharePoint
  • Software Development
  • Technology
  • Web Development
 Contact Us
Danger!This alert box could indicate a dangerous or potentially negative action.

    What is 4 x 9?

    This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.Accept Reject Privacy Policy
    Privacy & Cookies Policy

    Privacy Overview

    This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
    Necessary
    Always Enabled
    Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
    Non-necessary
    Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
    SAVE & ACCEPT
    • Company
      • About Us
      • Organization Belief
      • Awards & Achievements
      • IP Protection and Data Security
      • FAQ
      • Testimonial
    • Services
      • APP & WEB DEVELOPMENT
        • Mobile
        • Android
        • iPhone
        • React Native
        • Angular JS Development
        • Mean Stack Development
        • PHP
        • Python
        • Python DJAngo Framework
        • Ruby on Rails
        • WordPress
      • MICROSOFT TECHNOLOGIES
        • .Net Application Development
        • C# and VB.Net Development
        • ASP.Net Development
        • Wireframing
        • SharePoint Development
        • Tableau Development
      • INDUSTRIES
        • Education
        • Health
        • Fitness
        • Food & Restaurants
        • Travel
        • Auto Mobile
        • Real Estate
        • Enterprise
        • Logistic
      • TECHNOLOGY
        • Blockchain
        • IOT
        • App Analytics
        • Payment Integration
        • Cloud Services
        • Google Map Integration
        • App Market Research
        • MVP Development
        • Chat Integration
    • Data Visulization
      • Consulting and Strategy Services
        • Assessment and Recommendations
        • Planning and Roadmap Creation
        • Technology Advisory
        • Diagnostic Services
        • POC Development
      • Development Services
        • Data Preparation / Integration
        • Data Visualization
        • Dashboarding / Story Development
        • Reporting / Decision Making
        • Data Visulization Sharing With Stakeholders
      • Support and Maintenance Services
    • Technology Services
    • Clients
      • Case Study
      • Projects
      • Testimonials
    • Contact Us