[Yandex Cloud documentation](../../index.md) > [Yandex Message Queue](../index.md) > [Getting started](index.md) > Code examples > Terraform

# Terraform

You can use Terraform to create message queues in Message Queue.

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 guides on the [Terraform](https://www.terraform.io/docs/providers/yandex/index.html) website or [its mirror](../../terraform/index.md).

  If you do not have Terraform yet, [install it and configure the Yandex Cloud provider](../../tutorials/infrastructure-management/terraform-quickstart.md#install-terraform).
  
  
  To manage infrastructure using Terraform under a service account or user accounts (a Yandex account, a federated account, or a local user), [authenticate](../../terraform/authentication.md) using the appropriate method.

  To create a message queue: 
    
  1. In the configuration file, describe the parameters of the queue to create:
            
     Sample configuration file for a standard queue:

     ```hcl
     provider "yandex" {
       folder_id = "<folder_ID>"
       zone      = "ru-central1-a"
     }

     resource "yandex_message_queue" "example_queue" {
       name                        = "mq-terraform-example"
       visibility_timeout_seconds  = 600
       receive_wait_time_seconds   = 20
       message_retention_seconds   = 1209600
       access_key                  = "<static_access_key_ID>"
       secret_key                  = "<secret_part_of_static_access_key>"
     }
     ```

     Sample configuration file for a FIFO queue:

     ```hcl
     provider "yandex" {
       folder_id = "<folder_ID>"
       zone      = "ru-central1-a"
     }

     resource "yandex_message_queue" "example-fifo-queue" {
       name                        = "mq-terraform-example.fifo"
       visibility_timeout_seconds  = 600
       receive_wait_time_seconds   = 20
       message_retention_seconds   = 1209600
       fifo_queue                  = true
       access_key                  = "<static_access_key_ID>"
       secret_key                  = "<secret_part_of_static_access_key>"
     }
     ```

     Example of a configuration file for a queue with a redirect policy for moving undelivered messages to a DLQ named `mq_terraform_deadletter_example`:

     ```hcl
     provider "yandex" {
       folder_id = "<folder_ID>"
       zone      = "ru-central1-a"
     }

     resource "yandex_message_queue" "example_fifo_queue" {
       name                        = "mq-terraform-example"
       visibility_timeout_seconds  = 600
       receive_wait_time_seconds   = 20
       message_retention_seconds   = 1209600
       redrive_policy              = jsonencode({
         deadLetterTargetArn = yandex_message_queue.example_deadletter_queue.arn
         maxReceiveCount     = 3
       })
       access_key                  = "<static_access_key_ID>"
       secret_key                  = "<secret_part_of_static_access_key>"
     }

     resource "yandex_message_queue" "example_deadletter_queue" {
       name                        = "mq_terraform_deadletter_example"
       access_key                  = "<static_access_key_ID>"
       secret_key                  = "<secret_part_of_static_access_key>"
     }
     ```

     Where:

     * `name`: Queue name.
     * `visibility_timeout_seconds`: [Visibility timeout](../concepts/visibility-timeout.md).
     * `receive_wait_time_seconds`: Waiting time for messages to enter the queue if [Long Polling](../concepts/long-polling.md) is used. The valid values are from 0 to 20 seconds. The default value is 0 seconds.
     * `message_retention_seconds`: Message retention time in the queue, in seconds.
     * `redrive_policy`: Redirect policy for moving messages to a [dead-letter queue](../concepts/dlq.md).
       * `deadLetterTargetArn`: ARN of the DLQ messages will be redirected to.
       * `maxReceiveCount`: Number of attempts to read a message from the queue before redirecting it to the DLQ.
     * `fifo_queue`: Indicates that a [FIFO queue](../concepts/queue.md#fifo-queues) is created.
     * `content_based_deduplication`: Enables [content-based deduplication](../concepts/deduplication.md#content-based-deduplication) in FIFO queues.
     * `access_key`: ID of the service account's static access key for the queue. If it is not specified in the queue configuration, the ID from the provider configuration is used.
     * `secret_key`: Secret part of the static access key. If no secret key is set in the queue configuration, the key from the provider configuration is used.

     For more information about the resources you can create with Terraform, see [this provider guide](../../terraform/index.md).
     
  1. Make sure the configuration files are correct.
     
     1. In the terminal, navigate to the directory where you created your configuration file.
     1. Run a check using this command:
        ```
        terraform plan
        ```
     If the configuration is correct, the terminal will display a list of the resources and their settings. Otherwise, Terraform will show any detected errors. 
        
  1. Deploy the cloud resources.

     1. If the configuration is correct, run this command:
        ```
        terraform apply
        ```
     1. Confirm creating the resources.
     
     This will create all the resources you need in the specified folder. You can check the new resources and their settings using the [management console](https://console.yandex.cloud). To delete the resources you created, run the `terraform destroy` command.