DEVELOPER BLOG

HOME > DEVELOPER BLOG > 【Introduction to GCP】Using Python with Cloud function - PrismScaler

【Introduction to GCP】Using Python with Cloud function - PrismScaler

1. Introduction

Hello! We are a writer team from Definer Inc. Cloud Functions is a serverless compute service provided by cloud platforms like Google Cloud Platform (GCP), AWS Lambda, and Microsoft Azure. It allows developers to run small pieces of code in response to events without the need to manage server infrastructure. Python is a popular programming language used with Cloud Functions due to its simplicity, ease of use, and versatility. In this issue, you are wondering how to use Python with Cloud function. Let's take a look at the actual screens and resources to explain in detail.

2. Purpose/Use Cases

This article will utilize the Cloud Functions technology for the purpose of serverless Python execution. It is a collection of information and practices that can be helpful for IT professionals who want to use GCP for serverless code execution.
  1. Rapid Development
  2. Event-Driven Automation
  3. Scalability
  4. Integration with Cloud Services
  5. Microservices Architecture
  6. Cost-Effective
  7. Data Processing and Analysis
  8. Web Applications and APIs
  9. IoT and Real-time Data
  10. Proof of Concept and Prototyping

3. What is GCP Cloud Functions?

First, let's review GCP Cloud Functions. Cloud Functions is a service that allows you to run applications completely serverless. It allows you to run programs such as Python without the need to build, maintain, or manage servers or load balancing. Cloud Functions are classified as FaaS (Functions as a Service), allowing you to concentrate on writing code.   Differences from AWS Lambda The differences between Cloud Functions and AWS Lambda include language support, maximum execution time, and price.   ・Supported languages While Python, Java, GO, Node.js, etc. are supported by both services, Powershell and C3 are supported only by Lambda, and PHP is supported only by Cloud Functions.   ・Maximum execution time 15 minutes for Lambda and 9 minutes for Cloud Functions.   ・Fees The free allowance is up to 1 million executions for Lambda and up to 2 million executions for Cloud Functions. The fee per million requests is $0.2 for Lambda and $0.4 for Cloud Functions, while the fee for resource allocation is $1.44 for Lambda and $0.21 for Cloud Functions per 1 GB 24 hours.

4. How to Create Cloud Functions

We will immediately start running Python with Cloud Functions.   (1) Login to GCP and Access Cloud Functions: Sign in to your GCP account and navigate to the Cloud Functions dashboard. (2) Create Function: Click on "Create Function" to start creating a new Cloud Function. Provide a name for your function and select the region where you want to deploy it. (3) Configure Trigger: Choose the trigger type for your function. In this example, select "HTTP" as the trigger, which means the function will be invoked by making HTTP requests. Check "Allow unauthenticated invocations" to allow unrestricted access to the function without authentication for testing purposes. Uncheck "HTTPS required" if you don't want the function to be accessed only via HTTPS. (4) Function Code: In the "Source code" section, you will define the Python function that will run when the Cloud Function is triggered. In this example, the function is named "hello_world" and accepts an HTTP request object as a parameter. It checks if the request contains a query parameter called "message" or a JSON payload with the key "message" and returns the corresponding value. If neither is found, it returns "Hello World!".   (5) Deploy the Function: After writing the function code, click "Deploy" to deploy the Cloud Function with the specified configuration and code.  
def hello_world(request):
    request_json = request.get_json()
    if request.args and 'message' in request.args:
        return request.args.get('message')
    elif request_json and 'message' in request_json:
        return request_json['message']
    else:
        return f'Hello World!'                
(6) Verify Execution: Once the function is deployed, you can verify that it can be executed. Go to the Cloud Functions details screen and click on "Triggers" to get the URL for the function. This URL represents the API endpoint to access the Cloud Function. Click on the displayed URL, and it will make an HTTP request to the Cloud Function, which will run the "hello_world" function and return "Hello World!".

With these steps, you have successfully run Python with Cloud Functions in GCP. The function can now be accessed and executed by making HTTP requests to the provided URL. You can further enhance the function to handle more complex logic, interact with other GCP services, or be triggered by different events. Python's versatility and simplicity make it an excellent choice for building serverless applications with Cloud Functions in GCP.

 

5. Cited/Referenced Articles

6. About the proprietary solution "PrismScaler"

・PrismScaler is a web service that enables the construction of multi-cloud infrastructures such as AWS, Azure, and GCP in just three steps, without requiring development and operation. ・PrismScaler is a web service that enables multi-cloud infrastructure construction such as AWS, Azure, GCP, etc. in just 3 steps without development and operation. ・The solution is designed for a wide range of usage scenarios such as cloud infrastructure construction/cloud migration, cloud maintenance and operation, and cost optimization, and can easily realize more than several hundred high-quality general-purpose cloud infrastructures by appropriately combining IaaS and PaaS.  

7. Contact us

This article provides useful introductory information free of charge. For consultation and inquiries, please contact "Definer Inc".

8. Regarding Definer

・Definer Inc. provides one-stop solutions from upstream to downstream of IT. ・We are committed to providing integrated support for advanced IT technologies such as AI and cloud IT infrastructure, from consulting to requirement definition/design development/implementation, and maintenance and operation. ・We are committed to providing integrated support for advanced IT technologies such as AI and cloud IT infrastructure, from consulting to requirement definition, design development, implementation, maintenance, and operation. ・PrismScaler is a high-quality, rapid, "auto-configuration," "auto-monitoring," "problem detection," and "configuration visualization" for multi-cloud/IT infrastructure such as AWS, Azure, and GCP.