如何使用 terraform 将 CNAME 指向主机名
How to point CNAME to a hostname using terraform
我正在使用 terraform 添加 CNAME 记录,我想将它指向我在 linode 上使用 terraform 创建的主机名。
这是我的main.tf
terraform {
required_providers {
linode = {
source = "linode/linode"
version = "1.16.0"
}
}
}
provider "linode" {
token = "some_secret_token"
}
resource "linode_instance" "example_instance" {
label = "example_instance_ubuntu-eu-west"
image = "linode/ubuntu20.04"
region = "eu-west"
type = "g6-nanode-1"
root_pass = "testing@linode"
}
resource "linode_domain" "example_domain" {
domain = "example.mydomain.com"
soa_email= "my@email.com"
type = "master"
}
resource "linode_domain_record" "example_domain_record" {
domain_id = linode_domain.example_domain.id
name = "example.mydomain.com"
record_type= "CNAME"
target = linode_instance.example_instance.label
ttl_sec = 300
}
terrafomr plan
结果
linode_domain.example_domain: 刷新状态... [id=1753447]
linode_instance.example_instance:正在刷新状态...[id=33094611]
Terraform 使用选定的提供程序生成以下执行计划。资源操作用以下符号表示:
- 创建
Terraform 将执行以下操作:
# linode_domain_record.example_domain_record will be created
+ resource "linode_domain_record" "example_domain_record" {
+ domain_id = 1753447
+ id = (known after apply)
+ name = "example.mydomain.com"
+ record_type = "CNAME"
+ target = "example_instance_ubuntu-eu-west"
+ ttl_sec = 300
}
Plan: 1 to add, 0 to change, 0 to destroy.
但是 terraform apply
错误:
linode_domain_record.example_domain_record: Creating...
╷
│ Error: Error creating a Linode DomainRecord: [400] [target] You have entered an invalid target. It must be a valid hostname.; [name] Invalid hostname
│
│ with linode_domain_record.example_domain_record,
│ on main.tf line 28, in resource "linode_domain_record" "example_domain_record":
│ 28: resource "linode_domain_record" "example_domain_record" {
│
╵
但是 example_instance_ubuntu-eu-west
确实存在于我的 linodes
根据@mark-b 的评论,您可能需要考虑使用 A
(address) 记录类型而不是 CNAME
来引用 linode。
只要 Linode 有可访问的 public IPv4 地址(将下面的 ADDR
替换为 linode 的 public IP),您可以:
resource "linode_domain_record" "example_domain_record" {
domain_id = linode_domain.example_domain.id
name = "example.mydomain.com"
record_type= "A"
target = ADDR
ttl_sec = 300
}
参见:linode_domain_record
and specifically the documentation for record_type
It's unlikely but if the instance has an IPv6 address then you'll want to use AAAA
我正在使用 terraform 添加 CNAME 记录,我想将它指向我在 linode 上使用 terraform 创建的主机名。
这是我的main.tf
terraform {
required_providers {
linode = {
source = "linode/linode"
version = "1.16.0"
}
}
}
provider "linode" {
token = "some_secret_token"
}
resource "linode_instance" "example_instance" {
label = "example_instance_ubuntu-eu-west"
image = "linode/ubuntu20.04"
region = "eu-west"
type = "g6-nanode-1"
root_pass = "testing@linode"
}
resource "linode_domain" "example_domain" {
domain = "example.mydomain.com"
soa_email= "my@email.com"
type = "master"
}
resource "linode_domain_record" "example_domain_record" {
domain_id = linode_domain.example_domain.id
name = "example.mydomain.com"
record_type= "CNAME"
target = linode_instance.example_instance.label
ttl_sec = 300
}
terrafomr plan
结果
linode_domain.example_domain: 刷新状态... [id=1753447] linode_instance.example_instance:正在刷新状态...[id=33094611]
Terraform 使用选定的提供程序生成以下执行计划。资源操作用以下符号表示:
- 创建
Terraform 将执行以下操作:
# linode_domain_record.example_domain_record will be created
+ resource "linode_domain_record" "example_domain_record" {
+ domain_id = 1753447
+ id = (known after apply)
+ name = "example.mydomain.com"
+ record_type = "CNAME"
+ target = "example_instance_ubuntu-eu-west"
+ ttl_sec = 300
}
Plan: 1 to add, 0 to change, 0 to destroy.
但是 terraform apply
错误:
linode_domain_record.example_domain_record: Creating...
╷
│ Error: Error creating a Linode DomainRecord: [400] [target] You have entered an invalid target. It must be a valid hostname.; [name] Invalid hostname
│
│ with linode_domain_record.example_domain_record,
│ on main.tf line 28, in resource "linode_domain_record" "example_domain_record":
│ 28: resource "linode_domain_record" "example_domain_record" {
│
╵
但是 example_instance_ubuntu-eu-west
确实存在于我的 linodes
根据@mark-b 的评论,您可能需要考虑使用 A
(address) 记录类型而不是 CNAME
来引用 linode。
只要 Linode 有可访问的 public IPv4 地址(将下面的 ADDR
替换为 linode 的 public IP),您可以:
resource "linode_domain_record" "example_domain_record" {
domain_id = linode_domain.example_domain.id
name = "example.mydomain.com"
record_type= "A"
target = ADDR
ttl_sec = 300
}
参见:linode_domain_record
and specifically the documentation for record_type
It's unlikely but if the instance has an IPv6 address then you'll want to use
AAAA