Static website in Yandex Object Storage using Terraform
To host a static website in Object Storage using Terraform:
If you no longer need the resources you created, delete them.
Get your cloud ready
Sign up for Yandex Cloud and create a billing account:
- Navigate to the management console
and log in to Yandex Cloud or create a new account. - On the Yandex Cloud Billing
page, make sure you have a billing account linked and it has theACTIVEorTRIAL_ACTIVEstatus. If you do not have a billing account, create one and link a cloud to it.
If you have an active billing account, you can navigate to the cloud page
Learn more about clouds and folders here.
Required paid resources
The cost of hosting a static website includes:
- Fee for storing data for a static website (see Object Storage pricing).
- Fee for data operations (see Object Storage pricing).
- Fee for outgoing traffic from Yandex Cloud to the internet (see Object Storage pricing).
- Fee for public DNS queries and zones (see Yandex Cloud DNS pricing).
Create an infrastructure
With Terraform
Terraform is distributed under the Business Source License
For more information about the provider resources, see the relevant documentation on the Terraform
To create an infrastructure for a static website in Object Storage using Terraform:
-
Install Terraform, get the authentication credentials, and specify the source for installing the Yandex Cloud provider (see Configure your provider, Step 1).
-
Prepare your infrastructure description files:
Ready-made configurationManually-
Clone the repository with configuration files.
git clone https://github.com/yandex-cloud-examples/yc-s3-static-website.git -
Navigate to the repository directory. Make sure it contains the following files:
static.tf: New infrastructure configuration.index.htmlanderror.html: Website's main page and error page.
- Create a folder for configuration files.
- In the folder, create:
-
static.tfconfiguration file:static.tf
locals { folder_id = "<folder_ID>" domain = "<domain>" } terraform { required_providers { yandex = { source = "yandex-cloud/yandex" version = ">= 0.47.0" } } } provider "yandex" { 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 = local.domain max_size = 1073741824 acl = "public-read" website { index_document = "index.html" error_document = "error.html" } # Certificate Manager certificate https { certificate_id = data.yandex_cm_certificate.example.id } } resource "yandex_cm_certificate" "le-certificate" { name = "my-le-cert" domains = ["${local.domain}"] managed { challenge_type = "DNS_CNAME" } } resource "yandex_dns_recordset" "validation-record" { zone_id = yandex_dns_zone.zone1.id name = yandex_cm_certificate.le-certificate.challenges[0].dns_name type = yandex_cm_certificate.le-certificate.challenges[0].dns_type data = [yandex_cm_certificate.le-certificate.challenges[0].dns_value] ttl = 600 } data "yandex_cm_certificate" "example" { depends_on = [yandex_dns_recordset.validation-record] certificate_id = yandex_cm_certificate.le-certificate.id #wait_validation = true } 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 = "${local.domain}." public = true } resource "yandex_dns_recordset" "rs2" { zone_id = yandex_dns_zone.zone1.id name = "${local.domain}." type = "ANAME" ttl = 600 data = ["${local.domain}.website.yandexcloud.net"] } -
index.htmlfile containing the textHello world!:index.html
<!doctype html> <html> <head> <title>Hello, world!</title> </head> <body> <p>Hello, world!</p> </body> </html> -
error.htmlfile containing the textError!:error.html
<!doctype html> <html> <head> <title>Error!</title> </head> <body> <p>Error!</p> </body> </html>
-
Learn more about the properties of Terraform resources in the provider documentation:
-
-
Under
localsin thestatic.tffile, set the following user-defined properties:folder_id: ID of the folder to create the resources in.domain: Domain name inexample.comformat, without a trailing dot.
-
Create resources:
-
In the terminal, go to the directory where you edited the configuration file.
-
Make sure the configuration file is correct using this command:
terraform validateIf the configuration is correct, you will get this message:
Success! The configuration is valid. -
Run this command:
terraform planYou will see a detailed list of resources. No changes will be made at this step. If the configuration contains any errors, Terraform will show them.
-
Apply the changes:
terraform apply -
Type
yesand press Enter to confirm the changes.
-
After creating the infrastructure, test the website.
Test the website
To check that the website is running, use one of the standard Object Storage addresses:
http://<bucket_name>.website.yandexcloud.nethttp://website.yandexcloud.net/<bucket_name>
If you have configured your own domain for a website, use its address, e.g., example.com.
How to delete the resources you created
To stop paying for the resources you created:
-
Open the
static.tfconfiguration file and delete the description of the new infrastructure from it. -
Apply the changes:
-
In the terminal, go to the directory where you edited the configuration file.
-
Make sure the configuration file is correct using this command:
terraform validateIf the configuration is correct, you will get this message:
Success! The configuration is valid. -
Run this command:
terraform planYou will see a detailed list of resources. No changes will be made at this step. If the configuration contains any errors, Terraform will show them.
-
Apply the changes:
terraform apply -
Type
yesand press Enter to confirm the changes.
-