Web application on SourceCraft Sites and Yandex Cloud Functions
In this tutorial, you will deploy a web application from a repository
- Frontend: Static website on SourceCraft Sites
with a field for enter the name and a Hello button. - Backend: Yandex Cloud Functions function in Python that returns
Hello, <name>!. - Yandex API Gateway gateway that accepts requests from the frontend and calls the function.
The application code and CI/CD configuration are stored in a SourceCraft repository
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:
- Get ready to work.
- Create a service account.
- Create a repository fork.
- Create a service connection.
- Prepare the configuration and application code.
- Deploy the application.
- Test the application.
If you no longer need the resources you have created, delete them and unpublish the website.
Get ready to work
-
Log in to the Yandex Cloud management console
or sign up. -
Make sure you have a billing account with the
ACTIVEorTRIAL_ACTIVEstatus. If you do not have a billing account, create one and link a cloud to it. -
Select or create a folder to place your function and API gateway in.
-
Authenticate in SourceCraft on the service home page
or sign up . -
Select or create a public organization
in SourceCraft.To create a service connection at the organization level, you need the Organization admin
(organization-manager.admin) or Organization owner (organization-manager.organizations.owner) role.
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.
Required paid resources
The infrastructure support cost includes:
- Fee for function calls, computing resources, and outgoing traffic (see Yandex Cloud Functions pricing).
- Fee for gateway requests and outgoing traffic (see Yandex API Gateway pricing).
Note
For Cloud Functions and API Gateway, a non-chargeable amount of services 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
Create a service account
Under a service account, CI/CD workflows will create and delete resources, while the API gateway will call a function.
-
In the Yandex Cloud management console
, select your prepared folder. -
Navigate
to Identity and Access Management. -
Click Create service account.
-
In the Name field, specify
sites-web-app-sa. -
Click
Add role and select roles for the folder:- functions.editor: For creating, updating, calling, and deleting a function.
- api-gateway.editor: For creating and deleting an API gateway.
- iam.serviceAccounts.user: For specifying a service account in the function version and specifying an API gateway.
-
Click Create.
Create a repository fork
A fork
- Open the example repository
. - In the top-right corner, click
Fork. - Select your prepared public organization and specify the repository name, e.g.,
sourcecraft-sites-web-app. - Select the
masterbranch for the fork and click Create a fork.
The repository stores:
.sourcecraft/ci.yaml: CI/CD configuration with thecreate-resourcesanddelete-resourcesworkflows..sourcecraft/sites.yaml: Website publication configuration from thesitesdirectory in thereleasebranch.sites/: Frontend HTML and JavaScript. Thesites/config.jsfile 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
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.
Create a service connection
Create a connection at the organization level. The default-service-connection name must match the one specified in .sourcecraft/ci.yaml.
-
In SourceCraft, open the organization where you created the repository fork.
-
Under
Settings, navigate to Service connections. -
In the top-right corner, click
New service connection. -
Under Basic information, specify the
default-service-connectionname. -
Under Scope, allow using it for all repositories.
-
Under Yandex Cloud settings, select your prepared folder and the
sites-web-app-saservice account.Tip
To re-request the list of clouds, folders, and service accounts from Yandex Cloud, click
Synchronize. This can be of use if alongside creating a service connection you also created a folder or service account. -
Click Create service connection and wait for the operation to complete.
For details, see this tutorial on configuring a service connection
Prepare the configuration and application code
To edit a file in the SourceCraft UI, open it and click master branch.
-
Open the
.sourcecraft/ci.yamlfile. -
Make sure the
tokens:SERVICE_CONNECTION_WITH_TOKEN:service_connectionsection shows the name of the service connection you created earlier. -
In the
envsection, check the names of the function (YC_FUNCTION_NAME) and API gateway (YC_API_GATEWAY_NAME).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.
Deploy the application
- In your repository, under
CI/CD, navigate to the CI/CD section. - Click
New launch. - Select the
masterbranch for the configuration and launch, and thecreate-resourcesworkflow; then click Launch now. - 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:
site:
root: sites
ref: release
After this branch appears, the website is published automatically in a few minutes.
Test the application
- Open
https://<organization_slug>.sourcecraft.site/<repositry_name>/in your browser. - In the Name field, enter the name, e.g.,
Alice, and click Hello. - Make sure
Hello, Alice!appeared under the button.
Delete the Yandex Cloud resources
- In your repository, manually run
thedelete-resourcesworkflow from themasterbranch. - 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 and function in the Yandex Cloud console.
If the sites-web-app-sa service account is no longer in use, delete it after deleting the function and API gateway.
Unpublish the website
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.