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.
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
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.
https://github.com/hisato-kawaji/protocol-buffer-with-serverless-sample
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 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
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.
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
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.
Thank you very much.
Recommended Posts