EC2에서 yum 명령어 실행 시 나타나는 Cannot find a valid baseurl for repo: amzn2-core/2/x86_64 에러 해결

EC2에서 yum 명령어를 실행하면 나타나는「Cannot find a valid baseurl for repo: amzn2-core/2/x86_64」라는 에러를 해결 하는 방법에 대해서 정리해 봤습니다.
2022.07.22

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

안녕하세요 클래스메소드 김재욱(Kim Jaewook) 입니다. 이번에는 EC2에서 yum 명령어를 실행하면 나타나는「Cannot find a valid baseurl for repo: amzn2-core/2/x86_64」라는 에러를 해결 하는 방법에 대해서 정리해 봤습니다.

에러 발생

EC2 생성시 사용자 데이터에 다음과 같은 코드를 입력하고 EC2를 생성합니다.

결과물로써는 EC2 Public IP로 접속할 시 아파치 웹 페이지가 표시될 것입니다.

하지만 EC2 인스턴스의 Public IP로 접속하면 Bad Gateway가 표시되면서 접속이 되지 않습니다.

문제 해결

에러 로그를 확인하기 위해 유저 데이터 로그를 확인합니다.

sudo cat /var/log/cloud-init-output.log

EC2 인스턴스로 접속한 다음. 해당 명령어를 입력해서 유저 데이터 로그를 출력합니다.


One of the configured repositories failed (Unknown),
 and yum doesn't have enough cached data to continue. At this point the only
 safe thing yum can do is fail. There are a few ways to work "fix" this:

     1. Contact the upstream for the repository and get them to fix the problem.

     2. Reconfigure the baseurl/etc. for the repository, to point to a working
        upstream. This is most often useful if you are using a newer
        distribution release than is supported by the repository (and the
        packages for the previous distribution release still work).

     3. Run the command with the repository temporarily disabled
            yum --disablerepo= ...

     4. Disable the repository permanently, so yum won't use it by default. Yum
        will then just ignore the repository until you permanently enable it
        again or use --enablerepo for temporary usage:

            yum-config-manager --disable 
        or
            subscription-manager repos --disable=

     5. Configure the failing repository to be skipped, if it is unavailable.
        Note that yum will try to contact the repo. when it runs most commands,
        so will have to try and fail each time (and thus. yum will be be much
        slower). If it is a very temporary problem though, this is often a nice
        compromise:

            yum-config-manager --save --setopt=.skip_if_unavailable=true

Cannot find a valid baseurl for repo: amzn2-core/2/x86_64
Could not retrieve mirrorlist https://amazonlinux-2-repos-ap-northeast-1.s3.dualstack.ap-northeast-1.amazonaws.com/2/core/latest/x86_64/mirror.list error was
12: Timeout on https://amazonlinux-2-repos-ap-northeast-1.s3.dualstack.ap-northeast-1.amazonaws.com/2/core/latest/x86_64/mirror.list: (28, 'Failed to connect to amazonlinux-2-repos-ap-northeast-1.s3.dualstack.ap-northeast-1.amazonaws.com port 443 after 2702 ms: Connection timed out')
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd

로그를 확인해 보면 yum 명령어에서 에러가 발생하는 것을 확인할 수 있습니다.


[ec2-user@ip-xx-x-x-xx ~]$ ping www.google.com
PING www.google.com (142.251.42.132) 56(84) bytes of data.

인터넷 상태를 확인하기 위해 www.google.com로 ping을 보내보면, 핑이 날아가지 않는 것을 확인할 수 있습니다.

Security Group에서 인바운드 규칙과 아웃바운드 규칙을 재차 확인합니다.

간혹 IaC로 작성할 시 아웃바운드 규칙을 빼먹는 경우가 종종 있는데, 아웃바운드 규칙을 제대로 설정했는지 확인한 다음, EC2 인스턴스를 다시 생성합니다.


Complete!
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
Server version: Apache/2.4.51 ()
Server built: Oct 8 2021 22:03:47
Cloud-init v. 19.3-45.amzn2 finished at Fri, 22 Jul 2022 13:17:00 +0000. Datasource DataSourceEc2. Up 68.03 seconds

재생성한 EC2 인스턴스로 접속한 다음 유저 데이터 로그를 확인해 보면 아파치까지 문제 없이 설치된 것을 확인할 수 있습니다.

EC2 인스턴스의 Public IP로 접속하면 정상적으로 아파치 웹 페이지가 표시돠는 것을 확인할 수 있습니다.

그 외「Cannot find a valid baseurl for repo: amzn2-core/2/x86_64」에러 해결 방법에 대해서는 아래 블로그를 참고해 주세요.

본 블로그 게시글을 보시고 문의 사항이 있으신 분들은 클래스메소드코리아 (info@classmethod.kr)로 연락 주시면 빠른 시일 내 담당자가 회신 드릴 수 있도록 하겠습니다 !