All Products
Search
Document Center

3. Create and test your functions

Last Updated: Oct 16, 2018

Create a function

  1. Log on to the Function Compute console.

  2. Select the new service in the left-side navigation pane to go to the service page.

  3. Click Create Function to go to the Create Function page.

    1. Click Select All, and select nodejs8 or python2.7 from the drop-down list. This section takes these languages for example.

    2. Click Select in Empty Function.

    3. Select No Trigger from the Trigger Type drop-down list.

    4. Configure the Service Name, Function Name, Function Description, Runtime, and Runtime Environment parameters.

    5. Click Next.

    6. Make sure that all the information is correct, and click Create.

Write a function

You need to use a third-party module for to perform MySQL operations in the function code. Download this module to the directory of the function that Function Compute calls, package the module and the function in the same folder, and then upload this package to Function Compute. For more information about packaging the third-party module, see Install a third-party dependency.

The host in the code is the intranet IP address. You can obtain this IP address in the RDS console.

The following sections describe how to package and upload the code that uses Python 2.7 and Nodejs8.

Python 2.7

  1. Create a directory to save code and dependent modules:

    1. mkdir /tmp/code
  2. Install dependent modules in the /tmp/code directory:

    1. cd /tmp/code
    2. pip install --install-option="--install-lib=$(pwd)" PyMySQL

    You have installed PyMySQL in the /tmp/code directory.

  3. Create a new code file, such as /tmp/code/main.py, and use pymysql in the code:

    1. # -*- coding: utf-8 -*-
    2. import pymysql.cursors
    3. # Connect to the database
    4. connection = pymysql.connect(
    5. host=<host-name>,
    6. user=<user-name>,
    7. password=<password>,
    8. db=<database-name>,
    9. charset='utf8mb4',
    10. cursorclass=pymysql.cursors.DictCursor)
    11. def handler(event, context):
    12. with connection.cursor() as cursor:
    13. # Read a single record
    14. sql = "SELECT count(*) FROM `users`"
    15. cursor.execute(sql)
    16. result = cursor.fetchone()
    17. print(result)
    18. return result
  4. After installation, the content in the /tmp/code directory is as follows:

    1. ls /tmp/code
    2. PyMySQL-0.7.11.dist-info main.py pymysql

In the Code Configuration field, select Upload Folder, and upload the /tmp/code folder.

Nodejs

  1. Create a directory to save code and dependent modules:

    1. mkdir /tmp/code
  2. Install dependent modules in the /tmp/code directory:

    1. cd /tmp/code
    2. npm install mysql
  3. Create a new code file, such as /tmp/code/index.js, and use mysql in the code:

    1. var mysql = require('mysql');
    2. module.exports.handler = (event, context, callback) => {
    3. var connection = mysql.createConnection({
    4. Host: <host-name>,
    5. user: <user-name>,
    6. password: <password>,
    7. database : <database-name>
    8. });
    9. connection.query('SELECT count(*) FROM `users`', (error, results, fields) => {
    10. if (error) throw error;
    11. connection.end((err) => {
    12. callback(err, results);
    13. });
    14. });
    15. };
  4. After saving the file, the content in the /tmp/code directory is as follows:

    1. ls /tmp/code
    2. index.js node_modules

    In the Code Configuration field, select Upload Folder, and upload the /tmp/code folder.Execute the function to view the result.

Test function

Click Invoke in the console to view the function execution result. Now, your function can access the RDS instance in the VPC.

For more information about how to contact us, see Contact us.