Ubuntu 20.04.3 AMI created with aws-chroot has /etc/resolv.conf missing (and fails to resolve dns)

I am having the same issue reported in AMI created with aws-chroot has /etc/resolv.conf missing · Issue #4698 · hashicorp/packer · GitHub, but with Ubuntu 20.04.

When I build a minimal Ubuntu 20.04 image and boot it, there is no /etc/resolv.conf (it should be a link to /run/systemd/resolve/stub-resolv.conf.

The result is no network connectivity for most things, for instance ping google.com fails. Otherwise it appears that netplan and systemd-resolv are configured correctly.

This is my packer file:

variable "aws_region" {
  default = env("AWS_REGION")
}

variable "ubuntu_version" {
  type    = string
  default = "focal-20.04"
}

data "amazon-ami" "ubuntu" {
  filters = {
    virtualization-type = "hvm"
    name                = "ubuntu/images/*ubuntu-${var.ubuntu_version}-amd64-server-*"
    root-device-type    = "ebs"
  }
  owners      = ["099720109477"]
  most_recent = true
  region      = var.aws_region
}

source "amazon-chroot" "ubuntu" {
  region     = var.aws_region
  source_ami = data.amazon-ami.ubuntu.id
  ami_name   = "ubuntu-${var.ubuntu_version}-${formatdate("YYYY-MM-DD'T'hh-mm", timestamp())}"
}

build {
  sources = [
    "source.amazon-chroot.ubuntu"
  ]

  provisioner "shell" {
    inline = [
      "echo hello world"
    ]
  }
}

I ended up hacking around this with a systemd service:

[Unit]
Description=Fix missing resolv.conf
After=systemd-resolved.service

[Service]
ExecStart=/usr/bin/ln -sf /run/systemd/resolve/stub-resolv.conf /etc/resolv.conf

[Install]
WantedBy=multi-user.target

But I’d really love to understand why this isn’t working out of the box!