[Yandex Cloud documentation](../../index.md) > [Yandex Cloud Functions](../index.md) > [Tutorials](index.md) > Serverless-based backend > Web application on SourceCraft Sites and Cloud Functions

# Web application on SourceCraft Sites and Yandex Cloud Functions

In this tutorial, you will deploy a web application from a [repository](https://sourcecraft.dev/examples/sourcecraft-sites-web-app) using SourceCraft [CI/CD](https://sourcecraft.dev/portal/docs/en/sourcecraft/concepts/ci-cd). The app features the following components:

* Frontend: Static website on [SourceCraft Sites](https://sourcecraft.dev/portal/docs/en/sourcecraft/concepts/sites) with a field for enter the name and a **Hello** button.
* Backend: Yandex Cloud Functions [function](../concepts/function.md) in Python that returns `Hello, <name>!`.
* Yandex API Gateway [gateway](../../api-gateway/concepts/index.md) that accepts requests from the frontend and [calls the function](../../api-gateway/concepts/extensions/cloud-functions.md).

The application code and CI/CD configuration are stored in a SourceCraft [repository](https://sourcecraft.dev/portal/docs/en/sourcecraft/concepts/#repos). Yandex Cloud resources are created through a [service connection](https://sourcecraft.dev/portal/docs/en/sourcecraft/concepts/service-connections).

This use case is suitable for prototypes and smaller projects: you do not have to manager servers, and SourceCraft Sites provides free hosting with HTTPS access without uploading a custom TLS certificate.

To create a web app:

1. [Get ready to work](#prepare).
1. [Create a service account](#create-sa).
1. [Create a repository fork](#create-repository).
1. [Create a service connection](#create-service-connection).
1. [Prepare the configuration and application code](#configure).
1. [Deploy the application](#deploy).
1. [Test the application](#test).

If you no longer need the resources you have created, [delete them](#clear-out) and [unpublish the website](#delete).

## Get ready to work {#prepare}

1. Log in to the Yandex Cloud [management console](https://console.yandex.cloud) or sign up.
1. Make sure you have a [billing account](../../billing/concepts/billing-account.md) with the `ACTIVE` or `TRIAL_ACTIVE` status. If you do not have a billing account, [create one](../../billing/quickstart/index.md) and [link a cloud](../../billing/operations/pin-cloud.md) to it.
1. Select or [create a folder](../../resource-manager/operations/folder/create.md) to place your function and API gateway in.
1. Authenticate in SourceCraft on the service [home page](https://sourcecraft.dev) or [sign up](https://sourcecraft.dev/portal/docs/en/sourcecraft/security/registration).
1. Select or [create a public organization](https://sourcecraft.dev/portal/docs/en/sourcecraft/operations/org-create) in SourceCraft.

    To create a service connection at the organization level, you need the [Organization admin](https://sourcecraft.dev/portal/docs/en/sourcecraft/security/#organization-manager-admin) (`organization-manager.admin`) or [Organization owner](https://sourcecraft.dev/portal/docs/en/sourcecraft/security/#organization-manager-organizations-owner) (`organization-manager.organizations.owner`) role.

{% note warning %}

You can only publish a website from a public repository in a public SourceCraft organization. The repository files will be available to all internet users. Do not save tokens, access keys, and other secrets in them. A service connection allows accessing Yandex Cloud without storing long-lived keys in the code.

{% endnote %}

### Required paid resources {#paid-resources}

The infrastructure support cost includes:

* Fee for function calls, computing resources, and outgoing traffic (see [Yandex Cloud Functions pricing](../pricing.md)).
* Fee for gateway requests and outgoing traffic (see [Yandex API Gateway pricing](../../api-gateway/pricing.md)).

{% note info %}

For Cloud Functions and API Gateway, a [non-chargeable amount of services](../../billing/concepts/serverless-free-tier.md) is available. Exceeding this limit is charged according to the relevant pricing policy.

Website hosting on SourceCraft Sites is free. You can find the CI/CD terms of use in the [SourceCraft pricing policy](https://sourcecraft.dev/portal/docs/en/sourcecraft/pricing#tariff-differences).

{% endnote %}

## Create a service account {#create-sa}

Under a [service account](../../iam/concepts/users/service-accounts.md), CI/CD workflows will create and delete resources, while the API gateway will call a function.

1. In the Yandex Cloud [management console](https://console.yandex.cloud), select your prepared folder.
1. [Navigate](https://console.yandex.cloud/link/iam) to **Identity and Access Management**.
1. Click **Create service account**.
1. In the **Name** field, specify `sites-web-app-sa`.
1. Click ![image](../../_assets/console-icons/plus.svg) **Add role** and select roles for the folder:

    * [functions.editor](../security/index.md#functions-editor): For creating, updating, calling, and deleting a function.
    * [api-gateway.editor](../../api-gateway/security/index.md#api-gateway-editor): For creating and deleting an API gateway.
    * [iam.serviceAccounts.user](../../iam/security/index.md#iam-serviceAccounts-user): For specifying a service account in the function version and specifying an API gateway.

1. Click **Create**.

## Create a repository fork {#create-repository}

A [fork](https://sourcecraft.dev/portal/docs/en/sourcecraft/concepts/#forks) enables copying the example into your organization along with the CI/CD and SourceCraft Sites settings.

1. Open the [example repository](https://sourcecraft.dev/examples/sourcecraft-sites-web-app).
1. In the top-right corner, click ![image](../../_assets/console-icons/code-fork.svg) **Fork**.
1. Select your prepared public organization and specify the repository name, e.g., `sourcecraft-sites-web-app`.
1. Select the `master` branch for the fork and click **Create a fork**.

The repository stores:

* `.sourcecraft/ci.yaml`: CI/CD configuration with the `create-resources` and `delete-resources` workflows.
* `.sourcecraft/sites.yaml`: Website publication configuration from the `sites` directory in the `release` branch.
* `sites/`: Frontend HTML and JavaScript. The `sites/config.js` file contains the address of the API gateway that the workflow provides when deploying.
* `src/index.py`: Python function code.
* `yc-scripts/`: Scripts for getting an IAM token, creating and deleting Yandex Cloud resources.

{% note info %}

Cloning the example to a local computer does not create a repository in your organization. To follow this tutorial, use the fork you created in SourceCraft.

{% endnote %}

## Create a service connection {#create-service-connection}

Create a connection at the organization level. The `default-service-connection` name must match the one specified in `.sourcecraft/ci.yaml`.

1. In SourceCraft, open the organization where you created the repository fork.
1. Under ![image](../../_assets/console-icons/gear.svg) **Settings**, navigate to ![image](../../_assets/console-icons/cloud-nut-hex.svg) **Service connections**.
1. In the top-right corner, click ![image](../../_assets/console-icons/plus.svg) **New service connection**.
1. Under **Basic information**, specify the `default-service-connection` name.
1. Under **Scope**, allow using it for all repositories.
1. Under **Yandex Cloud settings**, select your prepared folder and the `sites-web-app-sa` service account.

    {% note tip %}
    
    To re-request the list of clouds, folders, and service accounts from Yandex Cloud, click ![image](../../_assets/console-icons/arrow-rotate-right.svg) **Synchronize**. This can be of use if alongside creating a service connection you also created a folder or service account.
    
    {% endnote %}

1. Click **Create service connection** and wait for the operation to complete.

For details, see [this tutorial on configuring a service connection](https://sourcecraft.dev/portal/docs/en/sourcecraft/operations/service-connections).

## Prepare the configuration and application code {#configure}

To edit a file in the SourceCraft UI, open it and click ![image](../../_assets/console-icons/pencil.svg) **Edit**. After editing each file, click **Commit changes** and commit to the `master` branch.

1. Open the `.sourcecraft/ci.yaml` file.
1. Make sure the `tokens:SERVICE_CONNECTION_WITH_TOKEN:service_connection` section shows the name of the service connection you created earlier.
1. In the `env` section, check the names of the function (`YC_FUNCTION_NAME`) and API gateway (`YC_API_GATEWAY_NAME`).

    {% note warning %}

    Make sure the selected cloud folder has no function and API gateway with these names. If it does, set other names: the workflow updates the existing function and uses the names from this configuration when deleting resources.

    {% endnote %}

## Deploy the application {#deploy}

1. In your repository, under ![image](../../_assets/console-icons/terminal-line.svg) **CI/CD**, navigate to the ![image](../../_assets/console-icons/arrows-3-rotate-right.svg) **CI/CD** section.
1. Click ![image](../../_assets/console-icons/plus.svg) **New launch**.
1. Select the `master` branch for the configuration and launch, and the `create-resources` workflow; then click **Launch now**.
1. Wait until the workflow completes with the **Success** status. If the workflow completed with an error, open it and check the logs of the cube that was not executed.

The workflow gets an IAM token through the service connection, sets a function and its version, and then creates an API gateway with the `GET /hello/{name}` route. After that, it writes the gateway address to the `sites/config.js` file and publishes the files in the `release` branch.

The `.sourcecraft/sites.yaml` file in the main branch is already configured to publish from the `release` branch:

```yaml
site:
  root: sites
  ref: release
```

After this branch appears, the website is published automatically in a few minutes.

## Test the application {#test}

1. Open `https://<organization_slug>.sourcecraft.site/<repositry_name>/` in your browser.
1. In the **Name** field, enter the name, e.g., `Alice`, and click **Hello**.
1. Make sure `Hello, Alice!` appeared under the button.

## Delete the Yandex Cloud resources {#clear-out}

1. In your repository, [manually run](https://sourcecraft.dev/portal/docs/en/sourcecraft/operations/run-workflow-manually) the `delete-resources` workflow from the `master` branch.
1. Wait for its successful completion. The workflow will delete the API gateway and function under the names from `.sourcecraft/ci.yaml`.

You can also delete the [API gateway](../../api-gateway/operations/api-gw-delete.md) and [function](../operations/function/function-delete.md) in the Yandex Cloud console.

If the `sites-web-app-sa` service account is no longer in use, [delete it](../../iam/operations/sa/delete.md) after deleting the function and API gateway.

## Unpublish the website {#delete}

Deleting the function and API gateway does not unpublish the static website. After deleting the cloud resources, delete the `.sourcecraft/sites.yaml` file from the `master` main branch.

#### Useful links {#see-also}

* [Hosting a static website using SourceCraft Sites](https://sourcecraft.dev/portal/docs/en/sourcecraft/tutorials/sites)
* [Configuring CI/CD with Cloud Functions](../../tutorials/serverless/ci-cd-sourcecraft-functions.md)
* [Connecting to a Managed Service for YDB database from a Python function in Cloud Functions](../../tutorials/serverless/connect-from-cf.md)