[Yandex Cloud documentation](../../../index.md) > [Yandex Compute Cloud](../../index.md) > [Tutorials](../index.md) > [OpenCart online store](index.md) > Terraform

# Creating an OpenCart online store using Terraform

To create an [OpenCart online store](index.md) using Terraform:

1. [Get your cloud ready](#before-you-begin).
1. [Create an infrastructure](#deploy).
1. [Configure OpenCart](#configure-opencart).

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


## Get your cloud ready {#before-you-begin}

Sign up for Yandex Cloud and create a [billing account](../../../billing/concepts/billing-account.md):
1. Navigate to the [management console](https://console.yandex.cloud) and log in to Yandex Cloud or create a new account.
1. On the **[Yandex Cloud Billing](https://center.yandex.cloud/billing/accounts)** page, make sure you have a billing account linked and it has the `ACTIVE` or `TRIAL_ACTIVE` [status](../../../billing/concepts/billing-account-statuses.md). If you do not have a billing account, [create one](../../../billing/quickstart/index.md) and [link](../../../billing/operations/pin-cloud.md) a cloud to it.

If you have an active billing account, you can create or select a [folder](../../../resource-manager/concepts/resources-hierarchy.md#folder) for your infrastructure on the [cloud page](https://console.yandex.cloud/cloud).

[Learn more about clouds and folders here](../../../resource-manager/concepts/resources-hierarchy.md).

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

* VM instance: use of computing resources, storage, public IP address, and OS (see [Compute Cloud pricing](../../pricing.md)).
* Managed Service for MySQL® cluster if created for DBMS support and maintenance: computing resources allocated to hosts, size of storage and backups (see [Managed Service for MySQL® pricing](../../../managed-mysql/pricing.md)).
* Public IP addresses if public access is enabled for cluster hosts (see [Virtual Private Cloud pricing](../../../vpc/pricing.md)).

## Create an infrastructure {#deploy}

With [Terraform](https://www.terraform.io/), you can quickly create a cloud infrastructure in Yandex Cloud and manage it using configuration files. These files store the infrastructure description written in HashiCorp Configuration Language (HCL). If you change the configuration files, Terraform automatically detects which part of your configuration is already deployed, and what should be added or removed.

Terraform is distributed under the [Business Source License](https://github.com/hashicorp/terraform/blob/main/LICENSE). The [Yandex Cloud provider for Terraform](https://github.com/yandex-cloud/terraform-provider-yandex) is distributed under the [MPL-2.0](https://www.mozilla.org/en-US/MPL/2.0/) license.

For more information about the provider resources, see the relevant documentation on the [Terraform](https://www.terraform.io/docs/providers/yandex/index.html) website or [its mirror](../../../terraform/index.md).

To set up your OpenCart online store with Terraform:

1. [Install Terraform](../../../tutorials/infrastructure-management/terraform-quickstart.md#install-terraform), [get the authentication credentials](../../../tutorials/infrastructure-management/terraform-quickstart.md#get-credentials), and specify the source for installing the Yandex Cloud provider (see [Configure your provider](../../../tutorials/infrastructure-management/terraform-quickstart.md#configure-provider), step 1).

1. Prepare your infrastructure description files:

    {% list tabs %}

    - Ready-made configuration

      1. Clone the repository with configuration files:

          ```bash
          git clone https://github.com/yandex-cloud-examples/yc-opencart-store.git
          ```

      1. Navigate to the repository directory. It should now contain the following files:
          * `opencart.tf`: New infrastructure configuration.
          * `opencart.auto.tfvars`: User data file.

    - Creating files manually

       1. Create a configuration file folder.

       1. In the folder, create:

           1. `opencart.tf` configuration file:

               {% cut "opencart.tf" %}

               ```hcl
               # Declaring variables for confidential parameters
               
               variable "folder_id" {
                 type = string
               }
               
               variable "vm_user" {
                 type = string
               }
               
               variable "ssh_key_path" {
                 type = string
               }
               
               variable "db_user" {
                 type = string
               }
               
               variable "db_password" {
                 type      = string
                 sensitive = true
               }
               
               # Adding other variables
               
               locals {
                 network_name = "network-1"
                 subnet_name1 = "subnet-1"
                 subnet_name2 = "subnet-2"
                 sg_db_name   = "opencart-sg"
                 sg_vm_name   = "opencart-sg-vm"
                 vm_name      = "opencart"
                 cluster_name = "opencart"
                 db_name      = "db1"
               }
               
               # Configuring a provider
               
               terraform {
                 required_providers {
                   yandex = {
                     source  = "yandex-cloud/yandex"
                     version = ">= 0.47.0"
                   }
                 }
               }
               
               provider "yandex" {
                 folder_id = var.folder_id
               }
               
               # Creating a cloud network
               
               resource "yandex_vpc_network" "network-1" {
                 name = local.network_name
               }
               
               # Creating subnets
               
               resource "yandex_vpc_subnet" "subnet-1" {
                 name           = local.subnet_name1
                 zone           = "ru-central1-a"
                 network_id     = yandex_vpc_network.network-1.id
                 v4_cidr_blocks = ["192.168.1.0/24"]
               }
               
               resource "yandex_vpc_subnet" "subnet-2" {
                 name           = local.subnet_name2
                 zone           = "ru-central1-b"
                 network_id     = yandex_vpc_network.network-1.id
                 v4_cidr_blocks = ["192.168.2.0/24"]
               }
               
               # Creating security groups
               
               resource "yandex_vpc_security_group" "opencart-sg" {
                 name       = local.sg_db_name
                 network_id = yandex_vpc_network.network-1.id
               
                 egress {
                   protocol       = "ANY"
                   description    = "any"
                   v4_cidr_blocks = ["0.0.0.0/0"]
                 }
               
                 ingress {
                   protocol       = "TCP"
                   description    = "ext-https"
                   v4_cidr_blocks = ["0.0.0.0/0"]
                   port           = 3306
                 }
               }
               
               resource "yandex_vpc_security_group" "opencart-sg-vm" {
                 name       = local.sg_vm_name
                 network_id = yandex_vpc_network.network-1.id
               
                 egress {
                   protocol       = "ANY"
                   description    = "any"
                   v4_cidr_blocks = ["0.0.0.0/0"]
                   from_port      = 0
                   to_port        = 65535
                 }
               
                 ingress {
                   description    = "HTTP"
                   protocol       = "TCP"
                   v4_cidr_blocks = ["0.0.0.0/0"]
                   port           = 80
                 }
               
                 ingress {
                   protocol       = "TCP"
                   description    = "ssh"
                   v4_cidr_blocks = ["0.0.0.0/0"]
                   port           = 22
                 }
               
                 ingress {
                   protocol       = "TCP"
                   description    = "ext-https"
                   v4_cidr_blocks = ["0.0.0.0/0"]
                   port           = 443
                 }
               }
               
               # Specifying a prebuilt VM image
               
               resource "yandex_compute_image" "opencart-image" {
                 source_family = "opencart"
               }
               
               # Creating a VM instance
               
               resource "yandex_compute_instance" "opencart" {
                 name        = "opencart"
                 platform_id = "standard-v3"
                 zone        = "ru-central1-a"
               
                 resources {
                   core_fraction = 20
                   cores         = 2
                   memory        = 4
                 }
               
                 boot_disk {
                   initialize_params {
                     image_id = yandex_compute_image.opencart-image.id
                     type     = "network-ssd"
                     size     = "13"
                   }
                 }
               
                 network_interface {
                   subnet_id          = yandex_vpc_subnet.subnet-1.id
                   security_group_ids = [yandex_vpc_security_group.opencart-sg-vm.id]
                   nat                = true
                 }
               
                 metadata = {
                   user-data = "#cloud-config\nusers:\n  - name: ${var.vm_user}\n    groups: sudo\n    shell: /bin/bash\n    sudo: ['ALL=(ALL) NOPASSWD:ALL']\n    ssh_authorized_keys:\n      - ${file("${var.ssh_key_path}")}"
                 }
               }
               
               # Creating a cluster MySQL®
               # If you do not need a cluster, delete the code block creating the MySQL® cluster, database, and user
               
               resource "yandex_mdb_mysql_cluster" "opencart-mysql" {
                 name               = local.cluster_name
                 environment        = "PRODUCTION"
                 network_id         = yandex_vpc_network.network-1.id
                 version            = "8.0"
                 security_group_ids = [yandex_vpc_security_group.opencart-sg.id]
               
                 resources {
                   resource_preset_id = "s2.micro"
                   disk_type_id       = "network-ssd"
                   disk_size          = "10"
                 }
               
                 host {
                   zone             = "ru-central1-a"
                   subnet_id        = yandex_vpc_subnet.subnet-1.id
                   assign_public_ip = false
                 }
               
                 host {
                   zone             = "ru-central1-b"
                   subnet_id        = yandex_vpc_subnet.subnet-2.id
                   assign_public_ip = false
                 }
               }
               
               # Creating a database for MySQL®
               
               
               resource "yandex_mdb_mysql_database" "db1" {
                 cluster_id = yandex_mdb_mysql_cluster.opencart-mysql.id
                 name       = local.db_name
               }
               
               # Creating a user for MySQL®
               
               resource "yandex_mdb_mysql_user" "user1" {
                 cluster_id = yandex_mdb_mysql_cluster.opencart-mysql.id
                 name       = var.db_user
                 password   = var.db_password
                 permission {
                   database_name = yandex_mdb_mysql_database.db1.name
                   roles         = ["ALL"]
                 }
               }
               ```

               {% endcut %}

           1. `opencart.auto.tfvars` user data file:

               {% cut "opencart.auto.tfvars" %}

               ```hcl
               folder_id    = "<folder_ID>"
               vm_user      = "<VM_user_name>"
               ssh_key_path = "<path_to_public_SSH_key>"
               db_user      = "<DB_user_name>"
               db_password  = "<DB_password>"
               ```

               {% endcut %}

    {% endlist %}

    Learn more about the properties of Terraform resources in the relevant provider guides:

    * [Network](../../../vpc/concepts/network.md#network): [yandex_vpc_network](../../../terraform/resources/vpc_network.md)
    * [Subnets](../../../vpc/concepts/network.md#subnet): [yandex_vpc_subnet](../../../terraform/resources/vpc_subnet.md)
    * [Security groups](../../../vpc/concepts/security-groups.md): [yandex_vpc_security_group](../../../terraform/resources/vpc_security_group.md)
    * [VM image](../../concepts/image.md): [yandex_compute_image](../../../terraform/resources/compute_image.md)
    * [VM instance](../../concepts/vm.md): [yandex_compute_instance](../../../terraform/resources/compute_instance.md)
    * [MySQL® cluster](../../../managed-mysql/concepts/index.md): [yandex_mdb_mysql_cluster](../../../terraform/resources/mdb_mysql_cluster.md)
    * DB MySQL®: [yandex_mdb_mysql_database](../../../terraform/resources/mdb_mysql_database.md)
    * User MySQL®: [yandex_mdb_mysql_user](../../../terraform/resources/mdb_mysql_user.md)

1. In the `opencart.auto.tfvars` file, set the following user-defined properties:

    * `folder_id`: [Folder ID](../../../resource-manager/operations/folder/get-id.md).
    * `vm_user`: VM user name.
    * `ssh_key_path`: Path to the public SSH key to authenticate the user on the VM. For more information, see [Creating an SSH key pair](../../operations/vm-connect/ssh.md#creating-ssh-keys).
    * `db_user`: DB username, e.g., `user1`.
    * `db_password`: DB password (8 to 128 characters).

1. Create the resources:

    1. In the terminal, navigate to the configuration file directory.
    1. Make sure the configuration is correct using this command:
    
       ```bash
       terraform validate
       ```
    
       If the configuration is valid, you will get this message:
    
       ```bash
       Success! The configuration is valid.
       ```
    
    1. Run this command:
    
       ```bash
       terraform plan
       ```
    
       You will see a list of resources and their properties. No changes will be made at this step. Terraform will show any errors in the configuration.
    1. Apply the configuration changes:
    
       ```bash
       terraform apply
       ```
    
    1. Type `yes` and press **Enter** to confirm the changes.

1. [Get the public IP address of the VM](../../operations/instance-groups/get-info.md): you will need it later to [configure OpenCart](#configure-opencart).

Once you created the infrastructure, [configure OpenCart](#configure-opencart).

## Configure OpenCart {#configure-opencart}

1. Open the web interface of the OpenCart online store. In the browser, open `http://<VM_public_IP_address>/`. The OpenCart settings page opens.
1. Read the license and click **Continue**.

   ![Step 1](../../../_assets/tutorials/opencart/opencart1.png)

1. Make sure that all lines with system requirements are marked with green ticks and click **Continue**.

   ![Step 2](../../../_assets/tutorials/opencart/opencart2.png)

1. Set up access to the DB:

   {% list tabs %}

   - Local server MySQL®

      DB connection attributes are generated in a special file when a VM is created:
      1. Log in to the created VM via SSH.
      1. Switch to `sudo -i` administration mode.
      1. Open `default_passwords.txt` in the admin's home directory:

         ```bash
         root@opencart:~# cat default_passwords.txt
         MYSQL_USER=opencart
         MYSQL_PASS=qDbvN1R6tA6ET
         MYSQL_ROOT_PASS=5DiVb80l1kXVz
         MYSQL_DB=opencart
         ```

      1. On the OpenCart setup page, in the DB section, enter the relevant data:
         * **Username**: `MYSQL_USER` variable value.
         * **Database**: `MYSQL_DB` variable value.
         * **Password**: `MYSQL_PASS` variable value.

         Leave the other fields unchanged.

   - Cluster Managed Service for MySQL®

      If you are using a Managed Service for MySQL® cluster, enter the required cluster attributes:
      * **Hostname**: Enter the [fully qualified domain name (FQDN)](../../concepts/network.md#hostname) of the created DB. To find out this name:
         1. Open the folder page in the [management console](https://console.yandex.cloud) in a new browser tab.
         1. Go to the **Managed Service for MySQL®** section.
         1. Select the cluster you created in the table.
         1. Select the **Hosts** tab in the left menu.
         1. Hover over the **Hostname** field (for example, `rc1c-vok617m35g3dj23i`) and copy the database's FQDN by clicking ![copy](../../../_assets/console-icons/copy.svg).
      * **Username**: Username (`user1` in the example).
      * **Database**: DB name (`db1` in the example).
      * **Password**: User password you specified.

      Leave the other fields unchanged.

   {% endlist %}

1. Enter the administrator's name, password, and current email address. Then click **Continue**.

   ![Step 3](../../../_assets/tutorials/opencart/opencart3.png)

1. A page will open to notify you that system configuration is complete. To configure the online store, click **Login to your administration** and enter your admin username and password.

   ![Step 4](../../../_assets/tutorials/opencart/opencart4.png)

1. When the installation is complete, log in to the VM via SSH and delete the installation files you no longer need:

   ```bash
   user@opencart:~$ sudo -i
   root@opencart:~# rm -rf /var/www/opencart/install/
   ```

1. To test the home page, go to `http://<VM_public_IP_address>/`. You will see your website home page the way your online store visitors will see it.

   ![Step 5](../../../_assets/tutorials/opencart/opencart5.png)

## Delete the resources you created {#clear-out}

How to delete the resources you created:

1. Open the `opencart.tf` configuration file and delete your infrastructure description from it.
1. Apply the changes:

    1. In the terminal, navigate to the configuration file directory.
    1. Make sure the configuration is correct using this command:
    
       ```bash
       terraform validate
       ```
    
       If the configuration is valid, you will get this message:
    
       ```bash
       Success! The configuration is valid.
       ```
    
    1. Run this command:
    
       ```bash
       terraform plan
       ```
    
       You will see a list of resources and their properties. No changes will be made at this step. Terraform will show any errors in the configuration.
    1. Apply the configuration changes:
    
       ```bash
       terraform apply
       ```
    
    1. Type `yes` and press **Enter** to confirm the changes.

#### See also {#see-also}

* [Creating an OpenCart online store from the management console](console.md)