如何在AWS中使用Terraform为应用负载均衡器创建自定义域名。

huangapple go评论51阅读模式
英文:

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&#39;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&#39;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&#39;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&#39;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&#39;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 &quot;aws_route53_record&quot; &quot;new-dev&quot; {
  zone_id = aws_route53_zone.zone_dev.zone_id
  name    = &quot;new-dev.foocorp.com&quot;
  type    = &quot;A&quot;
  ttl     = 300
  records = [aws_alb.application_load_balancer.dns_name]
}

huangapple
  • 本文由 发表于 2023年5月15日 09:06:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76250309.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定