arrow

Open Service APIs For Aliyun API Gateway

1. Experiment

1.1 Knowledge points

In this experiment, Alibaba Cloud API Gateway is used. This experiment demonstrates the process of connecting services to API Gateway. API Gateway provides high-performance and high-availability API hosting services to help expose your applications that are deployed on Alibaba Cloud products, such as ECS and Container Service. API Gateway allows you to publish and manage APIs, and maintain their lifecycles. You can perform simple operations to make data or services public rapidly, cost-effectively, and safely.

1.2 Experiment process

  • Deploy Django.
  • Create API Gateway.

1.3 Required cloud resources

  • ECS
  • API Gateway

1.4 Prerequisites

  • If you are using your own Alibaba Cloud account instead of the account provided by this lab for this experiment, be sure to choose the same Ubuntu 16.04 operating system for your ECS in order to run the experiment smoothly.
  • Before starting the experiment, confirm that the previous experiment has been normally closed and exited.

2. Start the experiment environment

Click Start Lab in the upper-right corner of the page to start the experiment.

image desc.

After the experiment environment is started, the system has deployed resources required by this experiment in the background, including the ECS instance, RDS instance, Server Load Balancer (SLB) instance, and OSS bucket. An account consisting of the username and password for logging on to the Alibaba Cloud console is also provided.

image desc

After the experiment environment is started and related resources are deployed, an experiment countdown starts. You have two hours to perform experimental operations. After the countdown ends, the experiment stops and related resources are released. During the experiment, pay attention to the remaining time and arrange your time wisely. Use the username and password provided by the system to log on to the Alibaba Cloud console and view related resources:

openCole

Go to the logon page of the Alibaba Cloud console.

image desc

Enter the sub-user account and click Next.

image desc

Enter the sub-user password and click Log on.

image desc

After you log on to the console, the following page appears.

image desc

3. Deploy Django

3.1 Log on to an ECS instance

Click Elastic Compute Service, as shown in the following figure.

image desc

We can see one running ECS instance in the US West 1 region.

image desc

Copy this ECS instance’s Internet IP address and remotely log on to this ECS (Ubuntu system) instance. For details of remote login, refer to login

image desc

The default account name and password of the ECS instance:

Account name: root

Password: nkYHG890..

3.2 Install Django

After you log on, run the following command to update the APT installation source:

apt update

image desc

Run the following command to install Django:

apt install -y python3-pip && pip3 install Django==2.2.7

image desc

Run the following command to create a Django project named Labex:

django-admin startproject Labex

image desc

Run the following command to create an app named aliyun in the Labex project:

cd Labex && django-admin startapp aliyun

image desc

Run the following command to configure the default database for the Labex project:

python3 manage.py migrate

image desc

Run the vim Labex/settings.py command to open the configuration file of the Labex project and modify the settings, as shown in the following figure. Save the modifications and exit.

image desc

Run the vim aliyun/views.py command, delete the source content in the file, and copy the following content to the file. Save the modifications and exit.

This file defines the functions that are used to receive and process client requests.

from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.

def show(request):
    return HttpResponse("<h1>this is aliyun app</h1>")


def add(request):
    a = request.GET['a']
    b = request.GET['b']
    c = int(a) + int(b)
    return HttpResponse(str(c))


def subtract(request):
    a = request.GET.get('a','none_a')
    b = request.POST.get('b','none_b')
    c = int(a) - int(b)
    return HttpResponse(str(c))

image desc

Run the vim Labex/urls.py command to open the URL file of the Labex project and modify the file, as shown in the following figure. Save the modifications and exit.

from aliyun import views

path('', views.show),
path('add/', views.add, name='add'),
path('subtract/', views.subtract, name='subtract'),

image desc

Run the following command to start Django:

python3 manage.py runserver 0.0.0.0:80

image desc

4. Create API Gateway

4.1 Create an API group

Return to the Alibaba Cloud console and access the API Gateway console, as shown in the following figure.

image desc

image desc

