[Yandex Cloud documentation](../../../index.md) > [Yandex Cloud Functions](../../index.md) > [Step-by-step guides](../index.md) > Creating a function > Creating a function version

# Creating a function version

To create a function [version](../../concepts/function.md#version), you can use one of the [code upload formats](../../concepts/function.md#upload). This example uses a ZIP archive.

{% note warning %}

Use Object Storage to [upload](../../../storage/operations/objects/upload.md) files larger than 3.5 MB. For more information about limits, see [Quotas and limits in Cloud Functions](../../concepts/limits.md).

{% endnote %}

## Preparing a ZIP archive with the function code {#zip-archive}

1. Save the following code to a file named `index.js`:

    ```js
    exports.handler = async function (event, context) {
        let name = 'World';
        if (event.queryStringParameters && event.queryStringParameters.name) {
            name = event.queryStringParameters.name
        }
        return {
            'statusCode': 200,
            'headers': {
                'Content-Type': 'text/plain'
            },
            'isBase64Encoded': false,
            'body': `Hello, ${name}!`
        }
    };
    ```

1. Add `index.js` to the `hello-js.zip` archive.

    {% note info %}
    
    If you use the Finder context menu to create a ZIP archive on macOS, the system automatically adds the `__MACOSX` system directory to the archive. This directory may cause errors when you build functions. To delete this directory from your ZIP archive, use the command line to navigate to the folder containing the archive and run this command:
    
    ```bash
    zip -d <archive_name>.zip "__MACOSX/*"
    ```
    
    {% endnote %}

## Creating a function version {#version-create}

When creating a version, specify the following settings:

* _Runtime environment_: Provides additional libraries and environment variables accessible from the function code. It matches the programming language of your function. For more information, see [Runtime](../../concepts/runtime/index.md).
* _Entry point_: Function to invoke as the [handler](../../concepts/function.md#programming-model).
* _Timeout_: Maximum function execution time. After this time, the service stops the function without waiting for a response. It includes the initialization time during the first run.

{% list tabs group=instructions %}

- Management console {#console}

    1. In the [management console](https://console.yandex.cloud), navigate to the folder containing the function.
    1. Navigate to **Cloud Functions**.
    1. Select the function whose version you want to create.
    1. Under **Last version**, click **Сreate in editor**.
    1. Select the [runtime](../../concepts/runtime/index.md). Disable **Add files with code examples**.
    1. Click **Continue**.
    1. Prepare the function code:
       * **Runtime environment**: `nodejs18`.
       * **Code source**: `ZIP archive`.
       * **File**: `hello-js.zip`.
       * **Entry point**: `index.handler`.
    1. Configure the version:
       * **Timeout**: `5`.
       * **Memory**: `128 MB`.
       * [**Service account**](../../../iam/concepts/users/service-accounts.md): `Not selected`.
       * [**Environment variables**](../../concepts/runtime/environment-variables.md): `Not selected`.
    1. Click **Save changes**.

- CLI {#cli}

    If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../../cli/quickstart.md#install).

    The folder used by default is the one specified when [creating](../../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

    To create a function version, run this command:

    ```
    yc serverless function version create \
      --function-name=my-nodejs-function \
      --runtime nodejs18 \
      --entrypoint index.handler \
      --memory 128m \
      --execution-timeout 5s \
      --source-path ./hello-js.zip
    ```

    Where:

    * `--function-name`: Name of the function whose version you want to create.
    * `--runtime`: Runtime.
    * `entrypoint`: Entry point in `<file_name_without_extension>.<handler_name>` format.
    * `--memory`: Amount of RAM.
    * `--execution-timeout`: Maximum function execution time before timeout.
    * `--source-path`: ZIP archive with the function code and required dependencies.

    Result:

    ```
    done (1s)
    id: d4evvn8obisa********
    function_id: d4elpv8pft63********
    created_at: "2020-08-01T19:09:19.531Z"
    runtime: nodejs18
    entrypoint: index.handler
    resources:
      memory: "134217728"
    execution_timeout: 5s
    image_size: "4096"
    status: ACTIVE
    tags:
      - $latest
    log_options:
      folder_id: b1g681qpemb4********
    ```

- Terraform {#tf}

    With [Terraform](https://www.terraform.io/), you can quickly create a cloud infrastructure in Yandex Cloud and manage it using configuration files. These files store the infrastructure description written in HashiCorp Configuration Language (HCL). If you change the configuration files, Terraform automatically detects which part of your configuration is already deployed, and what should be added or removed.
    
    Terraform is distributed under the [Business Source License](https://github.com/hashicorp/terraform/blob/main/LICENSE). The [Yandex Cloud provider for Terraform](https://github.com/yandex-cloud/terraform-provider-yandex) is distributed under the [MPL-2.0](https://www.mozilla.org/en-US/MPL/2.0/) license.
    
    For more information about the provider resources, see the guides on the [Terraform](https://www.terraform.io/docs/providers/yandex/index.html) website or [its mirror](../../../terraform/index.md).

    If you do not have Terraform yet, [install it and configure the Yandex Cloud provider](../../../tutorials/infrastructure-management/terraform-quickstart.md#install-terraform).
    
    
    To manage infrastructure using Terraform under a service account or user accounts (a Yandex account, a federated account, or a local user), [authenticate](../../../terraform/authentication.md) using the appropriate method.

    To create a new function version:

    1. Open the Terraform configuration file and change the function settings:
      
       * `yandex_function`: Description of the new function and its source code.
         * `name`: Function name.
         * `description`: Text description of the function.
         * `user_hash`: Any string to identify the function version. When you change the function, update this string as well. Updating this string triggers a function update.
         * `runtime`: Function [runtime](../../concepts/runtime/index.md).
         * `entrypoint`: Function name in the source code that will serve as an entry point to applications.
         * `memory`: Amount of memory allocated for the function, in MB.
         * `execution_timeout`: Function execution timeout.
         * `service_account_id`: ID of the service account you want to use to invoke the function.
         * `content`: Function source code.
           * `content.0.zip_filename`: Name of the ZIP archive containing the function source code.

        Here is an example of the configuration file structure:

        ```
        resource "yandex_function" "test-function" {
            name               = "test-function"
            description        = "Test function"
            user_hash          = "first-function"
            runtime            = "nodejs18"
            entrypoint         = "main"
            memory             = "128"
            execution_timeout  = "10"
            service_account_id = "<service_account_ID>"
            content {
                zip_filename = "<path_to_ZIP_archive>"
            }
        }
        ```

       {% note info %}
    
       If you change the function name or description, the system will not create a new version.

       {% endnote %}

        For more on the properties of the `yandex_function` resource, see [this provider guide](../../../terraform/resources/function.md).

    1. Validate your configuration using this command:
        
       ```
       terraform validate
       ```

       If the configuration is valid, you will get this message:
        
       ```
       Success! The configuration is valid.
       ```

    1. Run this command:

       ```
       terraform plan
       ```
        
       You will see a list of resources and their properties. No changes will be made at this step. Terraform will show any errors in the configuration. 
         
    1. Apply the configuration changes:

       ```
       terraform apply
       ```
    1. Type `yes` and press **Enter** to confirm the changes.
      
    You can check the new version using the [management console](https://console.yandex.cloud) or this [CLI](../../../cli/quickstart.md) command:

    ```
    yc serverless function version list --function-name <function_name>
    ```

- API {#api}

    To create a function version, use the [createVersion](../../functions/api-ref/Function/createVersion.md) REST API method for the [Function](../../functions/api-ref/Function/index.md) resource or the [FunctionService/CreateVersion](../../functions/api-ref/grpc/Function/createVersion.md) gRPC API call.

    **Request examples**

    To use the examples below, install [cURL](https://curl.haxx.se) and [authenticate](../../api-ref/functions/authentication.md) with the API.

    {% cut "Example where code is fetched from an Object Storage bucket" %}

    1. [Upload](../../../storage/operations/objects/upload.md) the `hello-js.zip` archive with the function version code to your Object Storage bucket.
    1. Create a file named `body.json` with the following request body:

        ```json
        {
          "functionId": "<function_ID>",
          "runtime": "nodejs22",
          "entrypoint": "index.handler",
          "resources": {
            "memory": "134217728"
          },
          "executionTimeout": "5s",
          "serviceAccountId": "<service_account_ID>",
          "package": {
            "bucketName": "<bucket_name>",
            "objectName": "hello-js.zip"
          },
        }
        ```

        Where:

        * `functionId`: ID of the function whose version you want to create.
        * `runtime`: [Runtime](../../concepts/runtime/index.md#runtimes).
        * `entrypoint`: Entry point in `<file_name_without_extension>.<handler_name>` format.
        * `memory`: Amount of RAM.
        * `executionTimeout`: Maximum function execution time before timeout.
        * `serviceAccountId`: ID of the service account with a [role](../../../storage/security/index.md#service-roles) that allows bucket data reads.
        * `bucketName`: Name of the bucket where you uploaded the ZIP archive with the function code and required dependencies.
        * `objectName`: [Key of the bucket object](../../../storage/concepts/object.md#key) that contains the function code.

    {% endcut %}

    {% cut "Example where code is provided in the request body" %}

    1. Encode the ZIP archive with the function code into Base64 format:

        ```bash
        base64 -i hello-js.zip > output.txt
        ```

    1. Create a file named `body.json` with the following request body:

        ```json
        {
          "functionId": "<function_ID>",
          "runtime": "nodejs22",
          "entrypoint": "index.handler",
          "resources": {
            "memory": "134217728"
          },
          "executionTimeout": "5s",
          "content": "<ZIP_archive_contents_in_Base64_encoding>"
        }
        ```

        Where:

        * `functionId`: ID of the function whose version you want to create.
        * `runtime`: [Runtime](../../concepts/runtime/index.md#runtimes).
        * `entrypoint`: Entry point in `<file_name_without_extension>.<handler_name>` format.
        * `memory`: Amount of RAM.
        * `executionTimeout`: Maximum function execution time before timeout.
        * `content`: Function version code in Base64 encoding, `output.txt` file contents.

    {% endcut %}

    {% cut "Example where code is fetched from another function version in Cloud Functions" %}

    Create a file named `body.json` with the following request body:

    ```json
    {
      "functionId": "<function_ID>",
      "runtime": "nodejs22",
      "entrypoint": "index.handler",
      "resources": {
        "memory": "134217728"
      },
      "executionTimeout": "5s",
      "versionId": "<previous_function_version_ID>"
    }
    ```

    Where:

    * `functionId`: ID of the function whose version you want to create.
    * `runtime`: [Runtime](../../concepts/runtime/index.md#runtimes).
    * `entrypoint`: Entry point in `<file_name_without_extension>.<handler_name>` format.
    * `memory`: Amount of RAM.
    * `executionTimeout`: Maximum function execution time before timeout.
    * `versionId`: ID of a [previous function version](version-list.md).

    {% endcut %}

    Send the request by specifying the path to the previously created file with the request body:

    ```bash
    export IAM_TOKEN=$(yc iam create-token)
    curl -X POST \
      -H "Authorization: Bearer ${IAM_TOKEN}" \
      -d "@<body.json_file_path>" \
      https://serverless-functions.api.cloud.yandex.net/functions/v1/versions
    ```
    
    Result:
    
    ```json
    {
      "done": false,
      "metadata": {
        "@type": "type.googleapis.com/yandex.cloud.serverless.functions.v1.CreateFunctionVersionMetadata",
        "functionVersionId": "d4e25m0gila4********"
      },
      "id": "d4edk0oobcc9********",
      "description": "Create function version",
      "createdAt": "2023-10-11T11:22:21.286786431Z",
      "createdBy": "ajeol2afu1js********",
      "modifiedAt": "2023-10-11T11:22:21.286786431Z"
    }
    ```


{% endlist %}

{% note info %}

To ensure integrity of references, you cannot update function versions. For more information about the resource relationships, see [Function](../../concepts/function.md).

{% endnote %}