Blueprint Detailed Guide
Blueprint Overview
When creating Cloud Functions, you can set a Blueprint to utilize the Runtime source code provided by Cloud Functions. Refer to the following for the Blueprint items provided by Cloud Functions.
| Category | Detailed description | Remarks |
|---|---|---|
| Hello World | When the function is invoked, it responds with Hello Serverless World! | |
| Execution after timeout | It outputs code that should run after the function call timeout but does not execute. | PHP, Python not supported |
| HTTP request body | Parse the request body. | PHP not supported |
| Send HTTP requests | The Cloud function sends an HTTP request. | PHP not supported |
| Print logs | Logs the user’s Samsung Cloud Platform Console request. | PHP not supported |
| Throw a custom error | Enter the error logic directly to handle the error. | |
| Using Environment Variable | Configure environment variables within the Cloud function and execute it. |
Hello World
Hello World Explains the response-receiving configuration and a function call example (using the function URL).
Hello World Setup
To set up Hello World, follow these steps.
Click the All Services > Compute > Cloud Functions menu. Go to the Service Home page of Cloud Functions.
On the Service Home page, click the Function menu. You will be taken to the Function List page.
Function List page, click the resource to be called via URL. You will be taken to the Function Detail page.
After clicking the Configuration tab, click the Edit button for the Function URL item. The Edit Function URL popup window opens.
In the Function URL Edit popup, set Activation status to Enabled, then click the Confirm button.
Category Detailed description Enable status Configure the use of the function URL Authentication type Select whether to use IAM authentication for requests to the function URL Access control Add accessible IPs to enable management - Set to Use, then you can input and add a public access IP
Table. Required input fields when adding a triggerAfter navigating to the Code tab, click the Edit button. You will be taken to the Function Code Edit page.
After adding the handling logic for success and failure cases, click the Save button.
- Node.js source codeColor mode
exports.handleRequest = async function (params) { /** * @description User writing area (Function details) */ const response = { statusCode: 200, body: JSON.stringify('Hello Serverless World!'), }; return response; };exports.handleRequest = async function (params) { /** * @description User writing area (Function details) */ const response = { statusCode: 200, body: JSON.stringify('Hello Serverless World!'), }; return response; };Hello World - Node.js source code - Python source codeColor mode
import json def handle_request(params): # User writing area (Function details) return { 'statusCode': 200, 'body': json.dumps('Hello Serverless World!') }import json def handle_request(params): # User writing area (Function details) return { 'statusCode': 200, 'body': json.dumps('Hello Serverless World!') }Hello World - Python source code - PHP source codeColor mode
<?php function handle_request() { # User writing area (Function details) $res = array( 'statusCode' => 200, 'body' => 'Hello Serverless World!', ); return $res; } ?><?php function handle_request() { # User writing area (Function details) $res = array( 'statusCode' => 200, 'body' => 'Hello Serverless World!', ); return $res; } ?>Hello World - PHP source code
- Node.js source code
Check function call
On the Function Details page, in the Configuration tab, invoke the function URL and then verify the response.
Hello Serverless World!
Execution after timeout
Describes configuring execution after timeout (Execution after timeout) and provides an example of invoking the function (using the function URL).
Configure execution after timeout
To set Execution after timeout, follow these steps.
- Click the All Services > Compute > Cloud Functions menu. Navigate to the Service Home page of Cloud Functions.
- On the Service Home page, click the Function menu. You will be taken to the Function List page.
- On the Function List page, click the resource for which you want to set a trigger. You will be taken to the Function Details page.
- After clicking the Trigger tab, click the Add Trigger button. The Add Trigger popup window opens.
- Add Trigger In the popup window, after selecting the Trigger Type item, enter the required information displayed at the bottom and click the OK button.
- Required information varies depending on the trigger type.
Trigger Types Input field API Gateway - API name: Select an existing API or create a new one
- Stage: Select an existing stage or create a new one
Cronjob - Refer to the example and enter the trigger’s repeat frequency(minute, hour, day, month, day of week)
- Timezone setting: select the reference time zone to apply
Table. Required input fields when adding a trigger
- Required information varies depending on the trigger type.
- After moving to the Code tab, click the Edit button. You will be taken to the Function Code Edit page.
- After adding the handling logic for success and failure cases, click the Save button.
- Node.js source codeColor mode
exports.handleRequest = async function (params) { /** * @description User writing area (Function details) */ console.log("Hello world 3"); await delay(3000); const response = { statusCode: 200, body: JSON.stringify('Hello Serverless World!'), }; return response; }; const delay = (ms) => { return new Promise(resolve=>{ setTimeout(resolve,ms) }) }exports.handleRequest = async function (params) { /** * @description User writing area (Function details) */ console.log("Hello world 3"); await delay(3000); const response = { statusCode: 200, body: JSON.stringify('Hello Serverless World!'), }; return response; }; const delay = (ms) => { return new Promise(resolve=>{ setTimeout(resolve,ms) }) }Execution after timeout - Node.js source code
- Node.js source code
Check function call
On the Function Detail page’s Configuration tab, invoke the function URL and, after a brief period, check the response.
Hello Serverless World!
HTTP request body
Explains the configuration for parsing the Request Body and an example of calling the function (using the function URL).
Setting HTTP request body
To set the HTTP request body, follow these steps.
- Click the All Services > Compute > Cloud Functions menu. Navigate to the Service Home page of Cloud Functions.
- On the Service Home page, click the Function menu. You will be taken to the Function List page.
- Function List page, click the resource to set the trigger. You will be taken to the Function Details page.
- After clicking the Trigger tab, click the Add Trigger button. The Add Trigger popup window opens.
- Add Trigger In the popup window, select the Trigger Type option, then fill in the required information shown at the bottom and click the OK button.
- Required information varies depending on the trigger type.
Trigger Types Input field API Gateway - API name: Select an existing API or create a new one
- Stage: Select an existing stage or create a new one
Cronjob - Refer to the example and enter the trigger’s repeat frequency (minutes, hours, day, month, day of week)
- Timezone setting: select the reference time zone to apply
Table. Required input fields when adding a trigger
- Required information varies depending on the trigger type.
- After moving to the Code tab, click the Edit button. You will be taken to the Function Code Edit page.
- After adding the handling logic for success and failure cases, click the Save button.
- Node.js source codeColor mode
exports.handleRequest = async function (params) { /** * @description User writing area (Function details) */ const response = { statusCode: 200, body: JSON.stringify(params.body), }; return response; };exports.handleRequest = async function (params) { /** * @description User writing area (Function details) */ const response = { statusCode: 200, body: JSON.stringify(params.body), }; return response; };Execution after timeout - Node.js source code - Python source codeColor mode
import json def handle_request(params): # User writing area (Function details) return { 'statusCode': 200, 'body': json.dumps(params.json) }import json def handle_request(params): # User writing area (Function details) return { 'statusCode': 200, 'body': json.dumps(params.json) }Execution after timeout - Python source code
- Node.js source code
Check function call
In the Configuration tab of the Function Details page, after calling the Function URL, check the Body data, request Body value, and response Body value.
Request Body value
Color mode{ "testKey" :"cloud-001", "testNames": [ { "name": "Son" }, { "name": "Kim" } ], "testCode":"test" }{ "testKey" :"cloud-001", "testNames": [ { "name": "Son" }, { "name": "Kim" } ], "testCode":"test" }Request Body value Response Body value
Color mode{ "testKey" :"cloud-001", "testNames": [ { "name": "Son" }, { "name": "Kim" } ], "testCode":"test" }{ "testKey" :"cloud-001", "testNames": [ { "name": "Son" }, { "name": "Kim" } ], "testCode":"test" }Response Body value
Send HTTP requests
Explains the HTTP request configuration and an example of calling a function (using the function URL).
Send HTTP requests Configure
To configure Send HTTP requests, follow these steps.
- Click the All Services > Compute > Cloud Functions menu. Navigate to the Service Home page of Cloud Functions.
- On the Service Home page, click the Function menu. You will be taken to the Function List page.
- Click the resource to set the trigger on the Function List page. Go to the Function Details page.
- After clicking the Trigger tab, click the Add Trigger button. The Add Trigger popup window opens.
- Add Trigger In the popup window, after selecting the Trigger Type item, enter the required information displayed at the bottom and click the OK button.
- Required information varies depending on the trigger type.
Trigger Types Input field API Gateway - API name: Select an existing API or create a new one
- Stage: Select an existing stage or create a new one
Cronjob - Refer to the example and enter the trigger’s repeat frequency (minutes, hours, day, month, day of week)
- Timezone setting: select the reference time zone to apply
Table. Required input items when adding a trigger
- Required information varies depending on the trigger type.
- After moving to the Code tab, click the Edit button. You will be taken to the Function Code Edit page.
- After adding the handling logic for success and failure cases, click the Save button.
- Node.js source codeColor mode
const request = require('request'); /** * @description User writing area (Function details) */ exports.handleRequest = async function (params) { return await sendRequest(params); }; async function sendRequest(req) { return new Promise((resolve, reject) => { // Port 80 and Port 443 are available url = "https://example.com"; // Destination URL const options = { uri: url, method:'GET', json: true, strictSSL: false, rejectUnauthorized: false } request(options, (error, response, body) => { if (error) { reject(error); } else { resolve({ statusCode: response.statusCode, body: JSON.stringify(body) }); } }); }); }const request = require('request'); /** * @description User writing area (Function details) */ exports.handleRequest = async function (params) { return await sendRequest(params); }; async function sendRequest(req) { return new Promise((resolve, reject) => { // Port 80 and Port 443 are available url = "https://example.com"; // Destination URL const options = { uri: url, method:'GET', json: true, strictSSL: false, rejectUnauthorized: false } request(options, (error, response, body) => { if (error) { reject(error); } else { resolve({ statusCode: response.statusCode, body: JSON.stringify(body) }); } }); }); }Send HTTP requests - Node.js source code - Python source codeColor mode
import json import requests def handle_request(params): # User writing area (Function details) # Port 80 and Port 443 are available url = "https://example.com" # Destination URL try: response = requests.get(url, verify=True) return { 'statusCode': response.status_code, 'body': json.dumps(response.text) } except requests.exceptions.RequestException as e: return str(e)import json import requests def handle_request(params): # User writing area (Function details) # Port 80 and Port 443 are available url = "https://example.com" # Destination URL try: response = requests.get(url, verify=True) return { 'statusCode': response.status_code, 'body': json.dumps(response.text) } except requests.exceptions.RequestException as e: return str(e)Send HTTP requests - Python source code
- Node.js source code
Check Function Call
On the Function Details page, after invoking the function URL in the Configuration tab, verify the response.
<!doctype html>
<html>
<head>
<title>Example Domain</title>
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style type="text/css">
body {
background-color: #f0f0f2;
margin: 0;
padding: 0;
font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
}
div {
width: 600px;
margin: 5em auto;
padding: 2em;
background-color: #fdfdff;
border-radius: 0.5em;
box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);
}
a:link, a:visited {
color: #38488f;
text-decoration: none;
}
@media (max-width: 700px) {
div {
margin: 0 auto;
width: auto;
}
}
</style>
</head>
<body>
<div>
<h1>Example Domain</h1>
<p>This domain is for use in illustrative examples in documents. You may use this
domain in literature without prior coordination or asking for permission.</p>
<p><a href="https://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html><!doctype html>
<html>
<head>
<title>Example Domain</title>
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style type="text/css">
body {
background-color: #f0f0f2;
margin: 0;
padding: 0;
font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
}
div {
width: 600px;
margin: 5em auto;
padding: 2em;
background-color: #fdfdff;
border-radius: 0.5em;
box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);
}
a:link, a:visited {
color: #38488f;
text-decoration: none;
}
@media (max-width: 700px) {
div {
margin: 0 auto;
width: auto;
}
}
</style>
</head>
<body>
<div>
<h1>Example Domain</h1>
<p>This domain is for use in illustrative examples in documents. You may use this
domain in literature without prior coordination or asking for permission.</p>
<p><a href="https://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>Print logs
This explains how to configure log output and an example of calling a function (using the function URL).
Configure Print logs
Print logs To set up response receiving, follow these steps.
- Click the All Services > Compute > Cloud Functions menu. Navigate to the Service Home page of Cloud Functions.
- On the Service Home page, click the Function menu. You will be taken to the Function List page.
- Function List page, click the resource to set the trigger. Function Details page will be displayed.
- After clicking the Trigger tab, click the Add Trigger button. The Add Trigger popup window opens.
- In the Add Trigger popup, select the Trigger Type item, then enter the required information displayed at the bottom and click the OK button.
- Required information varies depending on the trigger type.
Trigger Types Input field API Gateway - API name: Select an existing API or create a new one
- Stage: Select an existing stage or create a new one
Cronjob - Refer to the example and enter the trigger’s repeat frequency(minutes, hours, day, month, day of week)
- Timezone setting: select the reference time zone to apply
Table. Required input fields when adding a trigger
- Required information varies depending on the trigger type.
- After moving to the Code tab, click the Edit button. You will be taken to the Function Code Edit page.
- After adding the handling logic for success and failure cases, click the Save button.
- Node.js source codeColor mode
const winston = require('winston'); // Log module setting const logger = winston.createLogger({ format: winston.format.combine( winston.format.timestamp(), winston.format.printf(info => info.timestamp + ' ' + info.level + ': ' + info.message) ), transports: [ new winston.transports.Console() ] }); exports.handleRequest = async function (params) { /** * @description User writing area (Function details) */ const response = { statusCode: 200, body: JSON.stringify(params.body), }; logger.info(JSON.stringify(response, null, 2)); return response; };const winston = require('winston'); // Log module setting const logger = winston.createLogger({ format: winston.format.combine( winston.format.timestamp(), winston.format.printf(info => info.timestamp + ' ' + info.level + ': ' + info.message) ), transports: [ new winston.transports.Console() ] }); exports.handleRequest = async function (params) { /** * @description User writing area (Function details) */ const response = { statusCode: 200, body: JSON.stringify(params.body), }; logger.info(JSON.stringify(response, null, 2)); return response; };Print logs - Node.js source code - Python source codeColor mode
import json import logging # Log module setting logging.basicConfig(level=logging.INFO) def handle_request(params): # User writing area (Function details) response = { 'statusCode': 200, 'body': json.dumps(params.json) } logging.info(response) return responseimport json import logging # Log module setting logging.basicConfig(level=logging.INFO) def handle_request(params): # User writing area (Function details) response = { 'statusCode': 200, 'body': json.dumps(params.json) } logging.info(response) return responsePrint logs - Python source code
- Node.js source code
Check Function Call
After calling the function URL in the Configuration tab of the Function Details page, check the log in the Log tab.
[2023-09-07] 12:06:23] "host": "scf-xxxxxxxxxxxxxxxxxxxxx",
[2023-09-07] 12:06:23] "ce-id": "xxxxxxxxxxxxxxxxxxxxx",
[2023-09-07] 12:06:23] "ce-source": "xxxxxxxxxxxxxxxxxxxxx",[2023-09-07] 12:06:23] "host": "scf-xxxxxxxxxxxxxxxxxxxxx",
[2023-09-07] 12:06:23] "ce-id": "xxxxxxxxxxxxxxxxxxxxx",
[2023-09-07] 12:06:23] "ce-source": "xxxxxxxxxxxxxxxxxxxxx",Throw a custom error
Explains setting up a custom error (Throw a custom error) and an example of calling a function (using a function URL).
Configure Throw a custom error
To configure Throw a custom error, follow these steps.
- Click the All Services > Compute > Cloud Functions menu. Navigate to the Service Home page of Cloud Functions.
- On the Service Home page, click the Function menu. You will be taken to the Function List page.
- Click the resource to set the trigger on the Function List page. Go to the Function Details page.
- After clicking the Trigger tab, click the Add Trigger button. The Add Trigger popup window opens.
- Add Trigger In the popup window, after selecting the Trigger Type item, enter the required information displayed at the bottom and click the OK button.
- Required information varies depending on the trigger type.
Trigger Types Input field API Gateway - API name: Select an existing API or create a new one
- Stage: Select an existing stage or create a new one
Cronjob - Refer to the example and enter the trigger’s repeat frequency (minutes, hours, day, month, day of week)
- Timezone setting: select the reference time zone to apply
Table. Required input fields when adding a trigger
- Required information varies depending on the trigger type.
- After moving to the Code tab, click the Edit button. You will be taken to the Function Code Edit page.
- After adding the handling logic for success and failure cases, click the Save button.
- Node.js source codeColor mode
class CustomError extends Error { constructor(message) { super(message); this.name = 'CustomError'; } } exports.handleRequest = async function (params) { /** * @description User writing area (Function details) */ throw new CustomError('This is a custom error!'); };class CustomError extends Error { constructor(message) { super(message); this.name = 'CustomError'; } } exports.handleRequest = async function (params) { /** * @description User writing area (Function details) */ throw new CustomError('This is a custom error!'); };Throw a custom error - Node.js source code - Python source codeColor mode
class CustomError(Exception): def __init__(self, message): self.message = message def handle_request(parmas): raise CustomError('This is a custom error!')class CustomError(Exception): def __init__(self, message): self.message = message def handle_request(parmas): raise CustomError('This is a custom error!')Throw a custom error - Python source code - PHP source codeColor mode
<?php class CustomError extends Exception { public function __construct($message) { parent::__construct($message); $this->message = $message; } } function handle_request() { throw new CustomError('This is a custom error!'); } ?><?php class CustomError extends Exception { public function __construct($message) { parent::__construct($message); $this->message = $message; } } function handle_request() { throw new CustomError('This is a custom error!'); } ?>Throw a custom error - PHP source code
- Node.js source code
Check Function Call
On the Function Details page, after calling the Function URL in the Configuration tab, verify whether an error occurred in the Log tab.
Using Environment Variable
Explains the use of environment variables (Using Environment Variable) settings and a function call example (using function URL).
Using Environment Variable Configure
To configure Using Environment Variable, follow these steps.
- Click the All Services > Compute > Cloud Functions menu. Navigate to the Service Home page of Cloud Functions.
- On the Service Home page, click the Function menu. You will be taken to the Function List page.
- Click the resource to set the trigger on the Function List page. Go to the Function Details page.
- After clicking the Trigger tab, click the Add Trigger button. The Add Trigger popup window opens.
- In the Add Trigger popup, select the Trigger Type item, then enter the required information displayed at the bottom and click the OK button.
- Required information varies depending on the trigger type.
Trigger Types Input field API Gateway - API name: Select an existing API or create a new one
- Stage: Select an existing stage or create a new one
Cronjob - Refer to the example and enter the trigger’s repeat frequency(minutes, hours, days, months, day of week)
- Timezone setting: select the reference time zone to apply
Table. Required input fields when adding a trigger
- Required information varies depending on the trigger type.
- After moving to the Code tab, click the Edit button. You will be taken to the Function Code Edit page.
- After adding the handling logic for success and failure cases, click the Save button.
- Node.js source codeColor mode
exports.handleRequest = async function (params) { /** * @description User writing area (Function details) */ return process.env.test; };exports.handleRequest = async function (params) { /** * @description User writing area (Function details) */ return process.env.test; };Using Environment Variable - Node.js source code - Python source codeColor mode
import json import os def handle_request(params): # User writing area (Function details) return os.environ.get("test")import json import os def handle_request(params): # User writing area (Function details) return os.environ.get("test")Using Environment Variable - Python source code - PHP source codeColor mode
import json def handle_request(params): # User writing area (Function details) return os.environ.get("test")import json def handle_request(params): # User writing area (Function details) return os.environ.get("test")Using Environment Variable - PHP source code
- Node.js source code
- After moving to the Configuration tab, click the Edit button in the Environment Variables area. The Edit Environment Variables popup will open.
- After entering the environment variable information, click the Confirm button.
Category Detailed description Name Enter the key value value Enter the value Table. Environment Variable Input Items
Check function call
On the Function Details page, after calling the function URL in the Configuration tab, check the environment variable value in the Log tab.