×
Community Blog Accessing External APIs over CEN

Accessing External APIs over CEN

In this article, we will configure a Reverse Proxy to Access External APIs from China over Alibaba Cloud's Cloud Enterprise Network (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 external APIs, there is a bit of configuration required to get it working from China. This article shows how to configure a Reverse Proxy to access external 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.

Note: This article is for demo purposes only. All services hosted in China shall comply with local regulations.

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

1

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.

2

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

3

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

4

5

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

6

For SSL to be supported you need to install mod_ssl

7

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

8

The request for google translate will be received by this virtual host "translate.xxxxxlondon.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

9

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

10

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 https://texttospeech.xxxxxlondon.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

11

The voice file in base64 format looks as follows

12

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

13

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

0 0 0
Share on

Alibaba Clouder

2,605 posts | 747 followers

You may also like

Comments

Alibaba Clouder

2,605 posts | 747 followers

Related Products

  • CEN

    A global network for rapidly building a distributed business system and hybrid cloud to help users create a network with enterprise level-scalability and the communication capabilities of a cloud network

    Learn More
  • Networking Overview

    Connect your business globally with our stable network anytime anywhere.

    Learn More
  • API Gateway

    API Gateway provides you with high-performance and high-availability API hosting services to deploy and release your APIs on Alibaba Cloud products.

    Learn More
  • Edge Network Acceleration

    Establish high-speed dedicated networks for enterprises quickly

    Learn More