Terraform Error: Failed to fetch in ubuntu

Error

aws_instance.Chun-VM (remote-exec): E: Failed to fetch http://security.ubuntu.com/ubuntu/pool/main/a/apr-util/libaprutil1-ldap_1.6.1-5ubuntu4.22.04.1_amd64.deb  Unable to connect to us-east-1.ec2.archive.ubuntu.com:http:

Code

resource "aws_instance" "web" {
  ami           = "ami-053b0d53c279acc90"
  instance_type = "t3.micro"
  key_name 		= "aws-hl-training"

  tags = {
    Name = "HelloWorld-rajesh"
  }
  
  connection {
      type     = "ssh"
      user     = "ubuntu"
      private_key = file("aws-hl-training.pem")
      #host = aws_instance.web.public_ip
      host = self.public_ip
  } 
  
  provisioner "local-exec" {
    command = "mkdir devopsschool-local"
  }
  
 provisioner "remote-exec" {
    inline = [
	  "sudo apt-get update",
      "sudo apt-get install apache2 -y",
	  "sudo systemctl start apache2",
    ]
  }
  
  provisioner "file" {
    source      = "terraform.tfstate.backup"
    destination = "/tmp/"
  } 
  
  
}

output "instance_public_ip" {
  value = aws_instance.web.public_ip
}

output "instance_public_sg" {
  value = aws_instance.web.security_groups
}

Solution

$ sudo sed -i 's|^deb http://.*.ubuntu.com/ubuntu/.*|deb http://us.archive.ubuntu.com/ubuntu/ focal main restricted universe multiverse|' /etc/apt/sources.list
$ sudo apt update
$ sudo apt install libaprutil1-ldap
$ sudo apt-get update
$ sudo apt-get install apache2 -y
$ sudo systemctl start apache2

The error message you're seeing indicates that the apt package manager on your AWS EC2 instance is unable to connect to the Ubuntu package repositories to fetch a required package (libaprutil1-ldap) because it's unable to connect to the server us-east-1.ec2.archive.ubuntu.com.

Here are a few steps you can take to resolve this issue:

Check Network Connectivity:

Ensure that your EC2 instance has internet connectivity. Verify that it can reach external websites. You can try running a simple command like ping google.com to check if your instance can reach the internet. If not, there might be an issue with your VPC configuration or security groups.
Check DNS Configuration:

Confirm that your instance's DNS settings are correctly configured. Your instance should be able to resolve domain names. You can check your DNS configuration by examining the /etc/resolv.conf file. Make sure it contains valid DNS server IP addresses.

cat /etc/resolv.conf
If it doesn't, you can update it with the appropriate DNS server addresses.

Check Repository URL:

The error message mentions us-east-1.ec2.archive.ubuntu.com. This seems unusual for an Ubuntu repository URL. It should typically be archive.ubuntu.com. Check your /etc/apt/sources.list file to ensure that the repository URLs are correctly configured.
Update the Package Lists:

Before installing packages, it's a good practice to update the package lists first. Run the following command to refresh the package database:

sudo apt update
This command will fetch the latest package information from the Ubuntu repositories.

Retry the Installation:

After updating the package lists, try to install the package again. For example:

sudo apt install libaprutil1-ldap
This will attempt to install the package and its dependencies.

Choose a Different Mirror:

If you continue to face issues with the default Ubuntu repository mirror, you can consider changing to a different mirror. You can edit your /etc/apt/sources.list file and replace the default repository URLs with a mirror closer to your region.
For example, you can use the us.archive.ubuntu.com mirror:


deb http://us.archive.ubuntu.com/ubuntu/ focal main restricted universe multiverse
After editing the file, remember to run sudo apt update again.

Check Security Groups and Network ACLs:

Ensure that your AWS Security Groups and Network ACLs are correctly configured to allow outbound internet traffic from your EC2 instance.
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x