ubuntu 18.04 的打包机生成器 source_ami_filter?

packer builder source_ami_filter for ubuntu 18.04?

我正在尝试打包机。

我能够通过

为 ubuntu 16.04 创建图像
"source_ami_filter": {
          "filters": {
            "virtualization-type": "hvm",
            "name": "ubuntu/images/*ubuntu-xenial-16.04-amd64-server-*",
            "root-device-type": "ebs"
          },

但是当它通过将 16.04 修改为 18.04 来尝试相同的文件管理器时

"source_ami_filter": {
          "filters": {
            "virtualization-type": "hvm",
            "name": "ubuntu/images/*ubuntu-xenial-18.04-amd64-server-*",
            "root-device-type": "ebs"
          },

我收到以下错误。

==> amazon-ebs: Prevalidating any provided VPC information
==> amazon-ebs: Prevalidating AMI Name: packer-example 1592389575
==> amazon-ebs: No AMI was found matching filters: {
==> amazon-ebs:   Filters: [{
==> amazon-ebs:       Name: "virtualization-type",
==> amazon-ebs:       Values: ["hvm"]
==> amazon-ebs:     },{
==> amazon-ebs:       Name: "name",
==> amazon-ebs:       Values: ["ubuntu/images/*ubuntu-xenial-18.04-amd64-server-*"]
==> amazon-ebs:     },{
==> amazon-ebs:       Name: "root-device-type",
==> amazon-ebs:       Values: ["ebs"]
==> amazon-ebs:     }],
==> amazon-ebs:   Owners: ["099720109477"]
==> amazon-ebs: }
Build 'amazon-ebs' errored: No AMI was found matching filters: {
  Filters: [{
      Name: "virtualization-type",
      Values: ["hvm"]
    },{
      Name: "name",
      Values: ["ubuntu/images/*ubuntu-xenial-18.04-amd64-server-*"]
    },{
      Name: "root-device-type",
      Values: ["ebs"]
    }],
  Owners: ["099720109477"]
}

==> Some builds didn't complete successfully and had errors:
--> amazon-ebs: No AMI was found matching filters: {
  Filters: [{
      Name: "virtualization-type",
      Values: ["hvm"]
    },{
      Name: "name",
      Values: ["ubuntu/images/*ubuntu-xenial-18.04-amd64-server-*"]
    },{
      Name: "root-device-type",
      Values: ["ebs"]
    }],
  Owners: ["099720109477"]
}

想了解过滤器值的工作原理:

  1. 18.04 ?

  2. 换另一张图片(linux/redhat) ?

有人可以解决我对此的疑问吗?

重要说明:您应该设置一个 owners 属性,就像我在此处的示例中所设置的那样,否则您将很容易引入与您的模式匹配的恶意 AMI。 名称 字段由用户控制,未检查。

更新:由于这是关于 Packer 而不是 Terraform,这里是打包器解决方案:

"source_ami_filter": {
  "filters": {
    "virtualization-type": "hvm",
    "architecture": "x86_64",
    "name": "ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-*",
    "block-device-mapping.volume-type": "gp2",
    "root-device-type": "ebs"
  },
  "owners": ["099720109477"],
  "most_recent": true
},

这是我的善意但偏离主题的 Terraform 解决方案:

data "aws_ami" "ubuntu-18_04" {
  most_recent = true
  owners = ["${var.ubuntu_account_number}"]

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

variable "ubuntu_account_number" {
  default = "099720109477"
}

或者如果您想使用您自己的 KMS CMK 对其进行加密:

resource "aws_ami_copy" "ubuntu-18_04-encrypted" {
  name              = "${data.aws_ami.ubuntu-18_04.name}-encrypted"
  description       = "${data.aws_ami.ubuntu-18_04.description} (encrypted)"
  source_ami_id     = "${data.aws_ami.ubuntu-18_04.id}"
  source_ami_region = "${var.region}"
  encrypted         = true

  tags {
    ImageType      = "encrypted-ubuntu-18_04"
  }
}

data "aws_ami" "ubuntu-18_04" {
  most_recent = true
  owners = ["${var.ubuntu_account_number}"]

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

variable "ubuntu_account_number" {
  default = "099720109477"
}

我从 Terraform: Latest Ubuntu 18.04 LTS encrypted AMI gist on GitHub.

中引用了这些

在这里,在 2021 年,我们使用 Packer HCL2 语言和 Ubuntu 20.04 并支持乘法架构

variable "arch" {
  type    = string
  default = "${env("ARCH")}"
}

source "amazon-ebs" "ubuntu" {

  source_ami_filter {
    filters = {
      name = "ubuntu/images/*ubuntu-focal-20.04-*-server-*"
      architecture = "${var.arch}"
      root-device-type = "ebs"
      virtualization-type = "hvm"
    }

    most_recent = true
    owners = ["099720109477"]
  }
}