Skip to content

Instantly share code, notes, and snippets.

@MarshalW
Created November 27, 2023 03:11
Show Gist options
  • Select an option

  • Save MarshalW/fe08af08aac9a1b4455305b64f401ef1 to your computer and use it in GitHub Desktop.

Select an option

Save MarshalW/fe08af08aac9a1b4455305b64f401ef1 to your computer and use it in GitHub Desktop.

Revisions

  1. MarshalW created this gist Nov 27, 2023.
    72 changes: 72 additions & 0 deletions terraform-aliyun-dns-record.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    # Terraform 管理阿里云域名解析记录

    使用 Terraform 自动管理域名解析记录,很方便,速度也不慢。

    相比之下:

    - 阿里云,如果用 `aliyun-cli`,速度也不慢,但是需要为自动化编写脚本
    - aliyun-cli 还可以使用 docker 方式使用 - 但是因为加载镜像过程,从本地执行会比较慢


    `main.tf`:

    ```tf
    terraform {
    required_providers {
    alicloud = {
    source = "aliyun/alicloud"
    }
    }
    }
    provider "alicloud" {
    access_key = var.aliyun_access_key
    secret_key = var.aliyun_secret_key
    region = var.region
    }
    variable "aliyun_access_key" {
    type = string
    }
    variable "aliyun_secret_key" {
    type = string
    }
    variable "region" {
    type = string
    }
    resource "alicloud_alidns_record" "record" {
    domain_name = "your-domain.com"
    rr = "test"
    type = "A"
    value = "1.2.3.4"
    status = "ENABLE"
    }
    ```

    `terraform.tfvars`:

    ```tf
    aliyun_access_key = ""
    aliyun_secret_key = ""
    region = "cn-beijing"
    ```

    执行:

    ```bash
    # 初始化,安装 aliyun provider
    terraform init

    # 查看配置是否有问题
    terraform plan

    # 执行
    terraform apply

    # 销毁
    terraform destroy
    ```