[Solved] Packer: sudo: sorry, you must have a tty to run sudo Error

While packaging images using the packer, you might end up having the following error.

sudo: sorry, you must have a tty to run sudo.

You can solve this issue by just adding ssh_pty flag to the builder block of packer configuration as shown below.

"ssh_pty" : "true"

One such example builder is given below.

"builders": [{
    "type": "amazon-ebs",
    "region": "{{user `region`}}",
    "vpc_id": "vpc-72fc43517",
    "subnet_id":  "{{user `subnetId`}}",
    "source_ami": "{{user `amiId`}}",
    "instance_type": "{{user `instanceType`}}",
    "ssh_username": "{{user `sshUsername`}}",
    "ami_name": "{{user `amiName`}}-{{user `amiVersion` }}",
    "tags": {
        "Name": "{{user `amiName`}}-{{user `amiVersion` }}",
        "OS_Version": "{{user `osVersion`}}"
      },
    "ssh_pty" : "true"
  }],