Serverless Architecture: Creating a REST API from a Python Script with AWS Lambda

Fatih Koprucu
turkcell
Published in
8 min readNov 2, 2022

Imagine you wrote the perfect solution for any problem but it works only on your local computer. Maybe you don’t have time or experience to create an API and make that solution public. Then serverless architecture might be perfect for you.

From my experience, I wrote an ID verification script with Python which you can see the details at my previous post. Since I felt lazy to create a web service and deploy it to a server, I looked for a serverless option.

So What is Serverless Architecture?

Consider that I only know how to code. I don’t want to concern about server management, maintaining or scaling. In that case serverless architecture is there to help me.

Simply, serverless architecture isolates you from the infrastructure and server management. So your application is still running on a server but you only concentrate on your code. The server management is done by the cloud provider such as AWS, Microsoft, etc. The application developer codes the server-side business logic and the code runs on event-triggered containers which are managed by the providers.

You can enjoy the advantages of reducing complexity, operational and engineering cost but your structure will be depended on the service provider.

Should I use it?

Of course it depends on the complexity of the application. For more complex applications, it might make things more complicated.

Most of the serverless architecture providers arrange their pricing according to number of requests. So I recommend you to try it, if your application is not complicated, your number of request will not be that large and you are keen to deploy your code faster.

What will we do in this article?

  • I wrote a ID verification script in Python. (By the way, Python is not a must. You can go on with your favorite language) We will specify input args and response for our code.
  • Then we will deploy our code to AWS Lambda to use serverless architecture. If you don’t have an AWS account, you can create one and try it for free
  • Finally we will use AWS API Gateway to create a REST API from our Lambda function.

I assume this introduction is enough to start our journey.

Now it is time to write and deploy our code.

Step 1: Write your code

As I mentioned, you can find the details of my code on my previous post. To sum up, my code gets name, surname, ID number and birth year information from the user and returns a boolean value: True if the ID info is valid.

You can find the github repo of my code here. The short version of my main validate_id_info method:

Step 2: Deploy your code to AWS Lambda

If you don’t have a AWS account, I suggest you to create and play around with it. Don’t worry it is free for small amount of requests (At least it was free when I was writing this post :)).

Alternatively, you can go on with this Lambda tutorial rather than mine. Let’s create a Lambda Function…

  • Click Create Function
  • You will be redirected to creation page. Fill the Function name field with any name you want to give. Then choose your Runtime (Since my code is in Python, I selected Python). That’s all, you don’t need to change anything else. Your page should be like below then:
  • Then click Create Function button at the bottom of the page.

Your Lambda Function is created. Now it is time to transfer your code here.

  • At your functions page you can see your newly created function in the list. Click to your function. You will see the details of your function
  • Scroll down to Code source section and transfer your code there. I just simply copy-pasted my code there
  • Since we didn’t change any default configs here, we will define a method called lambda_handler. This method will handle the requests comes to our source code. This is my lambda_handler method:

As I mentioned before, I need ID number, name, surname and birth year information for my validate_id_info method so I will take these arguments from event, such as id_no = event[“idNo”], etc.

So you are expecting any input for your operations, you can specify input keyword and take it from event like a dictionary as event[<keyword>].

So I am calling validate_id_info method then if Id info is accurate, the response will be “Valid ID” otherwise response will be “Get lost!!!”

  • Now our code is ready, you should hit Deploy button to commit your changes. Every time you make any change, you should hit Deploy(!!!)
  • Our code is deployed. We should test it, right?
  • At the Test section click Configure test event like above. Then you will be see Configure test event form like below. Test event action should be “Create new event”. Give your event a name at Event name field. After that at Template field, select hello-world template. This is the section you will give inputs to your function. So I defined my idNo, name, surname and birthYear inputs. You can modify these according to your needs. Then click Save.
  • Now we can test it. Click Test button to trigger test event. Then you will see your response and other details about the request at Execution result tab like below:

That was a long step but we completed the deployment and Lambda Function definition.

Step 3: Create a REST API from your Lambda Function

In this step we will create a API Gateway REST API and invoke the lambda function we created on the previous step with HTTP call.

  • First we need to create the API. Go to your API Gateway page.
  • Click Create API button
  • Then you need to choose the type of the API. Scroll down to find REST API as below
  • Click Build on REST API box.
  • Then you will be redirected to the configuration page. Choose the REST protocol and give your API a meaningful name. Choose the endpoint type as Regional. Your configurations might be seen as the image:

Well done!!! You created your first REST API. But this is not enough to use that API.

If you are familiar with REST API development, you know that you are mapping url paths with the corresponding methods. For example, if any HTTP POST request comes to the URL “/validateidinfo”, I want “idVerifier” to be invoked.

In API Gateway we call that URL paths resources. Therefore we need to create resources to trigger our lambda functions. In order to create a resource:

  • In API Gateway page, while you are on the Resources tab and the resource “/” is highlighted, Click Actions, then choose Create Resource like below:
  • Now you can create a resource at this page. As I mentioned I want to map the URL “/validateidinfo” so I will create it as a resource. You can customize your own resource. My resource configs can be seen in the figure:
  • Click Create Resource to complete it.
  • Now you have a new resource, so you can connect your lambda function to that resource.
  • Click your new resource to highlight it (for me it is “/validateidinfo”). Then Click Actions and choose Create Method.
  • Method means the HTTP methods which are GET, POST, PUT, etc. I want my clients to reach my API with POST method so I choose POST and choose the check mark icon to confirm.
  • At method Setup page, we need to connect our lambda function with that POST Request. So I choose Lambda Function as Integration Type. Then the Lambda region and the name of the Lambda Function must be specified below:
  • Click save to complete the method creation. Then you will see the Permission dialog box:
  • Click OK to give permission.

Congratulations you have a brand new REST API !!!

But.. You need to deploy it to test from your Postman :)

Let’s deploy it.

  • Click Actions again and choose Deploy API.
  • You need a new deployment stage to deploy your API. So choose [New Stage] as Deployment stage and give your stage a proper name. I called my stage name “test”.
  • Click Deploy to complete
  • Now you will see the page below.

Now that is the exciting part!

You can now reach your REST API from anywhere.

Call Your REST API

Since your API is now public, you can call it from any platform. I prefer Postman to test it so here is my response:

Surprisingly it works…

I hope you make it through the path with me.

That seems like a long journey to create a simple REST API but believe me it is not as hard as it looks.

Now it is time to REST :)

Cheers…

References

--

--