[GCP] [Python] Deploy API serverless with Google Cloud Functions!

Hello. This is Satchie. Since it is neat, I misunderstood the day of the week to post last week. Excuse me. The day before, @ Rimo1129 (junior) wrote Make something like a job hunting portfolio on AWS ~ !!!!! As a job hunting chan, it is too good to write an Advent calendar. Right. I don't understand AWS at all, so please let me know. I don't know AWS, so I would like to play with Google Cloud Functions provided by Google.

What is Google Cloud Functions?

Cloud Functions is a serverless execution environment that you can use to create and connect to cloud services. Cloud Functions allows you to create simple, single-purpose functions related to events that occur in your cloud infrastructure or services. The function is triggered when the event of interest occurs.

That's right. It's like AWS lambda. This time, by specifying HTTP as a trigger, we will deploy the API serverlessly so that it can be hit.

plan

In Previous article, we collected information on restaurants around the company using the API of Gurunavi. This time I'm going to deploy a function that takes a keyword and a search method (and or or) as parameters and returns a list of store information that contains the keyword in the word of mouth. Since the data was small, I will prepare it in the following json without putting it in the database.

[{'id': '1046494',
  'name': 'Jinya',
  'longitude': '139.712244',
  'latitude': '35.647473',
  'holiday': '',
  'opentime': 'Mon-Fri Lunch: 11:30~14:00(L.O.13:30), Dinner: 17:00~23:30(L.O.22:30)(* Takeout 11:30~22:00 (reception on the day 11:30~21:30))\n Saturdays, Sundays, and holidays 11:30~23:30(L.O.22:30)(* [Happy hour] Saturdays, Sundays, and holidays 14:00~18:00! 200 yen drinks available!\n * Take-out reception is 11:30~22:00)',
  'category': 'Izakaya',
  'pr': '',
  'url_mobile': 'http://mobile.gnavi.co.jp/shop/1046494/?ak=m%2Bqqz7dSuc6%2FTCXqzE3tXC%2FHVysBRwNGyp5Ud0dSNe8%3D',
  'image_url': '',
  'review_text': 'There is a small vial on the shaved ice and it is chilled well. The one with chicken and okura. The one with chicken and yuba. This is refreshing and the richness of the chicken is beautiful. It was a hearty bowl with chicken soboro bowl with hot balls. It is a hearty exquisite set of ramen, rice and pork. It is a lunch with sweet and sour simmered sardines. It goes perfectly with the buns, so it stays hot because it's brought along with the steamed pork. It's a nice notice. It's just like roasting it and cooking it. There is a small small bottle on top of it. It's chilled well. It's got sardines and okra. It's simmered with sardines and yuba. It's refreshing and has a nice appearance. It's a hearty bowl with fried chicken and on-centric egg, and it's a hearty dish with ramen, rice, and pork. It's a lunch. It's a fluffy VANS with sweet tofu skin. It goes well with the steamed pork, so it stays hot and nice. It seems that it's just roasted and cooked. Recommended for those who like a fairly rare, soft-tasting lever.'},
 {'id': '5237768',
  'name': 'IL BOCCALONE',
  'longitude': '139.712111',
  'latitude': '35.647784',
  'holiday': '',
  'opentime': 'Mon-Sat 17:30~24:00(L.O.23:00)\n days 17:30~23:00(L.O.22:00)',
  'category': 'Italian',
  'pr': '',
  'url_mobile': 'http://mobile.gnavi.co.jp/shop/5237768/?ak=m%2Bqqz7dSuc6%2FTCXqzE3tXC%2FHVysBRwNGyp5Ud0dSNe8%3D',
  'image_url': '',
  'review_text': 'The fitcine was entwined with the rich sauce and it was delicious. Even though it is a black cabbage, the classic Parmesan cheese risotto is not only the power of the whole cheese but also the strong taste is really addictive Recommended ☆ Outside The crispy and tender meat is the most delicious. The umami of the shrimp is on the whole surface and you will want to go again. The fitcine is entwined with the rich sauce. The black cabbage is a classic parmesan cheese that is heavier than you think. The risotto is not only the power of the whole cheese, but also the strong taste is addictive. The deliciousness of the cheese is really melted. The outside is crispy and the soft meat is the best. The taste of shrimp is on the whole surface and it is delicious to go again.'}]···Continue

get along

This time we will create a function on the GCP console. Of course, you can also deploy locally with gcloud commands. We will create a function. Specify HTTP for the trigger here. When exposing authentication as an API, allow unauthenticated calls. スクリーンショット 2020-12-19 6.04.39.png

Next, set the runtime to Python, write the required packages in requirements.txt, and change the entry point to the name of the function you want to execute. Also, when using Cloud Function for the first time, it is necessary to enable Cloud Build from the point of "Enable API". After that, write a function and add any necessary files. (This time shop_data.json is added)

スクリーンショット 2020-12-19 7.09.14.png

A green checkmark will appear when the deployment is complete. スクリーンショット 2020-12-19 7.14.40.png

try

Hit

