Accessing Google API over CEN

By Vikram Godse, Solution Architect

Alibaba Cloud Cloud Enterprise Network (CEN) provides connectivity to Alibaba Cloud Virtual Private Clouds (VPCs) across the globe. This provides low latency bandwidth in addition to cross border connectivity to workloads from any international Alibaba Cloud Region into Alibaba Cloud in China

For users who have their apps built around the Google API’s there is a bit of configuration required to get it working from China. This article shows how to configure a Reverse Proxy to Access Google API’s from China over Alibaba Cloud CEN.

This approach is only to showcase accessing the custom API’s developed by an organization and hosted on Google API Gateway. In the long term, it is recommended that the custom API’s be moved to Alibaba Cloud API Gateway.

Prerequisites

  1. Alibaba Cloud Account
  2. 2 VPC’s one in China Region and the other in the International region. We have considered Beijing and London in this document
  3. 2 ECS instances 1 in Beijing VPC and the other in the London VPC
  4. CEN Configuration Across the two VPC’s
  5. Access to Google APIs for testing. We are using the Google Texttospeech API and the Google Translate API for the test.

Architecture Overview

Image for post

The diagram above depicts the overall connectivity between the Alibaba Cloud regions between Beijing and London and the connectivity to Google API over the internet

  1. The VPC in Beijing hosts an ECS instance running Centos 7 which will be the client machine wanting to access the Google APIs
  2. The VPC in London hosts an ECS instance running Centos 7 and Apache 2.4.6
  3. The VPC in London connects to Google API’s over standard Internet

Initial Setup

  1. The IP for the ECS instance in Beijing is 192.168.0.201
  2. The IP for the ECS instance in London is 172.16.0.31

Setup two hostnames in the /etc/hosts file on the Beijing server pointing to the London server for eg.

Image for post

These hostnames will be the Virtual hosts that we will configure on the London Webserver, one each for the Google Translate and Text-to-Speech API’s

Install and Configure Apache

Install Apache on the Centos Server in London

Image for post

The modules that are needed to use Apache as a reverse proxy include mod_proxy itself and several of its add-on modules, which extend its functionality to support different network protocols. Specifically, we will be using:

  1. mod_proxy, the main proxy module Apache module for redirecting connections; it allows Apache to act as a gateway to the underlying application servers.
  2. mod_proxy_http, which adds support for proxying HTTP connections.
  3. mod_proxy_balancer and mod_lbmethod_byrequests, which add load balancing features for multiple backend servers.

All these modules are enabled by default, but we can check the modules installed

Image for post
Image for post

If any of the modules are not listed you can enable them by editing the /etc/httpd/conf.modules.d/00-proxy.conf

And un-commenting the required modules by removing the #

Save the file and then restart the httpd service

Image for post

For SSL to be supported you need to install mod_ssl

Image for post

Configure Apache for Reverse Proxy

Setup two virtual hosts one for the “Google Translate API” and another for the “Text-to-Speech” API

Create an Apache Config file in the /etc/httpd/conf.d/google-translate.conf and make the following entries

Image for post

The request for google translate will be received by this virtual host “translate.rentokillondon.com” over the CEN, which is in London and it will proxy the request to Google Translate over https using the Google API URL https://translate.googleapis.com It will forward the URL context as it has received from the request.

An example request to translate some content from English to German is shown in the following diagram

Image for post

We can create another Virtual host for Google-Text-to-Speech API

Create another virtual host by creating another configuration file /etc/httpd/conf.d/textospeech.conf and make the following entries

Image for post

Restart the httpd server after making the virtual host entry.

Now from the server in Beijing when we have to call the Text-to-Speech API we will call http://texttospeech.rentokillondon.com/ which is our reverse proxy virtual host in London.

This reverse proxy will proxy the request to https://texttospeech.googleapis.com/

It will include the URL context that is received in the host header and forward it to google.

Following is a sample call that will convert some text in French to a voice file in base64 text format which can be decoded to generate the MP3 voice file

Image for post

The voice file in base64 format looks as follows

Image for post

Using base64 we can decode the file to a binary mp3 format audio file

Image for post

The output file is as the audio.mp3 file

Conclusion

In this article, we showed the configuration for accessing Google API’s from Alibaba Cloud in China using Cloud Enterprise Network (CEN). However this exercise has been created for users who are using googleapi to host their custom API’s outside China and want to use the same API’s in China. You are recommended to move the API’s to Alibaba Cloud API Gateway in the long term.

Further Reading

Apache Reverse Proxy Guide

Reference:https://www.alibabacloud.com/blog/accessing-google-api-over-cen_594311?spm=a2c41.12451065.0.0

Written by

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store