Static website in Yandex Object Storage
In this guide, you will learn how to upload your website static files to Yandex Object Storage and link a domain name to a bucket.
To host a static website in Object Storage:
- Prepare your cloud.
- Create a public bucket.
- Enable a website for a bucket.
- Configure the DNS.
- Upload the website files.
- Check that the website is running.
If you no longer need the website, delete the files from the storage, as well as the availability zone.
You can also deploy the infrastructure for hosting a static website in Object Storage via Terraform using a ready-made configuration file.
Prepare your cloud
Sign up for Yandex Cloud and create a billing account:
- Go to the management console
and log in to Yandex Cloud or create an account if you do not have one yet. - On the Billing
page, make sure you have a billing account linked and it has theACTIVE
orTRIAL_ACTIVE
status. If you do not have a billing account, create one.
If you have an active billing account, you can go to the cloud page
Learn more about clouds and folders.
Required paid resources
The cost of hosting a static website includes:
- Fee for storing data for a static website (see Yandex Object Storage pricing).
- Fee for data operations (see Yandex Object Storage pricing).
- Cost of outgoing traffic from Yandex Cloud to the internet (see Yandex Object Storage pricing).
- A fee for public DNS queries and zones (see Yandex Cloud DNS pricing).
Create a public bucket
To create a bucket for static website files:
-
Go to the Yandex Cloud management console
and select the folder where you will perform the operations. -
On the folder page, click Create resource and select Bucket.
-
In the Name field, enter a name for the bucket (for instance,
www.example.com
). The bucket name will become a part of the website domain name. After configuration is complete, the bucket becomes available at two addresses:http(s)://<bucket_name>.website.yandexcloud.net
http(s)://website.yandexcloud.net/<bucket name>
The name must meet the following requirements:
- The name must be from 3 to 63 characters long.
- The name may contain lowercase Latin letters, numbers, hyphens, and periods.
- The first and last characters must be letters or numbers.
- The characters to the right and left of the period must be letters or numbers.
- The name must not look like an IP address, e.g.,
10.1.3.9
.
Note
If you plan to use your own domain for the website, the bucket name must exactly match the name of your domain, and this must be a third-level or higher domain. For more information, see Your own domain.
-
Specify the maximum size of the bucket in GB.
-
Enable Public read access to the objects.
-
Click Create bucket.
Enable a website for a bucket
You should upload and configure the index page and error page. To do this:
- Create the following files on your computer:
index.html
with the textHello world!
.
The file content gets displayed when accessing the website's home page.error.html
with the textError!
.
The file content appears in website responses with4xx
errors.
- Go to your bucket's page, open the Objects tab, and click Upload. In the window that opens, select the created files and click the confirmation button.
- Click Upload.
- Open the Website tab on your bucket page.
- Select Hosting.
- In the Home page field, specify
index.html
. - In the Error page field, specify
error.html
. - Click Save.
- Make sure the website's home page opens. To do this, access the website from a browser via a link like
http://<bucket_name>.website.yandexcloud.net
. - Make sure the error page opens. To do this, access the website from a browser via a link like
http://<bucket_name>.website.yandexcloud.net/error-check
.
Note
By default, the website is accessible only over HTTP, for instance, http://www.example.com
or http://www.example.com.website.yandexcloud.net
. To provide HTTPS support for your website, upload your own security certificate to Object Storage.
Configure the DNS
If you have a registered domain name, assign it to the bucket. To do this, create a CNAME record in the DNS.
You can use the Cloud DNS service to manage the domain.
Note
The bucket name must exactly match the name of your domain, and this must be a third-level or higher domain. For more information, see Your own domain.
The tutorial below describes configuring DNS for the third-level www.example.com
domain name.
Add a zone
To add a public zone:
- Open the Cloud DNS section of the folder where you need to create a DNS zone.
- Click Create zone.
- Specify the zone settings:
- Zone name:
example-zone-1
. - Zone:
example.com
. Specify your registered domain. - Type: Public.
- Zone name:
- Click Create.
Create a CNAME record
Create a CNAME DNS record in the public zone.
- Select
example.com
from the list. - Click Create record.
- Set the record parameters:
- Name:
www
. - Record type: Select
CNAME
as the value. - TTL (record time to live): Leave the default value.
- Value: Enter
www.example.com.website.yandexcloud.net.
.
- Name:
- Click Create.
Delegate the domain name
Delegation is the transfer of authority from the registrar's servers to yours. For a domain, NS resource records are created (ns1.yandexcloud.net
and ns2.yandexcloud.net
).
To delegate a domain, specify its DNS servers in the registrar's account.
Delegation does not take effect immediately. It normally takes internet service providers up to 24 hours (86400 seconds) to update records. This depends on the TTL value that determines how long domain records are cached.
You can verify domain delegation using the Whoisdig
utility:
dig +short NS example.com
Result:
ns2.yandexcloud.net.
ns1.yandexcloud.net.
Upload the website files
After you configure and test availability, upload the remaining files that are necessary for website operation. To do this, use the management console
Check that the website is running
To check that the website is running, use one of the standard Object Storage addresses:
http://www.example.com.website.yandexcloud.net
http://website.yandexcloud.net/www.example.com
If you configured your own domain, use http://www.example.com
.
How to delete the resources you created
To stop paying for the resources:
How to create an infrastructure using Terraform
With Terraform
For more information about the provider resources, see the documentation on the Terraform
If you change the configuration files, Terraform automatically determines which part of your configuration is already deployed and what should be added or removed.
To host a static website in Object Storage using Terraform:
-
Install Terraform and specify the source for installing the Yandex Cloud provider (see Configure a provider, step 1).
-
Prepare files with the infrastructure description:
Ready-made archiveCreating files manually- Create a directory for files:
- Download the archive
(2 KB). - Unpack the archive to the directory. Now, the directory should include the
static.tf
configuration file, as well as the filesindex.html
anderror.html
.
-
Create a directory for files:
-
Create the
static.tf
configuration file in the directory:static.tflocals { token = "<OAuth_or_IAM_token>" cloud_id = "<cloud_ID>" folder_id = "<folder_ID>" } terraform { required_providers { yandex = { source = "yandex-cloud/yandex" version = ">= 0.47.0" } } } provider "yandex" { token = local.token cloud_id = local.cloud_id folder_id = local.folder_id } resource "yandex_iam_service_account" "sa" { name = "my-sa" } resource "yandex_resourcemanager_folder_iam_member" "sa-editor" { folder_id = local.folder_id role = "storage.editor" member = "serviceAccount:${yandex_iam_service_account.sa.id}" } resource "yandex_iam_service_account_static_access_key" "sa-static-key" { service_account_id = yandex_iam_service_account.sa.id description = "static access key for object storage" } resource "yandex_storage_bucket" "test" { access_key = yandex_iam_service_account_static_access_key.sa-static-key.access_key secret_key = yandex_iam_service_account_static_access_key.sa-static-key.secret_key bucket = "www.example.com" acl = "public-read" website { index_document = "index.html" error_document = "error.html" } } resource "yandex_storage_object" "index-html" { access_key = yandex_iam_service_account_static_access_key.sa-static-key.access_key secret_key = yandex_iam_service_account_static_access_key.sa-static-key.secret_key bucket = yandex_storage_bucket.test.id key = "index.html" source = "index.html" } resource "yandex_storage_object" "error-html" { access_key = yandex_iam_service_account_static_access_key.sa-static-key.access_key secret_key = yandex_iam_service_account_static_access_key.sa-static-key.secret_key bucket = yandex_storage_bucket.test.id key = "error.html" source = "error.html" } resource "yandex_dns_zone" "zone1" { name = "example-zone-1" description = "Public zone" zone = "example.com." public = true } resource "yandex_dns_recordset" "rs1" { zone_id = yandex_dns_zone.zone1.id name = "www" type = "CNAME" ttl = 200 data = ["www.example.com.website.yandexcloud.net."] }
-
In the directory, create an
index.html
file with the textHello world!
:index.html<!doctype html> <html> <head> <title>Hello, world!</title> </head> <body> <p>Hello, world!</p> </body> </html>
-
In the directory, create an
error.html
file with the textError!
:error.html<!doctype html> <html> <head> <title>Hello, world!</title> </head> <body> <p>Hello, world!</p> </body> </html>
For more information about the parameters of resources used in Terraform, see the provider documentation:
-
In the
static.tf
file, paste the following values in thelocals
section:token
: An OAuth token (if you're using a Yandex account) or IAM token (if you're using a Yandex account or federated account) to access Yandex Cloud. The IAM token is valid for up to 12 hours but can't exceed the federation's cookie lifetime.cloud_id
: ID of the cloud to create resources in.folder_id
: ID of the folder to create resources in.
-
Create resources:
-
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.
-