Bootstrap File Description
In HTTP Cloud Functions, scf_bootstrap is a required startup file that serves as the entry point for the Web Server. It is responsible for starting your web service, configuring the runtime environment, loading dependencies, and ensuring the service properly listens for requests.
Purpose of the Bootstrap File
The scf_bootstrap file is automatically executed when a cloud function instance starts, primarily performing the following tasks:
| Responsibility | Description |
|---|---|
| Start Web Service | Ensures your web service starts properly and listens for requests (default port is 9000) |
| Environment Configuration | Sets paths for runtime dependency libraries, environment variables, etc. |
| Dependency Loading | Loads library files and extensions for custom languages and versions For real-time dependency pulling, it's recommended to download to the /tmp directory |
| Global Initialization | Performs global operations required before function invocation (such as HTTP Client initialization, database connection pool creation, etc.) for reuse during invocation |
| Plugin Loading | Starts security, monitoring, and other plugins |
Prerequisites
To ensure the bootstrap file can be executed correctly, the following conditions must be met:
| Requirement | Description |
|---|---|
| Fixed Filename | Must be named scf_bootstrap, other names are not supported |
| Execute Permission | File must have execute permission (recommended 777 or 755) |
| System Environment | Must be able to run in SCF system environment (CentOS 7.6) |
| Shebang Declaration | If it's a Shell script, the first line must include #!/bin/bash |
| Absolute Path | Startup commands must use absolute paths (see "Standard Language Environment Absolute Paths" below) |
| Listen Address | It's recommended to use 0.0.0.0, do not use internal loopback address 127.0.0.1 |
| File Format | File must end with LF line ending (not CRLF) |
| Write Permission Restrictions | In Tencent Cloud standard environment, only the /tmp directory is readable/writable; please note the path when outputting files |
⚠️ Important Note: On Linux/macOS systems, use the
chmod +x scf_bootstraporchmod 777 scf_bootstrapcommand to set execute permissions.
Standard Language Environment Absolute Paths
When writing startup commands, you must use the absolute path corresponding to the language. Here are common version path references:
| Language Version | Absolute Path |
|---|---|
| Node.js 18.15 | /var/lang/node18/bin/node |
| Node.js 16.13 | /var/lang/node16/bin/node |
| Node.js 14.18 | /var/lang/node14/bin/node |
| Node.js 12.16 | /var/lang/node12/bin/node |
| Python 3.10 | /var/lang/python310/bin/python3.10 |
| Python 3.9 | /var/lang/python39/bin/python3.9 |
| Python 3.7 | /var/lang/python37/bin/python3.7 |
| Python 3.6 | /var/lang/python3/bin/python3.6 |
| PHP 8.0 | /var/lang/php80/bin/php |
| PHP 7.4 | /var/lang/php74/bin/php |
| PHP 7.2 | /var/lang/php7/bin/php |
| Java 11 | /var/lang/java11/bin/java |
| Java 8 | /var/lang/java8/bin/java |
Startup Command Templates
- Node.js
- Python
- PHP
- Java
- Go
Functions Framework (Recommended)
#!/bin/bash
node node_modules/@cloudbase/functions-framework/bin/tcb-ff.js --port=9000 --logDirname=/tmp/logs --interceptOutput=false --extendedContextKey=X-Cloudbase-Context
Parameter Description:
| Parameter | Description | Default Value |
|---|---|---|
--port | Function listening port | 9000 |
--logDirname | Log storage directory | /tmp/logs |
--interceptOutput | Whether to intercept standard output | false |
--extendedContextKey | Extended context request header key | X-Cloudbase-Context |
💡 Tip: You need to install the
@cloudbase/functions-frameworkdependency inpackage.json.
Express Framework
#!/bin/bash
export PORT=9000
/var/lang/node18/bin/node app.js
📝 Note: Please change
app.jsto your actual startup filename.
Next.js Framework
#!/bin/bash
export PORT=9000
/var/lang/node18/bin/node node_modules/next/dist/bin/next start
Flask Framework
#!/bin/bash
export PORT=9000
/var/lang/python3/bin/python3 app.py
📝 Note: Please change
app.pyto your actual startup filename.
Django Framework
#!/bin/bash
export PORT=9000
/var/lang/python3/bin/python3 manage.py runserver 0.0.0.0:9000
FastAPI Framework
#!/bin/bash
export PORT=9000
/var/lang/python3/bin/python3 -m uvicorn main:app --host 0.0.0.0 --port 9000
📝 Note: Please change
maininmain:appto your application filename, andappto your application instance name.
Built-in Web Server
#!/bin/bash
/var/lang/php7/bin/php -c /var/runtime/php7 -S 0.0.0.0:9000 hello.php
📝 Note: Please change
hello.phpto your entry filename.
Laravel Framework
#!/bin/bash
/var/lang/php7/bin/php artisan serve --host=0.0.0.0 --port=9000
Spring Boot
#!/bin/bash
export PORT=9000
/var/lang/java8/bin/java -Dserver.port=${PORT} -jar target/my-app.jar
📝 Note: Please change
my-app.jarto your actual JAR filename.