curl "https://asia-northeast1-<Project name>.cloudfunctionsnet/ebimog-api?keywords=Horse,Italian&retrieval_type=or"
{
  "results": [
    {
      "id": "5351809",
      "name": "Uncle Tom",
      "longitude": "139.711817",
      "latitude": "35.647634",
      "holiday": "",
      "opentime": "",
      "category": "pasta",
      "pr": "",
      "url_mobile": "http://mobile.gnavi.co.jp/shop/5351809/?ak=m%2Bqqz7dSuc6%2FTCXqzE3tXC%2FHVysBRwNGyp5Ud0dSNe8%3D",
      "image_url": ""
    },
    {
      "id": "5497878",
      "name": "AFURI",
      "longitude": "139.710687",
      "latitude": "35.648240",
      "holiday": "Nothing",
      "opentime": "Mon-Sun Lunch / Supper: 11:00-5 the next day:00",
      "category": "ramen",
      "pr": "The motto is "nostalgic taste" ramen that you will want to eat again.",
      "url_mobile": "http://mobile.gnavi.co.jp/shop/5497878/?ak=m%2Bqqz7dSuc6%2FTCXqzE3tXC%2FHVysBRwNGyp5Ud0dSNe8%3D",
      "image_url": ""
    },
    {
      "id": "6145400",
      "name": "Meat sushi Ebisu Yokocho store",
      "longitude": "139.710681",
      "latitude": "35.648097",
      "holiday": "Open all year round\n * We will be closed during the year-end and New Year holidays.",
      "opentime": "Mon-Sat, the day before public holidays 15:00-5 the next day:00\n days / holidays 15:00~23:00",
      "category": "Meat sushi",
      "pr": "Nigiri sushi specializing in meat, mainly beef and horse meat. That is meat sushi.!!!\n\n Open until 5 am!\n There is no choice but to eat the famous Sashitoro!",
      "url_mobile": "http://mobile.gnavi.co.jp/shop/6145400/?ak=m%2Bqqz7dSuc6%2FTCXqzE3tXC%2FHVysBRwNGyp5Ud0dSNe8%3D",
      "image_url": ""
    },・ ・ ・ The following is omitted

It's back!

At the end

In this way, the API was easily created just by registering the function from the console. There are various triggers other than HTTP, so it seems fun to master the GCP service.

reference

-I tried using Python with Google Cloud Functions (using Beta)

Recommended Posts

[GCP] [Python] Deploy API serverless with Google Cloud Functions!
Try using Python with Google Cloud Functions
[GCP] Operate Google Cloud Storage with Python
Text extraction with GCP Cloud Vision API (Python3.6)
Google Cloud Vision API sample for python
Streaming speech recognition with Google Cloud Speech API
Deploy functions with Cloud Pak for Data
Use Google Cloud Vision API from Python
How to connect to Cloud Firestore from Google Cloud Functions with python code
Serverless face recognition API made with Python
[GCP] Procedure for creating a web application with Cloud Functions (Python + Flask)
Automatic voice transcription with Google Cloud Speech API
How to call Cloud API from GCP Cloud Functions
Serverless LINE bot made with IBM Cloud Functions
Deploy Python3 function with Serverless Framework on AWS Lambda
Speech transcription procedure using Python and Google Cloud Speech API
Run Google Cloud Functions locally with Cloud Native Build packs
Curry arbitrary functions with Python ....
Getting Started with Python Functions
Use Trello API with python
Easily serverless with Python with chalice
Use Twitter API with Python
Deploy Django serverless with Lambda
Study Python with Google Colaboratory
Python compilation with pyqt deploy
Web API with Python + Falcon
Play RocketChat with API / Python
Call the API with python3.
Access Google Drive with Python
Use subsonic API with python3
Google Drive Api Tips (Python)
Effortlessly with Serverless Python Requirements
Emulate GCP Cloud functions locally
Easy to use Nifty Cloud API with botocore and python
[GCP] A memorandum when running a Python program on Cloud Functions
Speech recognition of wav files with Google Cloud Speech API Beta
Get data from analytics API with Google API Client for python
[GCP] How to output Cloud Functions log to Cloud Logging (Stackdriver Logging) (Python)
Issue reverse geocoding in Japanese with Python Google Maps API
Play with YouTube Data API v3 using Google API Python Client
Python: Extract file information from shared drive with Google Drive API
[Package cloud] Manage python packages with package cloud
Create Awaitable with Python / C API
Get reviews with python googlemap api
Run Rotrics DexArm with python API
[Python] Hit the Google Translation API
Stream speech recognition using Google Cloud Speech gRPC API on python3 on Mac!
[Python3] Google translate google translate without using api
Quine Post with Qiita API (Python)
Play with GCP free frame ① ~ Cloud Run, Datastore & LINE Messaging API ~
Run XGBoost with Cloud Dataflow (Python)
Hit the Etherpad-lite API with Python
Play with GCP free frame ② ~ Airflow (on Compute Engine), Cloud Functions ~
[python] Read information with Redmine API
10 functions of "language with battery" python
Run python3 Django1.9 with mod_wsgi (deploy)
Cloud DevOps Cookbook Part 4-Explore DevOps DirectMail in Python with REST API
Translate Coursera's WebVTT format subtitles with the GCP Cloud Translation API
Introducing Google Map API with rails
Use Google Analytics API from Python
Python calling Google Cloud Vision API from LINE BOT via AWS Lambda