Terraform integration example

Updated at:

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

alicloud_pvtz_endpoint

Resolver endpoint

alicloud_pvtz_rule

Resolver forward rule

alicloud_pvtz_rule_attachment

Attaches or detaches a rule and a list of VPCs.

alicloud_pvtz_user_vpc_authorization

Cross-account VPC authorization

alicloud_pvtz_zone

PrivateZone

alicloud_pvtz_zone_attachment

Attaches or detaches a zone and a list of VPCs.

alicloud_pvtz_zone_record

DNS records of a PrivateZone

Data Sources

alicloud_pvtz_endpoints

Get endpoint information.

alicloud_pvtz_resolver_zones

Get region information.

alicloud_pvtz_rules

Get forward rule information.

alicloud_pvtz_service

PrivateZone status

alicloud_pvtz_zone_records

Get DNS record information.

alicloud_pvtz_zones

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

  1. Initialize and load the modules, including the provider modules.

    terraform init
    shell@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, other
  2. Validate the template syntax.

    terraform plan
    shell@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.
  3. Deploy the template.

    terraform apply

    The 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 destroy to release the resources that are created by the template.

  4. 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.