Create and set up a website with Application Load Balancer-assisted load balancing among three availability zones and fault tolerance in one zone.
With Terraform, you can quickly create a cloud infrastructure in Yandex Cloud and manage it with configuration files. These files store the infrastructure description written in HashiCorp Configuration Language (HCL). Terraform and its providers are distributed under the Business Source License.
For more information about the provider resources, see the documentation on the Terraform website or its mirror.
If you change the configuration files, Terraform automatically detects which part of your configuration is already deployed, and what should be added or removed.
To host a fault-tolerant load-balanced website in an instance group using Terraform:
-
Install Terraform, get the authentication credentials, and specify the source for installing the Yandex Cloud provider (see Step 1: Configuring a provider).
-
Prepare files with the infrastructure description:
-
Clone the repository with configuration files:
git clone https://github.com/yandex-cloud-examples/yc-terraform-alb-website.git
-
Go to the directory with the repository. Make sure it contains the following files:
application-load-balancer-website.tf
: New infrastructure configuration.application-load-balancer-website.auto.tfvars
: User data file.
For more information about the parameters of resources used in Terraform, see the relevant provider documentation:
- yandex_iam_service_account
- yandex_resourcemanager_folder_iam_member
- yandex_vpc_network
- yandex_vpc_subnet
- yandex_vpc_security_group
- yandex_compute_image
- yandex_compute_instance_group
- yandex_alb_backend_group
- yandex_alb_http_router
- yandex_alb_virtual_host
- yandex_alb_load_balancer
- yandex_dns_zone
- yandex_dns_recordset
-
-
In the
application-load-balancer-website.auto.tfvars
file, set the following user-defined properties:folder_id
: Folder ID.vm_user
: VM user name.ssh_key_path
: Path to the file with a public SSH key to authenticate the user on the VM. For more details, see Creating an SSH key pair.domain
: Domain name, e.g.,alb-example.com
.
-
Create resources:
-
In the terminal, go to the folder where you edited the configuration file.
-
Make sure the configuration file is correct using this command:
terraform validate
If the configuration is correct, you will get this message:
Success! The configuration is valid.
-
Run this command:
terraform plan
The terminal will display a list of resources with parameters. No changes will be made at this step. If there are errors in the configuration, Terraform will point them out.
-
Apply the configuration changes:
terraform apply
-
Confirm the changes: type
yes
into the terminal and press Enter.
-