インフラ

EC2 AmazonLinux2でyumが失敗するときに疑うこと 3つの解決策

こんにちは、ちゃりおです。

適当にEC2作成したら、yumでエラーがでて焦りました。
(よくよく考えたら「それはそうだよな」と思いました)

yumが通らないときに疑うことについてです。

OSは、AmazonLinux2を想定しています。
RHELとかだとyumリポジトリがS3にないとかで挙動が違うかもです。

Contents

エラー内容

yumを叩くと以下のエラーが。
なにやら、amazonlinuxへのリポジトリへのアクセスが403にっているらしい。

$ sudo yum update
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Could not retrieve mirrorlist http://amazonlinux.us-east-1.amazonaws.com/2/core/latest/x86_64/mirror.list error was
14: HTTP Error 403 - Forbidden
 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

解決策

ec2からamazonlinuxのyumリポジトリへアクセスできないことが原因です。
(リポジトリはS3にあります)

パブリックサブネットに配置する

インターネットから直接アクセスできる場所にEC2をおいて問題なければ、
パブリックサブネットに配置してパブリックIPを割り当てれば解決です。

NATインスタンスまたはNATGWを作成する

プライベートサブネットに配置する必要があるEC2であれば、
NATインスタンス・NATGWを作成しルートテーブルに追加します。

プロキシサーバを作るのもいいかもしれません。

NATインスタンス
NATGW

S3エンドポイントを作成する

プライベートサブネットでNATインスタンス・NATGWなくてもできる方法です。
エンドポイントの作成と、EC2のIAMロールの変更が必要になります。

NATインスタンス・NATGWと使わない分、料金を抑えられます。
ただ、Amazonの提供しているyum repositoryのみです。(S3にあるため)

【小ネタ】えっ、Private SubnetからNATサーバを経由せずにyum updateができるって!?

補足 パブリックサブネット・プライベートサブネット

パブリックサブネット: インターネットに直接アウトバウンドトラフィックを送れる
プライベートサブネット: NATGWやNATインスタンスを介して、インターネットにアウトバウンドトラフィックを送れる

yumなどEC2側から通信するものはアウトバウンドトラフィックになります。

パブリックサブネット・プライベートサブネット

まとめ

今回は、AmazonLinux2でyumがエラーになる件についてです。
必要な通信によって、解決策は変わってきそうです。

以下のようになると思います。

  • パブリックサブネットにおいて良ければ、パブリックサブネットに配置
  • プライベートサブネットであれば、NATインスタンス・NATGW
  • プライベートサブネットでNAT作ることができなければ、S3エンドポイント

適当に作ってしまって、思わぬところではまってしまいました。
そのインスタンスはどういった通信が必要なのかを、意識してリソース作成するのは大切ですね。

CIツール比較
CIツール コスト比較 GithubActions,TravisCI,CircleCIこんにちは、ちゃりおです。 今回はCIツール比較です。 CIツールの比較をするとき、できることや料金を比較すると思います。 メジャー...
php_nginx
amazonlinux2にnginx + php-fpm(php7.3)をインストールしてみるこんにちは、ちゃりおです。 いままでapacheを使う機会が多く、nginxを使ったことがありませんでした。 最近、nginxを初めて...
aws saa
AWS初心者が3週間でAWSソリューションアーキテクト(SAA)を取った勉強法こんにちは、ちゃりお(@chari7311)です! 以前社内の事情でAWSソリューションアーキテクトを3週間で取るよう言われました。 ...