Mounting a bucket to a function
- In the management console
, select the folder containing the function. - Select Cloud Functions.
- Select a function.
- Go to the Editor tab.
- In block Parameters, select or create a new service account with one of these roles:
storage.viewer
to only read data from the mounted bucket.storage.uploader
to read and write data from/to the mounted bucket.
- Under Mounted buckets, click Add bucket and specify:
- Mount point: Name of the mount point. Use this path to access the directory in which the bucket is mounted:
/function/storage/<mount_point>
. - Bucket: Name of the bucket you want to mount. Alternatively, you can create a new bucket.
- Folder: Folder within the bucket. Leave this field empty to mount the entire bucket.
- Read-only: Disables writing to the bucket. If this option is enabled, data from the mounted bucket is available as read-only.
- Mount point: Name of the mount point. Use this path to access the directory in which the bucket is mounted:
- Click Save changes.
If you do not have the Yandex Cloud command line interface yet, install and initialize it.
The folder specified in the CLI profile is used by default. You can specify a different folder using the --folder-name
or --folder-id
parameter.
To mount a bucket to a function, run this command:
yc serverless function version create \
--function-name=<function_name> \
--runtime <runtime_environment> \
--entrypoint <entry_point> \
--memory <RAM_amount> \
--execution-timeout <execution_timeout> \
--source-path <path_to_ZIP_archive> \
--service-account-id <service_account_ID>
--storage-mounts mount-point=<mount_point>,bucket=<bucket_name>,prefix=<folder_name>,read-only=false
Where:
--function-name
: Function name.--runtime
: Runtime environment.--entrypoint
: Entry point in the following format:<file_name_without_extension>.<listener_name>
.--memory
: Amount of RAM.--execution-timeout
: Maximum function running time before the timeout is reached.--source-path
: ZIP archive with the function code and required dependencies.--service-account-id
: Service account ID. The account must have thestorage.viewer
role to read data from the bucket or thestorage.uploader
role to write data.--storage-mounts
: Object Storage bucket mounting parameters:mount-point
: Name of the mount point. Use this path to access the directory in which the bucket is mounted:/function/storage/<mount_point>
.bucket
: Bucket.prefix
: Folder within the bucket. Leave this field empty to mount the entire bucket.read-only
: Disables writing to the bucket. If this option is enabled, data from the mounted bucket is available as read-only. The possible values aretrue
andfalse
.
Terraform
For more information about the provider resources, see the documentation on the Terraform
If you change the configuration files, Terraform automatically detects which part of your configuration is already deployed, and what should be added or removed.
If you don't have Terraform, install it and configure the Yandex Cloud provider.
To mount a bucket to a function:
-
Open the Terraform configuration file and add the
storage_mounts
section to the function description:resource "yandex_function" "bucketfunction" { name = "<bucket_name>" user_hash = "<function_hash>" runtime = "<runtime_environment>" entrypoint = "<entry_point>" memory = "<RAM_amount>" execution_timeout = "<execution_timeout>" service_account_id = "<service_account_ID>" content { zip_filename = "<path_to_ZIP_archive>" } storage_mounts { mount_point_name = "<mount_point>" bucket = "<bucket_name>" prefix = "<folder_name>" read_only = <read_only_flag> } }
Where:
storage_mounts
: Bucket mounting parameters:mount_point_name
: Name of the mount point. Use this path to access the directory in which the bucket is mounted:/function/storage/<mount_point>
.bucket
: Bucket.prefix
: Folder within the bucket. Leave this field empty to mount the entire bucket.read_only
: Disables writing to the bucket. If this option is enabled, data from the mounted bucket is available as read-only. The possible values aretrue
andfalse
.
For more information about the
yandex_function
resource parameters, see the provider documentation . -
Apply the changes:
-
In the terminal, change to the folder where you edited the configuration file.
-
Make sure the configuration file is correct using the command:
terraform validate
If the configuration is correct, the following message is returned:
Success! The configuration is valid.
-
Run the command:
terraform plan
The terminal will display a list of resources with parameters. No changes are made at this step. If the configuration contains errors, Terraform will point them out.
-
Apply the configuration changes:
terraform apply
-
Confirm the changes: type
yes
in the terminal and press Enter.
-
You can check the function update and its new configuration using the management console
yc serverless function version get <function_version_ID>
To mount a bucket to a function, use the createVersion REST API method for the Function resource or the FunctionService/CreateVersion gRPC API call.