GCP - Create api with Cloud Functions and API Gateway
Tags: gcp, cloudfunction, apigateway, api
Published: 2024-05-10 14:09:16

#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](#task1-create-2-service-account)
[Task 2 - cloud function creation](#task2-create-cloud-function)
[Task 3 - api gateway creation](#task3-create-apigateway)

###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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/17y2m4iuv5flfimh7l42.png)


![api-gateway service account](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/75niqza5p8bu3h8x4p5l.png)

- copy the email generated for `api-gateway` service account

---
---


###Task-2: Create Cloud function

-  Go to - `Cloud Functions -> +CREATE FUNCTION`


![create cloud function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ska5s5vc94yd4jnmcpjb.png)


- choose backend-function service account

![backend-function service account](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tvxzvbgnqy4hkm6whi4o.png)


- Leave all other function details to default -

![use default nodejs function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f98ag6a7l3ksnhbwqnai.png)

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

![deployed function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0xmtua75fxn6kt4cq5c2.png)

#### Change the permission for cloud function

- add  principal
![add  principal for cloud function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q3zr0jmfkdxt5fp9j9cs.png)



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

![Permission for api gateway](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xwkwoe4bhkk0boljcdnd.png)


![givining the invocation access for cloud function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fjhekvrmr6ttybdkmmcy.png)


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

![CF policy update](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/of2yelsdhcj6m6f3rklz.png)

---
---
### Task-3: Create Api-Gateway

- Get the address from cloud function from trigger section 

![Get the address from cloud function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ahp2c5ggiw0u2lbv30op.png)



- make api-spec file and change the address value which is found in above step

```yaml
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z3r74x14p8lljylw22i2.png)


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

![After api gateway created](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c23i6454zb3wosvispcy.png)



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

![api gateway url](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hl5zop0tue0c4ntdw1zv.png)


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

![api-gateway api working](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iukrfrzxlaeao5fzetrs.png)