英文:
How to create a custom domain for an application load balancer using Terraform in AWS
问题
I understand your request. Here is the translated content:
我一直在尝试找出如何使用Terraform创建具有自定义名称的应用负载均衡器。到目前为止,我有以下配置:
```hcl
resource "aws_alb" "application_load_balancer" {
name = "${var.brand}-be-${var.environment_name}-load-balancer"
tags = local.tags
load_balancer_type = "application"
subnets = data.aws_subnets.public_subnets.ids
# 安全组
security_groups = [aws_security_group.sg_load_balancer.id]
}
# 新建 new-dev.foocorp.com 的托管区域
resource "aws_route53_zone" "zone_dev" {
name = "new-dev.foocorp.com"
comment = "new-dev.foocorp.com 的托管区域"
records = [aws_alb.application_load_balancer.dns_name]
tags = merge(local.tags, {
Name = "new-dev.foocorp.com"
})
}
然而,我遇到了以下错误:
╷
│ 错误:不支持的参数
│
│ 在 main.tf 的第 323 行,resource "aws_route53_zone" "zone_dev":
│ 323: records = [ aws_alb.application_load_balancer.dns_name ]
│
│ 此处不应使用名为 "records" 的参数。
╵
据我了解,似乎不可能告诉ALB使用特定的域名,我必须等待它被创建,然后引用它的域名来创建新的Route 53记录?这是正确的吗?有没有更好的方法来做到这一点?
我想要的是,而不是 http://foo-bar-load-balancer-1234567890.us-east-1.elb.amazonaws.com/
,我希望有 http://new-dev.foocorp.com/
。
另外,AWS中的DNS记录还没有通过Terraform创建,这些区域在根账户中,而我正在尝试在子账户中进行操作,该子账户将用于dev
环境。我是否需要一些IAM策略来完成这个操作?
<details>
<summary>英文:</summary>
I've been trying to figure out how to create application load balancer with a custom name using Terraform. So far I have this:
resource "aws_alb" "application_load_balancer" {
name = "${var.brand}-be-${var.environment_name}-load-balancer"
tags = local.tags
load_balancer_type = "application"
subnets = data.aws_subnets.public_subnets.ids
security group
security_groups = [aws_security_group.sg_load_balancer.id]
}
Hosted Zone for new-dev.foocorp.com
resource "aws_route53_zone" "zone_dev" {
name = "new-dev.foocorp.com"
comment = "Hosted Zone for new-dev.foocorp.com"
records = [ aws_alb.application_load_balancer.dns_name ]
tags = merge(local.tags, {
Name = "new-dev.foocorp.com"
})
}
However, I'm getting:
╷
│ Error: Unsupported argument
│
│ on main.tf line 323, in resource "aws_route53_zone" "zone_dev":
│ 323: records = [ aws_alb.application_load_balancer.dns_name ]
│
│ An argument named "records" is not expected here.
╵
From what I understand, it's not possible to tell the ALB to use a specific domain name and I have to wait for it to be created in order to then get a reference to it's domain and use that when creating a new Route 53 entry? Is this correct? Is there a better way to do this?
Instead of `http://foo-bar-load-balancer-1234567890.us-east-1.elb.amazonaws.com/`, I would like to have `http://new-dev.foocorp.com/`.
In addition, the DNS records in AWS have not been Terraformed yet and the zones are in the root account and I'm trying to do this in a sub-account where the `dev` environment will be. Will I need some IAM policies for this?
</details>
# 答案1
**得分**: 4
要定义`records`,您必须使用[aws_route53_record](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route53_record),而不是`aws_route53_zone`。示例在TF文档中,但应该是:
```hcl
resource "aws_route53_record" "new-dev" {
zone_id = aws_route53_zone.zone_dev.zone_id
name = "new-dev.foocorp.com"
type = "A"
ttl = 300
records = [aws_alb.application_load_balancer.dns_name]
}
英文:
To define records
you have to use aws_route53_record, not aws_route53_zone
. Example is in TF docs, but it should be:
resource "aws_route53_record" "new-dev" {
zone_id = aws_route53_zone.zone_dev.zone_id
name = "new-dev.foocorp.com"
type = "A"
ttl = 300
records = [aws_alb.application_load_balancer.dns_name]
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论