Local-exec with sudo privileges

I want to update my local hosts file with the public IP of the newly created EC2 instance. But when I use sudo it does not allow me time to enter the password, it just moves on and then gets stuck on Still creating… forever. It works fine if I run terraform apply as root. Just wondering if anyone has found a better solution to this.

aws_instance.test: Still creating... [10s elapsed]
aws_instance.test: Provisioning with 'local-exec'...
aws_instance.test (local-exec): Executing: ["/bin/sh" "-c" "sudo sed -i '' 's/.*ec2-test/ ec2-test/' /etc/hosts"]
Password:aws_instance.test: Still creating... [20s elapsed]
aws_instance.test: Still creating... [30s elapsed]

This is the sample code

resource "aws_instance" "test" {
    ami                         = "ami-005c06c6de69aee84" # us-west-1
    instance_type               = "t3a.micro"
    associate_public_ip_address = true
    key_name                    = "alt_cf_shared"
    provisioner "local-exec" {
        command = "sudo sed -i '' 's/.*ec2-test/${self.public_ip} ec2-test/' /etc/hosts"