Skip to content

Instantly share code, notes, and snippets.

@MarshalW
Created November 27, 2023 03:11
Show Gist options
  • Save MarshalW/fe08af08aac9a1b4455305b64f401ef1 to your computer and use it in GitHub Desktop.
Save MarshalW/fe08af08aac9a1b4455305b64f401ef1 to your computer and use it in GitHub Desktop.
Terraform 管理阿里云域名解析记录

Terraform 管理阿里云域名解析记录

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

相比之下:

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

main.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:

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

执行:

# 初始化,安装 aliyun provider
terraform init

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

# 执行
terraform apply

# 销毁
terraform destroy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment