Terraform通过数据找到最新的ami

Ric*_*ick 6 amazon-web-services terraform terraform-provider-aws terraform0.12+

我正在尝试实现某种机制,让某人可以填写一个变量,该变量定义是要部署 Amazon Linux 机器还是自行创建的打包机。但由于某种原因,它在找到我自己的 AMI 的同时却没有找到 AWS AMI。这是代码:

模块的Main.tf:

    data "aws_ami" "latest" {
  most_recent = true
  owners      = [var.owner]

  filter {
    name   = "name"
    values = [lookup(var.default_ami, var.ami)]
  }
}

resource "aws_instance" "test-ec2deployment" {
  ami                         = data.aws_ami.latest.id
Run Code Online (Sandbox Code Playgroud)

变量.tf:

variable "default_ami" {
  type        = map
  description = "Choose windows 2016 or 2019"
  default = {
    "2016"  = "WIN2016-CUSTOM*"
    "2019"  = "WIN2019-CUSTOM*"
    "linux" = "ami-0fb02dcdd38048fb9"
  }
}

#Above are the options, here you need to make a decision.
variable "ami" {
  description = "You can either choose the 2019 or the 2016 image or the linux image."
  default     = "2019"
}
Run Code Online (Sandbox Code Playgroud)

最后是调用该模块的 main.tf:

module "aws_instance" {
  shared_credentials_file = ""
  source                  = ""
  env_name                = ""
  volume_size             = "60"
  ami                     = "linux"
}
Run Code Online (Sandbox Code Playgroud)

就像我说的,当我输入 2019 年或 2016 年时,它确实找到了正确的图像。错误消息如下:

module.aws_instance.data.aws_ami.latest: Refreshing state...

Error: Your query returned no results. Please change your search criteria and try again.
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

谢谢您的帮助!

PS:我故意清空了一些字段。

Mar*_*cin 1

AMI 名称和 AMI id 是两个不同的东西。您正在使用 AMI id (ami-0fb02dcdd38048fb9) 根据名称搜索 AMI。这显然是行不通的。您必须替换ami-0fb02dcdd38048fb9为 AMI 名称。

linux也许您还没有指定如何命名您的AMI Linux2019-CUSTOM*?无论您使用什么名称,都必须使用该名称,而不是 AMI id。