Using Arvancloud Terraform
Terraform is an open-source tool for creating and managing cloud infrastructure with code, which provides a simple process for deploying products on the cloud. Today, the recommended approach to implementing infrastructure is to design it as IaC (Infrastructure as Code), so that infrastructure can be deployed accurately and quickly with less human intervention.
With this tool, the cloud resources you need are defined in Terraform files. Using these files and sufficient access, you can test and deploy resources and services on cloud products without logging in to the panel.
To use Arvancloud Terraform, you first need to install Terraform and set up and configure a provider file. In this guide, we look at how to install and configure Terraform.
Installing Terraform
First, make sure a suitable version of Terraform is installed on your system. The minimum required version is Terraform 1.0 or later.
The official Terraform installation guide helps you get this version for your operating system.
Configuring the Provider
After installing Terraform, to configure the Arvancloud provider, create a file with the .tf extension (for example, main.tf) and enter the provider settings in it.
Copy the following content into your main.tf file:
terraform {
required_providers {
arvancloud = {
source = "terraform.arvancloud.ir/arvancloud/arvancloud"
# version = "x.y.z" # optional
}
}
}
provider "arvancloud" {
api_key = "your_api_key"
}
To use the Arvancloud provider, you need to create a machine user and get its access key. The Machine User guide helps you with this.
All operations in Terraform are done by the machine user. So make sure the machine user has the IAM permissions needed for the operations. The guide to managing access in your Arvancloud account helps you with this.
terraformblock: in this section, the requiredprovider(arvancloud) and its source are specified. You can also set a specific version of theprovider. If no version is specified, the latest version is installed automatically.providerblock: in this section, the authentication information, such as the access key (api_key), is entered to connect to Arvancloud.
After saving the file, run the following command in the same path to download and install the provider:
terraform init
Creating Resources
To create resources, simply add their definitions to one of the .tf files (for example, main.tf). The following sample code creates an A-type DNS record for a domain on the Arvancloud CDN:
resource "arvancloud_cdn_domain_dns_record" "a-example" {
domain = "cdn-terraform.ir"
name = "a-terraform"
ttl = 120
cloud = true
upstream_https = "https"
ip_filter_mode = {
count = "single"
order = "none"
geo_filter = "none"
}
type = "a"
value = {
a = [
{
ip = "1.1.1.1"
port = 1001
},
{
ip = "1.1.1.2"
weight = 101
}
]
}
}
After saving the file, run the following commands.
To run a preview:
terraform plan
To apply the changes:
terraform apply
You can define resources in the same
main.tfor in separate files (such asdns.tf). Terraform treats all.tffiles in the current folder as one configuration unit.
Common Terraform Commands
After the initial setup, you can manage your infrastructure with the following commands:
terraform fmt: formats configuration files according to Terraform standards.terraform validate: checks that your configuration files are correct and valid.terraform plan: gives a preview of the changes that will be applied (such as creating, editing, or deleting resources).terraform apply: applies the specified changes to the infrastructure.terraform destroy: deletes all resources managed by Terraform.
For more use cases and examples of Arvancloud Terraform settings, see the examples and usage guide in the Arvancloud GitLab.