Logo

dev-resources.site

for different kinds of informations.

GCP - Create api with Cloud Functions and API Gateway

Published at
5/10/2024
Categories
gcp
cloudfunction
apigateway
api
Author
Ankit malik
Categories
4 categories in total
gcp
open
cloudfunction
open
apigateway
open
api
open
GCP - Create api with Cloud Functions and API Gateway

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.

  1. For Cloud function - backend-function
  2. For api-gateway - api-gateway
  • Go to iam -> Service Accounts -> +CREATE SERVICE ACCOUNT

backend-function service account

api-gateway service account

  • copy the email generated for api-gateway service account

Task-2: Create Cloud function

  • Go to - Cloud Functions -> +CREATE FUNCTION

create cloud function

  • choose backend-function service account

backend-function service account

  • Leave all other function details to default -

use default nodejs function

  • Now, Deploy the function
  • After this, a function will be deployed

deployed function

Change the permission for cloud function

  • add principal
    add  principal for cloud function

  • give the permission to api-gateway account for function invocation.

Permission for api gateway

givining the invocation access for cloud function

  • Save it and policy will be updated and it will look like below image

CF policy update

Task-3: Create Api-Gateway

  • Get the address from cloud function from trigger section

Get the address from cloud function

  • 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

Create api-gateway screenshot

  • Wait for the API Gateway to be created, it will look like this after completion

After api gateway created

  • Go to newly created api-gateway and get the URL for api gateway from gateways tab

api gateway url

  • add hello after the given url and we will be able to access the api like this

api-gateway api working

Featured ones: