A Serverless Architecture on Huawei Cloud with FunctionGraph, SMN & API Gateway (APIG) Services

Elif Meriç
Huawei Developers
Published in
7 min readMay 3, 2023
Creating a Serverless Architecture on HUAWEI CLOUD

Introduction

Hello all! 🤗👋 In this article, we will learn about how to create a serverless architecture with FunctionGraph, SMN and API Gateway (APIG) services on Huawei Cloud. With this structure, we do not need to install any serverLet’s get started 🤗

In this scenario, we will create a serverless architecture on HUAWEI CLOUD. According to the architecture below, a FunctionGraph function should be created. The functionality of this function is getting messages from POST API request, and then sending this message to Simple Message Notification (SMN) service. To to this, an APIG trigger will be added to the function. To test the structure, a POST API request will be send from Postman to APIG endpoint URL. Then, the flow will be automatically executed. To sum up, Huawei Cloud FunctionGraph, API Gateway (APIG), Simple Message Notification (SMN) services and an EIP (to enable public inbound access of APIG) will be used.

The General Architecture

✅In the first step, a FunctionGraph function should be created. To create a function, go to the FunctionGraph service in Huawei Cloud console. Then, click on the Functions tab in the menu, then go to the Function List.

Huawei Cloud FunctionGraph Console

After that, choose Create from scratch option. Then, choose the Function Type as Event Function. After that, Function Name, Agency and Runtime should be selected. The runtime should be Python 3.9

Creating a Function from Scratch in Huawei Cloud FunctionGraph Service

The supported runtimes:

The supported runtimes in FunctionGraph

✅After creating the function, an APIG trigger should be added. To do that, the first step is creating an API Gateway in APIG service. In this scenario, we will create a dedicated gateway. Click Buy Dedicated Gateway button to create it.

Creating an API Gateway in APIG

In the API Gateway creation console below, enter the gateway name, and choose the Edition as Basic for this scenario. You can enable Public Inbound Access and assign an Elastic IP (EIP) to enable inbound traffic. Also check the Public outbound access box to enable public outbound traffic. If you do not have an EIP, click Create EIP next to the select box. Then, click Next and Confirm.

Creating an API Gateway
Creating an API Gateway

✅After creating the API Gateway, an API Group should be created in the second step. Go to the API Management > API Groups tab in the menu. Then click Create API Group button to create it. Then enter the API Group name and click OK. Find the API Group name you have just created in the API Group console then click on it.

Creating an API Group

In the API Group page, navigate to the APIs tab, then click to Create button to create an API in the created API Group. In the Create API page, enter the API Name, select the API Group you have created, select the method as POST and Protocol as HTTP (it may be HTTPS, but an SSL certificate is required for HTTPS, so HTTP is used in this scenario) in the URL section.

Creating an API

For the Visibility configuration, you can select private. Public APIs that have been published in the RELEASE environment can be listed on KooGallery. Private APIs cannot be listed on KooGallery. For this scenario, Authentication Mode can be selected as None. To know more about the other authentication methods, you can visit Huawei Cloud APIG documentation page located in the references section [1]. Then, enable CORS. For the Request Parameters section, add a query string parameter named promotion_message because the API requests & messages will be send through this parameter. Then select its Parameter Type as STRING.

Create an API — Frontend Configuration

When the Frontend configurations are completed, click Next button. Then, in the Backend Configuration page, select Backend Type as FunctionGraph, select the Function URN (Click Select, then select the URN which belongs to your FunctionGraph function). After that, select Version/Alias as Version and select latest. The Invocation mode is Synchronous. Then click Finish.

Create an API — Backend Configuration

You can view your API in API Group console now. To publish your API, click on the Publish button. Also, to debug it, you can click on the debug button and check the response.

Publishing an API

Locate to the FunctionGraph console again after creating the API in APIG service. Click on the Create Trigger button and select API Gateway (Dedicated) option as a Trigger Type. Select API Instance, enter API Name, API Group, and select Security Authentication as None and Protocol will be HTTP and click OK.

Create APIG Trigger in FunctionGraph

After creating the APIG trigger in FunctionGraph, locate to the Code tab, and insert the code below to get the promotion message as query string parameter and send the message to SMN service. Finally, click on the Deploy button.

import time
import json
import requests

def handler(event, context):
promotion_message = event.get('queryStringParameters', {}).get('promotion_message')
if not promotion_message:
return {
"statusCode": 400,
"body": json.dumps({"error": "Missing promotion_message parameter"})
}

msg = {
"subject": "20% Discount Opportunity!",
"message": json.dumps(promotion_message),
"time_to_live": "120"
}
send_smn(msg, context)

return {
"statusCode": 200,
"isBase64Encoded": False,
"body": json.dumps({"message": promotion_message}),
"headers": {
"Content-Type": "application/json"
}
}

def send_smn(msg, context):
endpoint = context.getUserData('smn_endpoint')
if not endpoint:
context.getLogger().error("Environment variable 'smn_endpoint' must be configured")
return False
topic_urn = context.getUserData('smn_topic_urn')
if not topic_urn:
context.getLogger().error("Environment variable 'smn_topic_urn' must be configured")
return False
project_id = context.getUserData('smn_project_id')
if not project_id:
context.getLogger().error("Environment variable 'smn_project_id' must be configured")
return False

url = 'https://%s/v2/%s/notifications/topics/%s/publish' % (endpoint, project_id, topic_urn)
headers = {
"x-auth-token": context.getToken(),
"content-type": 'application/json'
}

resp = requests.post(url, json=msg, headers=headers)

return True

In the last step, locate to Simple Message Notification (SMN) service console. Then click on the Topic Management > Topics tab to create a topic. Topic is created to be basically used for sending messages under it. Then, click on the Create Topic button to create a topic, and enter the topic name and enterprise project and click OK.

Creating a Topic

In the next step, click the topic name you have just created, and then click on the Add Subscription button to add a subscription. A subscription

Adding a subscription

You can add subscriptions in several ways. SMN supports a few protocols such as SMS, Email, HTTP, HTTPS, FunctionGraph (function). With these protocols, the messages can be send in different ways. For example, if you select SMS as a protocol, you should define the endpoint as a phone number with a country code, but if the protocol is Email, you should define its endpoint as an email address. If you want to know more about SMN, you can view Huawei Cloud SMN Documentation page which can be found in the references section [2].

Add a subscription

After you created the SMN topic and subscriptions, go back to FunctionGraph console. Locate to your function page, then click on the Configuration tab. Then go to the Environment Variables tab from the left menu, and add the environment variables as below, and enter their values.

Adding environment variables

Finally, you can send the promotion message from Postman. Firstly, get the APIG endpoint URL, and copy it to Postman URL section. Select the API method as POST in Postman, then define the promotion_message as a Query Parameter and enter its value (the message you will send). This operation will send POST API request to APIG endpoint, and the APIG will trigger FunctionGraph automatically, then the function will send this message to SMN, and SMN sends it to its subscribers (to the specified mail addresses, or mobile phone numbers).

Sending POST API Request from Postman

As a result, you can see the email sent by SMN below.

The Email Sent by SMN

Conclusion

In this article, creating a serverless FunctionGraph & SMN APIG architecture is aimed to make an automation to send promotion messages, so we firstly send the API requests from Postman to APIG, and the APIG triggers FunctionGraph function. Then, FunctionGraph sends the message comes from the API to SMN service. Thus, the SMN service sends this message to the specified endpoints.

References 📚

  1. Huawei Cloud APIG Documentation

2. Huawei Cloud SMN Documentation

3. Huawei Cloud FunctionGraph Documentation

--

--