ArchiveAbout

Kyle Pericak

"It works in my environment"

Created: 2019-10-11Updated: 2019-10-11

Google Cloud Functions: Basics

Category:cloudTags:gcpapi;
Building a simple servless API on Google Cloud Platform's Cloud Functions

This post covers getting started with a very simple Google Cloud Functions API, a great serverless solution with affordable pricing. This API will pull the IP address from the HTTP request, and return Hello <source IP>.


Table of Contents

Pricing Details

Cloud Functions pricing is pretty affordable for most use cases, assuming you write your functions in such a way that they don't get invoked a few too many millions of times.

Check the official Google docs for accurate and up-to-date pricing, but here's what it looked like at the time of my writing this.

Pricing is broken into a few fees:

  • Invocations: A flat fee per function execution. $0.0000004 per invocation, excluding the first 2 million.
  • Compute Time: At the lowest clock speed (200MHz x 128MB RAM), compute costs $0.000000231/100ms, ceil 100.
  • Networking: $0.12/GB egress for data, while ingress is free.

Deploy Hello World Function

Open Google Cloud Functions in your browser.

Create a function.

  • Name: HelloWorld
  • Memory Allocated: 128 MB
  • Trigger: HTTP
  • Authentication: Allow unauthenticated invocations
  • Source code: Inline Editor
  • Runtime: Python 3.7
  • Other settings: Leave them as defaults

main.py

The API function defined by Function to execute accepts a flask.Request argument.

def hello_world(request):
    """" Return a Hello message to sender of the API call """
		return 'Hello {}!'.format(request.remote_addr)

requirements.py

You can leave this blank. It's a list of libraries that will be used.


Accessing the Function

From the Console Web UI

  1. Navigate to the Functions page.
  2. Click on the 3 dots next to the function name, and choose "Test function".
  3. Populate and JSON for the GET request in "Triggering event". The above example doesn't accept any input data so {} is all you need.
  4. Click "Test the function"

From the Request URL

  1. Navigate to the Functions page.
  2. Click the function's name to navigate to the "Function details" page
  3. Go to the 'Trigger" tab
  4. Find the URL. Copy it.

You can now test the URL with curl, your browser, httpie, python, etc.

https://<region>-<project>.cloudfunctions.net/HelloWorld
Tags
ansible
Blog code last updated on 2024-02-18: 5ab386de2324c1884556552d0f043a42f2f726ab