【问题标题】:Terraform - Use data source output in variable defaultTerraform - 在变量默认值中使用数据源输出
【发布时间】:2020-11-03 22:30:39
【问题描述】:

我正在创建一个模块来启动一个基本的 Web 服务器。

我正在尝试获取它,以便如果用户未指定 AMI,则使用该区域的 ubuntu 映像。

我有一个data 块来获取该区域的 ubuntu 16.04 图像的 AMI ID,但我无法将其设置为变量的默认值,因为插值不起作用。

我的模块如下:-

main.tf

resource "aws_instance" "web" {
  ami = "${var.aws_ami}"
  instance_type = "${var.instance_type}"
  security_groups = ["${aws_security_groups.web.id}"]

  tags {
      Name = "WEB_SERVER"
  }
}

resource "aws_security_groups" "web" {
  name = "WEB_SERVER-HTTP-HTTPS-SG"

  ingress {
      from_port = "${var.http_port}"
      to_port = "${var.http_port}"
      protocol = "tcp"
      cidr_blocks = ["0.0.0.0/0"] 
  }

  ingress {
      from_port = "${var.https_port}"
      to_port = "${var.https_port}"
      protocol = "tcp"
      cidr_blocks = ["0.0.0.0/0"]
  }

  egress {
      from_port = 0
      to_port = 0
      protocol = "-1"
      cidr_blocks = ["0.0.0.0/0"]
  }
}

变量.tf

variable "instance_type" {
  description = "The instance size to deploy. Defaults to t2.micro"
  default = "t2.micro"
}

variable "http_port" {
  description = "The port to use for HTTP traffic. Defaults to 80"
  default = "80"
}

variable "https_port" {
  description = "The port to use for HTTPS traffic. Defaults to 443"
  default = "443"
}



data "aws_ami" "ubuntu" {
    filter {
        name = "state"
        values = ["available"]
    }

    filter {
    name   = "name"
    values = ["ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-*"]
  }

    filter {
        name   = "virtualization-type"
        values = ["hvm"]
    }

    owners = ["099720109477"]

}

locals {
  default_ami = "${data.aws_ami.ubuntu.id}"
}

variable aws_ami {
    description = "The AMI used to launch the instance. Defaults to Ubuntu 16.04"
    default = "${local.default_ami}"
}

【问题讨论】:

    标签: terraform


    【解决方案1】:

    尝试使用三元运算符插值:

    variable "user_specified_ami" {
      default = "ami-12345678"
    }
    
    resource "aws_instance" "web" {
      ami = "${var.user_specified_ami == "" ? data.aws_ami.ubuntu.id : var.user_specified_ami}"
      # ... other params omitted ....
    }
    

    user_specified_ami 的默认值设置为使用该AMI 的东西。将其设置为空白以使用 Terraform 从 AWS 提供商处获取的 AMI ID。

    即如果user_specified_ami 是其他任何空白 (""),则将为 AMI 选择它,否则 AMI Terraform 从 AWS 获取。

    顺便说一句,也许您想在 data "aws_ami" 资源中使用 most_recent = true 参数?

    【讨论】:

    • 为什么要为user_specified_ami 设置默认值而不是""?除非设置了user_specified_ami,否则对于默认为数据源AMI的模块来说,这似乎是一个更干净的API。
    • 是的,没错。我没有关注事物的模块方面,因为我不想使解决方案的本质过于复杂。
    【解决方案2】:

    与其他答案类似的解决方案,但使用coalesce function

    variable "user_specified_ami" {
      default = ""
    }
    
    resource "aws_instance" "web" {
      ami = coalesce(var.user_specified_ami, data.aws_ami.ubuntu.id)
    }
    

    【讨论】:

      【解决方案3】:

      KJH 的回答效果很好,但我觉得内联逻辑有点混乱,所以我使用null_data_source 进行了抽象。这就是它的样子:

      variable "ami" {
        default = ""
      }
      
      data "null_data_source" "data_variables" {
        inputs = {
          ami = "${var.ami == "" ? data.aws_ami.ubuntu.id : var.ami}"
        }
      }
      
      resource "aws_instance" "web" {
        ami = "${data.null_data_source.data_variables.outputs["ami"]}"
        # ... other params omitted ....
      }
      

      【讨论】:

        猜你喜欢
        • 2022-11-14
        • 2021-05-22
        • 2021-10-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-28
        • 1970-01-01
        • 2020-07-10
        相关资源
        最近更新 更多