Create an API group, as shown in the following figure.

image desc

Set Group Name and click Confirm.

image desc

Click OK.

image desc

image desc

4.2 Create a VPC access

Create a Virtual Private Cloud (VPC) access, as shown in the following figure.

image desc

Set VPC Access Name, VPC Id, Instance Id Or IP, and Instance Port, and click Confirm, as shown in the following figure.

image desc

Click VPC instances, as shown in the preceding figure. On the page that appears, obtain the VPC ID, as shown in the following figure.

image desc

Obtain the ECS ID, as shown in the following figure.

image desc

Create success.

image desc

4.3 Create an API

Create an API to receive parameters in the GET request, as shown in the following figure.

image desc

Set Group, API Name, and Security Certification, and then click Next.

image desc

Set the frontend request path and parameters of the API, and then click Next.

image desc

Set the backend request path and parameters of the API, enter the name of the created VPC access, and click Next.

image desc

Click Create.

image desc

Click Confirm.

image desc

You can see that the API has been created. Then, click Publish.

image desc

Select Test, enter test online in the Enter Change Remarks field, and click Publish to publish the API in the test environment, as shown in the following figure.

image desc

You can see that the API has been published in the test environment.

image desc

Create the second API to receive parameters in both the GET and POST requests.

Click Create API.

image desc

Set API Name and click Next.

image desc

Set the frontend request path and parameters, with one parameter as the Query parameter and the other as the Body parameter. Set HTTP Method to POST and click Next.

image desc

Set the backend request path and parameters.

image desc

Click Next.

image desc

Click Create.

image desc

Publish api2 in the test environment with the same method.

image desc

4.4 Authorize an app

To test whether the APIs can be normally used, create an app.

image desc

Set APP Name and click Confirm.

image desc

Click Confirm.

image desc

You can access the corresponding API only after authorizing the app.

Authorize labex_app to use api1 first.

image desc

Select Test, enter the keyword in the search box, click Search, and then click Add. api1 is published in the test environment. If api1 is published in the production environment, select Release.

image desc

After the app is added, click Confirm.

image desc

Use the same method to authorize labex_app to use api2.

4.5 Call APIs

After the app is authorized to use both APIs, click the app name to go to the APP details page.

image desc

You can see both APIs. Click Debug API for api1.

image desc

Enter any two numbers in the text boxes of the Query parameters and click Send Request, as shown in the following figure.

image desc

You can see that the response failed.

image desc

Return to the CLI of ECS instances. The following errors occur on the Django server. Note the IP address in the following error information, which needs to be added to the Django configuration file later.

image desc

Press Ctrl+C to stop the service.

image desc

Run the vim Labex/settings.py command to open the Django configuration file, and add the noted IP address to the file, as shown in the following figure. Save the modification and exit.

image desc

Run the following command to start Django:

python3 manage.py runserver 0.0.0.0:80

image desc

Return to the Alibaba Cloud console and click Send Request again. You can see a normal response this time.

image desc

<font color='red'>The user can cut off the above result picture when doing the experiment and send it to the teacher, indicating that the part of the current chapter has been completed.</font>

Click Debug API for api2.

image desc

Set the Query and Body parameters and click Send Request. You can see the response.

image desc

Now, the APIs have been successfully called.

<font color='red'>Users can cut off the above result picture when they are doing the experiment and send it to the teacher, indicating that the current experiment has been completed.</font>

Tips:
Before you leave this lab, remember to log out from your Alibaba RAM account before clicking the stop button of your lab. Otherwise, you will encounter some issues when opening a new lab session in the same browser.

image descimage desc

5. Experiment summary

This experiment demonstrates the process of connecting services to API Gateway. Alibaba Cloud API Gateway uses a distributed deployment and automatic scaling model to respond to a large number of API access requests with very low latency. It provides highly secure and efficient gateway functions for your backend services. API Gateway can also handle complex transactions, such as API reference maintenance, SDK maintenance, and API version management. This greatly reduces daily maintenance costs.