Introduction
In this article, we will explore how to create an API using Google Cloud Platform (GCP) Cloud Functions and API Gateway. Cloud Functions allow you to build serverless applications that can scale automatically based on demand. API Gateway is a service that acts as a central point of entry for your API requests, allowing you to handle tasks such as routing, authentication, rate limiting, and more.
By combining these two services, you can create a highly scalable and flexible API that can handle a wide range of use cases.
Prerequisites
Before we get started, make sure you have the following:
- A GCP working account
Tasks:
There are total3 tasks for this activity
Task 1 - service account creation
Task 2 - cloud function creation
Task 3 - api gateway creation
Task-1: Create 2 Service Account
Let's create two service accounts.
- For Cloud function -
backend-function
- For api-gateway -
api-gateway
- Go to
iam -> Service Accounts -> +CREATE SERVICE ACCOUNT
- copy the email generated for
api-gateway
service account
Task-2: Create Cloud function
- Go to -
Cloud Functions -> +CREATE FUNCTION
- choose backend-function service account
- Leave all other function details to default -
- Now, Deploy the function
- After this, a function will be deployed
Change the permission for cloud function
- Save it and policy will be updated and it will look like below image
Task-3: Create Api-Gateway
- Get the address from cloud function from trigger section
- make api-spec file and change the address value which is found in above step
swagger: '2.0'
info:
title: test-name-spec
description: Sample API on API Gateway with a Google Cloud Functions backend
version: 1.0.0
schemes:
- https
produces:
- application/json
paths:
/hello:
get:
summary: Greet a user
operationId: hello
x-google-backend:
address: https://us-central1-qwiklabs-gcp-03-3b06fe0404f2.cloudfunctions.net/cloud-backend-function
responses:
'200':
description: A successful response
schema:
type: string
- Create the gateway with above file and add the details similar to given screenshot
- Wait for the API Gateway to be created, it will look like this after completion
- Go to newly created api-gateway and get the URL for api gateway from gateways tab
- add
hello
after the given url and we will be able to access the api like this
Top comments (0)