Files
doc-exports/docs/fg/umn/functiongraph_04_0101.html
liusiying01 904c1aad8b FG UMN 20250526 version
Reviewed-by: Mützel, Andrea <andrea.muetzel@t-systems.com>
Co-authored-by: liusiying01 <liusiying@huawei.com>
Co-committed-by: liusiying01 <liusiying@huawei.com>
2025-10-06 15:14:20 +00:00

134 lines
16 KiB
HTML

<a name="functiongraph_04_0101"></a><a name="functiongraph_04_0101"></a>
<h1 class="topictitle1">Creating a Function from Scratch and Executing the Function</h1>
<div id="body0000001297194441"><p id="functiongraph_04_0101__p129935814336">This section describes how to quickly create and test a HelloWorld function on the FunctionGraph console.</p>
<div class="section" id="functiongraph_04_0101__section0418520193311"><h4 class="sectiontitle">Prerequisites</h4><ol id="functiongraph_04_0101__ol8106132642719"><li id="functiongraph_04_0101__li4257144418303">Grant the FunctionGraph operation permissions to the user.<p id="functiongraph_04_0101__p722313491535"><a name="functiongraph_04_0101__li4257144418303"></a><a name="li4257144418303"></a>To perform the operations described in this section, ensure that you have the <strong id="functiongraph_04_0101__b14529817145619">FunctionGraph FullAccess</strong> permissions, that is, all permissions for FunctionGraph. For more information, see section <span class="uicontrol" id="functiongraph_04_0101__uicontrol17648524536"><b>"Permissions Management"</b></span>.</p>
</li></ol>
</div>
<div class="section" id="functiongraph_04_0101__section2450193783315"><h4 class="sectiontitle">Step 1: Create a Function</h4><ol id="functiongraph_04_0101__ol1614414555101"><li id="functiongraph_04_0101__li7794535128">Log in to the FunctionGraph console. In the navigation pane, choose <strong id="functiongraph_04_0101__b4462201551817">Functions</strong> &gt; <strong id="functiongraph_04_0101__b1946212154188">Function List</strong>.</li><li id="functiongraph_04_0101__li27941371211">Click <strong id="functiongraph_04_0101__b17311142317202">Create Function</strong> in the upper right corner and choose <strong id="functiongraph_04_0101__b143807309244">Create from scratch</strong>.</li><li id="functiongraph_04_0101__li12226824191318">Configure basic function information by referring to <a href="#functiongraph_04_0101__fig290685013818">Figure 1</a>. Then, click <strong id="functiongraph_04_0101__b15399201685713">Create Function</strong>.
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="functiongraph_04_0101__table11741142114328" frame="border" border="1" rules="all"><caption><b>Table 1 </b>Parameters required for creating a function</caption><thead align="left"><tr id="functiongraph_04_0101__row0741172163213"><th align="left" class="cellrowborder" valign="top" width="13.209999999999999%" id="mcps1.3.3.2.3.3.2.4.1.1"><p id="functiongraph_04_0101__p18741122133215">Parameter</p>
</th>
<th align="left" class="cellrowborder" valign="top" width="62.519999999999996%" id="mcps1.3.3.2.3.3.2.4.1.2"><p id="functiongraph_04_0101__p474122113328">Description</p>
</th>
<th align="left" class="cellrowborder" valign="top" width="24.27%" id="mcps1.3.3.2.3.3.2.4.1.3"><p id="functiongraph_04_0101__p18696825172014">Example Value</p>
</th>
</tr>
</thead>
<tbody><tr id="functiongraph_04_0101__row174182193220"><td class="cellrowborder" valign="top" width="13.209999999999999%" headers="mcps1.3.3.2.3.3.2.4.1.1 "><p id="functiongraph_04_0101__p2741521153220">Function Type</p>
</td>
<td class="cellrowborder" valign="top" width="62.519999999999996%" headers="mcps1.3.3.2.3.3.2.4.1.2 "><p id="functiongraph_04_0101__p1810868253">Select <strong id="functiongraph_04_0101__b53078599572">Event Function</strong>.</p>
<p id="functiongraph_04_0101__p78971443127">An event function is triggered by a specific event, which is usually a request event in JSON format.</p>
</td>
<td class="cellrowborder" valign="top" width="24.27%" headers="mcps1.3.3.2.3.3.2.4.1.3 "><p id="functiongraph_04_0101__p16961725142012">Event Function</p>
</td>
</tr>
<tr id="functiongraph_04_0101__row172206201017"><td class="cellrowborder" valign="top" width="13.209999999999999%" headers="mcps1.3.3.2.3.3.2.4.1.1 "><p id="functiongraph_04_0101__p142206201509">Region</p>
</td>
<td class="cellrowborder" valign="top" width="62.519999999999996%" headers="mcps1.3.3.2.3.3.2.4.1.2 "><p id="functiongraph_04_0101__p1122015201508">Select the region where the function is located.</p>
<p id="functiongraph_04_0101__p1928725213513">Regions are geographic areas isolated from each other. Resources are region-specific and cannot be used across regions through internal network connections. Select a region near you to ensure the lowest latency possible.</p>
</td>
<td class="cellrowborder" valign="top" width="24.27%" headers="mcps1.3.3.2.3.3.2.4.1.3 "><p id="functiongraph_04_0101__p16961925102010">eu-de</p>
</td>
</tr>
<tr id="functiongraph_04_0101__row1774262183216"><td class="cellrowborder" valign="top" width="13.209999999999999%" headers="mcps1.3.3.2.3.3.2.4.1.1 "><p id="functiongraph_04_0101__p674215214327">Function Name</p>
</td>
<td class="cellrowborder" valign="top" width="62.519999999999996%" headers="mcps1.3.3.2.3.3.2.4.1.2 "><p id="functiongraph_04_0101__p37421421143219">Enter a function name. The naming rules are as follows:</p>
<ul id="functiongraph_04_0101__ul107421021143216"><li id="functiongraph_04_0101__li1374232143211">Consists of 1 to 60 characters, and can contain letters, digits, hyphens (-), and underscores (_).</li><li id="functiongraph_04_0101__li1574215214327">Start with a letter and end with a letter or digit.</li></ul>
</td>
<td class="cellrowborder" valign="top" width="24.27%" headers="mcps1.3.3.2.3.3.2.4.1.3 "><p id="functiongraph_04_0101__p1169613251201">HelloWorld</p>
</td>
</tr>
<tr id="functiongraph_04_0101__row1051811599242"><td class="cellrowborder" valign="top" width="13.209999999999999%" headers="mcps1.3.3.2.3.3.2.4.1.1 "><p id="functiongraph_04_0101__p35181859112410">Enterprise Project</p>
</td>
<td class="cellrowborder" valign="top" width="62.519999999999996%" headers="mcps1.3.3.2.3.3.2.4.1.2 "><p id="functiongraph_04_0101__p10210144615596">Select the enterprise project to which the function belongs. Enterprise projects let you manage cloud resources and users by project.</p>
<p id="functiongraph_04_0101__p12518159192418">The default value is <strong id="functiongraph_04_0101__b17960205818116">default</strong>. You can select the created enterprise project.</p>
</td>
<td class="cellrowborder" valign="top" width="24.27%" headers="mcps1.3.3.2.3.3.2.4.1.3 "><p id="functiongraph_04_0101__p16799134714249">default</p>
</td>
</tr>
<tr id="functiongraph_04_0101__row6743132111329"><td class="cellrowborder" valign="top" width="13.209999999999999%" headers="mcps1.3.3.2.3.3.2.4.1.1 "><p id="functiongraph_04_0101__p18743112119325">Agency</p>
</td>
<td class="cellrowborder" valign="top" width="62.519999999999996%" headers="mcps1.3.3.2.3.3.2.4.1.2 "><p id="functiongraph_04_0101__p14304185017237">Select an agency for the function. An agency is used to authorize FunctionGraph to access other cloud services. If FunctionGraph does not access any cloud service, you do not need to select an agency.</p>
<p id="functiongraph_04_0101__p7542182992319">By default, <strong id="functiongraph_04_0101__b13299136526">Use no agency</strong> is used. You can select an existing agency.</p>
</td>
<td class="cellrowborder" valign="top" width="24.27%" headers="mcps1.3.3.2.3.3.2.4.1.3 "><p id="functiongraph_04_0101__p1078694184517">Use no agency</p>
</td>
</tr>
<tr id="functiongraph_04_0101__row1396594224515"><td class="cellrowborder" valign="top" width="13.209999999999999%" headers="mcps1.3.3.2.3.3.2.4.1.1 "><p id="functiongraph_04_0101__p18965542164519">Permission Policies</p>
</td>
<td class="cellrowborder" valign="top" width="62.519999999999996%" headers="mcps1.3.3.2.3.3.2.4.1.2 "><p id="functiongraph_04_0101__p1452318171558"><strong id="functiongraph_04_0101__b65614191544">This parameter is displayed only when an agency is selected.</strong></p>
<p id="functiongraph_04_0101__p1129362412463">For details about how to adjust the permission policies on the IAM console, see section <span class="uicontrol" id="functiongraph_04_0101__uicontrol1614234537"><b>"Configuring Agency Permissions"</b></span>.</p>
</td>
<td class="cellrowborder" valign="top" width="24.27%" headers="mcps1.3.3.2.3.3.2.4.1.3 "><p id="functiongraph_04_0101__p211113919451">-</p>
</td>
</tr>
<tr id="functiongraph_04_0101__row7353105614716"><td class="cellrowborder" valign="top" width="13.209999999999999%" headers="mcps1.3.3.2.3.3.2.4.1.1 "><p id="functiongraph_04_0101__p5354115613478">Runtime</p>
</td>
<td class="cellrowborder" valign="top" width="62.519999999999996%" headers="mcps1.3.3.2.3.3.2.4.1.2 "><p id="functiongraph_04_0101__p6855173613013">Select a runtime to compile the function.</p>
<ul id="functiongraph_04_0101__ul5559452195219"><li id="functiongraph_04_0101__li185598527523">CloudIDE supports only online editing of Node.js, Python, PHP, and custom runtimes.</li><li id="functiongraph_04_0101__li3289254175220">After a function is created, the runtime language cannot be changed.</li></ul>
</td>
<td class="cellrowborder" valign="top" width="24.27%" headers="mcps1.3.3.2.3.3.2.4.1.3 "><p id="functiongraph_04_0101__p069752517203">Node.js 16.17</p>
</td>
</tr>
</tbody>
</table>
</div>
<ul id="functiongraph_04_0101__ul0608101195812"><li id="functiongraph_04_0101__li9653740175611"><strong id="functiongraph_04_0101__b138464532075">Advanced Settings</strong>: <strong id="functiongraph_04_0101__b177989591277">Collect Logs</strong> is disabled by default. If it is enabled, function execution logs will be reported to Log Tank Service (LTS). You will be billed for log management on a pay-per-use basis.
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="functiongraph_04_0101__table586619443117" frame="border" border="1" rules="all"><caption><b>Table 2 </b>Parameters for configuring Collect Logs</caption><thead align="left"><tr id="functiongraph_04_0101__row686618443112"><th align="left" class="cellrowborder" valign="top" width="32.31%" id="mcps1.3.3.2.3.4.1.3.2.3.1.1"><p id="functiongraph_04_0101__p14866749313">Parameter</p>
</th>
<th align="left" class="cellrowborder" valign="top" width="67.69%" id="mcps1.3.3.2.3.4.1.3.2.3.1.2"><p id="functiongraph_04_0101__p178668423117">Description</p>
</th>
</tr>
</thead>
<tbody><tr id="functiongraph_04_0101__row1886615419311"><td class="cellrowborder" valign="top" width="32.31%" headers="mcps1.3.3.2.3.4.1.3.2.3.1.1 "><p id="functiongraph_04_0101__p11866144103115">Log Configuration</p>
</td>
<td class="cellrowborder" valign="top" width="67.69%" headers="mcps1.3.3.2.3.4.1.3.2.3.1.2 "><p id="functiongraph_04_0101__p1886610493115">You can select <strong id="functiongraph_04_0101__b1757312367911">Auto</strong> or <strong id="functiongraph_04_0101__b134872038395">Custom</strong>.</p>
<ul id="functiongraph_04_0101__ul894114313424"><li id="functiongraph_04_0101__li13941539425"><strong id="functiongraph_04_0101__b8972558161013">Auto</strong>: Use the default log group and log stream. Log groups prefixed with "functiongraph.log.group" are filtered out.</li><li id="functiongraph_04_0101__li1713036164219"><strong id="functiongraph_04_0101__b15597122141115">Custom</strong>: Select a custom log group and log stream. Log streams that are in the same enterprise project as your function.</li></ul>
</td>
</tr>
<tr id="functiongraph_04_0101__row1386618412312"><td class="cellrowborder" valign="top" width="32.31%" headers="mcps1.3.3.2.3.4.1.3.2.3.1.1 "><p id="functiongraph_04_0101__p286619414315">Log Tag</p>
</td>
<td class="cellrowborder" valign="top" width="67.69%" headers="mcps1.3.3.2.3.4.1.3.2.3.1.2 "><p id="functiongraph_04_0101__p138661483113">You can use these tags to filter function logs in LTS. You can add 10 more tags.</p>
<p id="functiongraph_04_0101__p144926153617">Tag key/value: Enter a maximum of 64 characters. Only digits, letters, underscores (_), and hyphens (-) are allowed.</p>
</td>
</tr>
</tbody>
</table>
</div>
</li></ul>
<div class="fignone" id="functiongraph_04_0101__fig290685013818"><a name="functiongraph_04_0101__fig290685013818"></a><a name="fig290685013818"></a><span class="figcap"><b>Figure 1 </b>Configuring basic information</span><br><span><img id="functiongraph_04_0101__image19061950281" src="en-us_image_0000001679464913.png" title="Click to enlarge" class="imgResize"></span></div>
<p id="functiongraph_04_0101__p07801174408"></p>
</li></ol><ol start="4" id="functiongraph_04_0101__ol1548345201514"><li id="functiongraph_04_0101__li548194510153">Configure the code source, copy the following code to the code window, and click <strong id="functiongraph_04_0101__b1244111149105">Deploy</strong>.<div class="p" id="functiongraph_04_0101__p8880104813446">The sample code enables you to obtain test events and print test event information.<pre class="screen" id="functiongraph_04_0101__screen0178346114419">exports.handler = function (event, context, callback) {
const error = null;
const output = `Hello message: ${JSON.stringify(event)}`;
callback(error, output);
}</pre>
</div>
</li></ol>
</div>
<div class="section" id="functiongraph_04_0101__section1054774683313"><h4 class="sectiontitle">Step 2: Test the Function</h4><ol id="functiongraph_04_0101__ol138801929154513"><li id="functiongraph_04_0101__li118801429194518">On the function details page, click <strong id="functiongraph_04_0101__b19886205913217">Test</strong>. In the displayed dialog box, create a test event.</li><li id="functiongraph_04_0101__li1355343014466">Select <strong id="functiongraph_04_0101__b4461454191814">blank-template</strong>, set <strong id="functiongraph_04_0101__b856916620194">Event Name</strong> to <strong id="functiongraph_04_0101__b187021917198">test</strong>, modify the test event as follows, and click <strong id="functiongraph_04_0101__b13337151314192">Create</strong>.<pre class="screen" id="functiongraph_04_0101__screen77821922101">{
"hello": "function"
}</pre>
<div class="fignone" id="functiongraph_04_0101__fig1940722251517"><span class="figcap"><b>Figure 2 </b>Configuring a test event</span><br><span><img id="functiongraph_04_0101__image94071722101518" src="en-us_image_0000001631465770.png" title="Click to enlarge" class="imgResize"></span></div>
</li></ol>
</div>
<div class="section" id="functiongraph_04_0101__section587765273314"><h4 class="sectiontitle">Step 3: View the Execution Result</h4><p id="functiongraph_04_0101__p552811143539">Click <strong id="functiongraph_04_0101__b17700193891519">Test</strong> and view the execution result on the right.</p>
<ul id="functiongraph_04_0101__ul1714875285210"><li id="functiongraph_04_0101__li20148952105216"><strong id="functiongraph_04_0101__b11407142313534">Function Output</strong>: displays the return result of the function.</li><li id="functiongraph_04_0101__li1659264116539"><strong id="functiongraph_04_0101__b9557132517531">Log Output</strong>: displays the execution logs of the function.</li><li id="functiongraph_04_0101__li1014811523528"><strong id="functiongraph_04_0101__b1766282416535">Summary</strong>: displays key information of the logs.<div class="fignone" id="functiongraph_04_0101__fig157061582522"><span class="figcap"><b>Figure 3 </b>Viewing the execution result</span><br><span><img id="functiongraph_04_0101__image1670668155211" src="en-us_image_0000001261803038.png" title="Click to enlarge" class="imgResize"></span></div>
</li></ul>
<div class="note" id="functiongraph_04_0101__note1035002210544"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><p id="functiongraph_04_0101__p1235012235414">A maximum of 2 KB logs can be displayed. For more log information, see <a href="functiongraph_01_0170.html">Querying Function Logs</a>.</p>
</div></div>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="functiongraph_01_0505.html">Getting Started</a></div>
</div>
</div>
<script language="JavaScript">
<!--
image_size('.imgResize');
var msg_imageMax = "view original image";
var msg_imageClose = "close";
//--></script>