【问题标题】:Make null resource to wait on aws_route53_record in Terraform使空资源等待 Terraform 中的 aws_route53_record
【发布时间】:2020-12-14 01:40:00
【问题描述】:
resource "aws_route53_record" "record" {
    zone_id = data.aws_route53_zone.selected.zone_id
    name    = "${var.sfs_instance_name}.example.com"
    type    = "A"
    ttl     = "60"
    records = ["${aws_eip.sfs.public_ip}"]
  }

resource "null_resource" "sfs-ssl-certs" {

  connection {
    type        = "ssh"
    user        = "centos"
    host        = aws_eip.sfs.public_ip
    private_key = file("../keys/${var.sfs_instance_name}.pem")
  }

  provisioner "remote-exec" {
    inline = [
      "set -x",
      "sudo certbot --nginx -d ${var.sfs_instance_name}.example.com --register-unsafely-without-email --agree-tos --force-renewal --non-interactive > /home/centos/get_cert.log"
    ]
}

为域名"${var.sfs_instance_name}.example.com"动态创建nginx ssl,在执行结束时添加条目,因此certbox ssl证书创建失败,我该如何克服它,我可以等待resource "aws_route53_record"条目创建还是有其他解决方法?

【问题讨论】:

    标签: amazon-web-services terraform terraform-provider-aws


    【解决方案1】:

    我认为解决方法是添加depends_on:

    resource "null_resource" "sfs-ssl-certs" {
    
      depends_on = [aws_route53_record.record]
    
      connection {
        type        = "ssh"
        user        = "centos"
        host        = aws_eip.sfs.public_ip
        private_key = file("../keys/${var.sfs_instance_name}.pem")
      }
    
      provisioner "remote-exec" {
        inline = [
          "set -x",
          "sudo certbot --nginx -d ${var.sfs_instance_name}.example.com --register-unsafely-without-email --agree-tos --force-renewal --non-interactive > /home/centos/get_cert.log"
        ]
    }
    

    【讨论】:

      【解决方案2】:

      您可以通过从资源中正确插入值来避免depends_on

      resource "aws_route53_record" "record" {
          zone_id = data.aws_route53_zone.selected.zone_id
          name    = "${var.sfs_instance_name}.example.com"
          type    = "A"
          ttl     = "60"
          records = [aws_eip.sfs.public_ip]
        }
      
      resource "null_resource" "sfs-ssl-certs" {
      
        connection {
          type        = "ssh"
          user        = "centos"
          host        = aws_eip.sfs.public_ip
          private_key = file("../keys/${var.sfs_instance_name}.pem")
        }
      
        provisioner "remote-exec" {
          inline = [
            "set -x",
            "sudo certbot --nginx -d ${aws_route53_record.record.name} --register-unsafely-without-email --agree-tos --force-renewal --non-interactive > /home/centos/get_cert.log"
          ]
      }
      

      当无法通过将资源的值插入到其他资源中来直接告诉其他资源依赖链时,Terraform 只需要 depends_on 参数。一般来说,如果你可以避免使用它并坚持直接资源插值,那么它会让事情变得更好。作为另一个积极的方面,它避免了您必须在两个地方通过字符串连接来构建 DNS 记录名称。

      Terraform documentation around resource dependencies 也建议避免使用depends_on,除非绝对必要:

      配置中的大多数资源没有任何特定的 关系,并且 Terraform 可以对几个不相关的 资源并行。

      但是,有些资源必须在其他特定资源之后进行处理 资源;有时这是因为资源的工作方式,以及 有时资源的配置只需要信息 由其他资源生成。

      大多数资源依赖项都是自动处理的。地形 分析资源块中的任何表达式以查找对 其他对象,并将这些引用视为隐式排序 创建、更新或销毁资源时的要求。自从 大多数在行为上依赖于其他资源的资源也 参考这些资源的数据,通常不需要手动 指定资源之间的依赖关系。

      但是,某些依赖项无法隐式识别 配置。例如,如果 Terraform 必须管理访问控制 政策并采取需要这些政策存在的行动, 访问策略和资源之间存在隐藏的依赖关系 谁的创造取决于它。在这些罕见的情况下,depends_on 元参数可以显式指定依赖项。

      【讨论】:

        猜你喜欢
        • 2022-10-23
        • 2021-06-19
        • 1970-01-01
        • 2020-09-02
        • 2018-11-08
        • 2021-03-16
        • 1970-01-01
        • 2020-01-15
        • 1970-01-01
        相关资源
        最近更新 更多