[PYTHON] Let's create it by applying Protocol Buffer to the API with Serverless Framework.

TL;DR

I have a desire to handle the API area a little more with Serverless Framework, and I have tried it with Protocol Buffer, so I will share it.

What is Protocol Buffer?

https://developers.google.com/protocol-buffers

It's not a new technology, but it's an interface description language (IDL). It was developed by Google and seems to have been originally designed to be faster than XML. It is often abbreviated as Protobuf. As for Protocol Buffer, this article is easy to understand, so please refer to it. https://qiita.com/yugui/items/160737021d25d761b353

Why use Protocol Buffers for Serverless Framework?

Originally, the service was built and managed mainly by Serverless Framework, so the first motivation was to be able to share the usage when there are multiple clients, and the same API can be accessed from the Web or Native App. Even if I sent the expected services and sensor data all at once, I was wondering if I could somehow compress the data because of the large amount of communication, so I considered it.

sample

https://github.com/hisato-kawaji/protocol-buffer-with-serverless-sample

things to do

I tried to provide a service only for API Gateway + Lambda with Lambda code assuming Python. (The basic content of Serverless Framework is omitted, and the story focused on Protocol Buffer is described.)

Add plugin

Add serverless-python-requirements and serverless-apigw-binary to your plugin.

sls plugin install --name serverless-python-requirements
sls plugin install --name serverless-apigw-binary

It is also described in `serverless.yml. ``

plugins:
  - serverless-python-requirements
  - serverless-apigw-binary
custom:
.
.
.
  apigwBinary:
    types:
      - 'application/x-protobuf'

requierement.txt Since we are dealing with ProtoBuf on Lambda, we need to import protobuf. So, create requirement.txt.

protobuf==3.5.1
six==1.11.0

Schema creation

Install protobuf in advance.

git clone git://github.com/openx/python3-protobuf.git
cd python3-protobuf
./autogen.sh
./configure --prefix=$PREFIX #Specify the installation destination of protobuf
make
make check
sudo make install

cd python #Python binding
python setup.py build
python setup.py test
sudo python setup.py install

Create a .proto file and define the schema.

sample.proto


syntax = "proto3";

message Test {
  string test_id = 1;
  string hoge = 2;
}

After creating the schema, it's time to build.


protoc -I=. --python_out=. sample.proto

After executing the command, sample_pb2.py will be generated.

Implementation of Lambda Function

Write the function in serverless.yml.

functions:
  Get:
    handler: handler.get
    package:
      include:
        sample_pb2.py
      exclude:
        - serverless.yml
    events:
      - http:
          path: get
          method: post
          cors: true
          private: true

Prepare handler.py and actually install ProtoBuf. If you give the data serialized by Protocol Buffer to the body and parse it with Lambda, you can handle it. When returning with Response, you can respond by Serializing and handling with string.

mport json
import sample_pb2
import base64

def get(event, context):
    obj = sample_pb2.Test()
    obj.ParseFromString(base64.b64decode(event['body']))
    obj.test_id = 'bbb'
    response = {
        'statusCode': 200,
        'headers': { 
            "Access-Control-Allow-Origin": "*",
            'Content-Type': 'application/x-protobuf' 
        },
        'body': obj.SerializeToString().decode('utf-8'),
        'isBase64Encoded': True
    }

    return response

that? Easy?

I had a hard time getting used to working with Protocol Buffers, but the integration itself didn't take much time. If anything, it was quite a pain to debug with Lambda when handling it on Python after defining the schema.

Summary / Prospect

Thank you very much.

Recommended Posts

Let's create it by applying Protocol Buffer to the API with Serverless Framework.
Create a REST API to operate dynamodb with the Django REST Framework
The first API to make with python Djnago REST framework
I tried to create a RESTful API by connecting the explosive Python framework FastAPI to MySQL.
A script that makes it easy to create rich menus with the LINE Messaging API
How to create a serverless machine learning API with AWS Lambda
I want to create an API that returns a model with a recursive relationship in the Django REST Framework
Create a simple reception system with the Python serverless framework Chalice and Twilio
Use Twitter API to reduce the time taken by Twitter (create a highlighting (like) timeline)
How to create a submenu with the [Blender] plugin
Create Python version Lambda function (+ Lambda Layer) with Serverless Framework
Create a Todo app with the Django REST framework
Create a tweet heatmap with the Google Maps API
Let's add it to the environment variable from the command ~
Create a 2D array by adding a row to the end of an empty array with numpy
Let's tweet by hitting the terminal while screaming with Selenium! !!
Probably the easiest way to create a pdf with Python3
[pyqtgraph] Add region to the graph and link it with the graph region
The first step to creating a serverless application with Zappa
[Django] Pass the user instance authenticated by API to ModelSerializer
Let's touch the API of Netatmo Weather Station with Python. #Python #Netatmo
Throw appointments to others with the LINE WORKS calendar API
I tried to verify the speaker identification by the Speaker Recognition API of Azure Cognitive Services with Python. # 1
I tried to create serverless batch processing for the first time with DynamoDB and Step Functions
I tried to verify the speaker identification by the Speaker Recognition API of Azure Cognitive Services with Python. # 2
Find the white Christmas rate by prefecture with Python and map it to a map of Japan