Terraform integration example
Terraform is an open source tool that lets you safely and efficiently provision and manage cloud infrastructure. You can use Terraform to manage PrivateZone resources. This topic describes how to use Terraform to create an authoritative domain name (Zone).
Supported resources
For a list of PrivateZone resources and data sources that you can orchestrate with Terraform, see PrivateZone resources and data sources. If you are new to Terraform, see Introduction to Terraform.
Resource type | Resource link | Description |
Resources | Resolver endpoint | |
Resolver forward rule | ||
Attaches or detaches a rule and a list of VPCs. | ||
Cross-account VPC authorization | ||
PrivateZone | ||
Attaches or detaches a zone and a list of VPCs. | ||
DNS records of a PrivateZone | ||
Data Sources | Get endpoint information. | |
Get region information. | ||
Get forward rule information. | ||
PrivateZone status | ||
Get DNS record information. | ||
Get PrivateZone information. |
Procedure
Step 1: Configure permissions
To run a Terraform template, you need the permissions of a Resource Access Management (RAM) user. Create a RAM user, obtain an AccessKey, and add an access policy. The AccessKey is configured as an environment variable for Terraform. You must grant the RAM user the AliyunPvtzFullAccess permission to manage PrivateZone resources because the template creates an authoritative domain name (Zone). For more information, see Create a RAM user and Manage RAM user permissions.
Step 2: Install Terraform
To install and configure Terraform locally, see Install and configure Terraform locally.
After the installation is complete, open a command-line terminal and enter
terraform version. If the version information is returned, the installation is successful.If you do not want to install Terraform, you can use Cloud Shell. Cloud Shell is an online service from Alibaba Cloud that has a built-in Terraform runtime environment.
Step 3: Write the template
Create a file named main.tf and add the following content to create an authoritative domain name (Zone).
resource "alicloud_pvtz_zone" "test" {
zone_name = "test.example.com"
proxy_pattern = "ZONE"
}For more information about the configuration of the alicloud_pvtz_zone resource type, see PrivateZone resources and data sources.
Step 4: Run the template
Initialize and load the modules, including the provider modules.
terraform initshell@Alicloud:~$ terraform init Initializing the backend... Initializing provider plugins... - Checking for available provider plugins... - Downloading plugin for provider "alicloud" (hashicorp/alicloud) 1.227.1... The following providers do not have any version constraints in configuration, so the latest version was installed. To prevent automatic upgrades to new major versions that may contain breaking changes, it is recommended to add version = "..." constraints to the corresponding provider blocks in configuration, with the constraint strings suggested below. * provider.alicloud: version = "~> 1.227" Warning: registry.terraform.io: For users on Terraform 0.13 or greater, this provider has moved to aliyun/alicloud. Please update your source in required_providers. Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work. If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, otherValidate the template syntax.
terraform planshell@Alicloud:~$ terraform plan Refreshing Terraform state in-memory prior to plan... The refreshed state will be used to calculate this plan, but will not be persisted to local or remote state storage. ------------------------------------------------------------------------ An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # alicloud_pvtz_zone.test will be created + resource "alicloud_pvtz_zone" "test" { + creation_time = (known after apply) + id = (known after apply) + is_ptr = (known after apply) + name = (known after apply) + proxy_pattern = "ZONE" + record_count = (known after apply) + resource_group_id = (known after apply) + update_time = (known after apply) + zone_name = "test.example.com" + user_info { + region_ids = (known after apply) + user_id = (known after apply) } } Plan: 1 to add, 0 to change, 0 to destroy. Note: You didn't specify an "-out" parameter to save this plan, so Terraform can't guarantee that exactly these actions will be performed if "terraform apply" is subsequently run.Deploy the template.
terraform applyThe following output shows some of the logs that are returned during the deployment.
Plan: 1 to add, 0 to change, 0 to destroy. Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value: yes alicloud_pvtz_zone.test: Creating... alicloud_pvtz_zone.test: Creation complete after 1s [id=bbc559fc1239d5c06928xxx]Enter `yes` to apply the changes. Use
terraform destroyto release the resources that are created by the template.After the Zone is created, you can view it using OpenAPI or an SDK, or in the Cloud Backup console. In the domain name list on the PrivateZone domain management page, you can see the created zone test.example.com with domain type Normal Zone, 0 records, and zone scope Not Set.